Debian Codegraph @

tests/bandwidth-client.c

raw symbols SCIP
1 /*
2 * Copyright © Stéphane Raimbault <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <stdio.h>
8 #ifndef _MSC_VER
9 #include <sys/time.h>
10 #include <unistd.h>
11 #endif
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17
18 #include <modbus.h>cxx . . $ `<file>/src/modbus.h`/
19
20 #define G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! 1000
21
22 static uint32_tcxx . . $ uint32_t# gettime_mscxx . . $ gettime_ms(db52760e79bf9054).(void)
23 {
24 struct timevalcxx . . $ timeval# tv;
25 #if !defined(_MSC_VER)
26 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
27 return (uint32_tcxx . . $ uint32_t#) tv.tv_seccxx . . $ timeval#tv_sec. * 1000 + tv.tv_useccxx . . $ timeval#tv_usec. / 1000;
28 #else
29 return GetTickCount();
30 #endif
31 }
32
33 enumcxx . . $ $anonymous_type_b3925212981fe5e5_0# {
34 TCPcxx . . $ TCP.,
35 RTUcxx . . $ RTU.
36 };
37
38 /* Tests based on PI-MBUS-300 documentation */
39 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
40 {
41 uint8_tcxx . . $ uint8_t# *tab_bit;
42 uint16_tcxx . . $ uint16_t# *tab_reg;
43 modbus_tcxx . . $ modbus_t# *ctx;
44 int i;
45 int nb_points;
46 double elapsed;
47 uint32_tcxx . . $ uint32_t# start;
48 uint32_tcxx . . $ uint32_t# end;
49 uint32_tcxx . . $ uint32_t# bytes;
50 uint64_tcxx . . $ uint64_t# rate;
51 int rc;
52 int n_loop;
53 int use_backend;
54
55 if (argc > 1) {
56 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "tcp") == 0) {
57 use_backend = TCPcxx . . $ TCP.;
58 n_loop = 100000;
59 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[1], "rtu") == 0) {
60 use_backend = RTUcxx . . $ RTU.;
61 n_loop = 100;
62 } else {
63 printfcxx . . $ printf(581fddd72a8db8fc).("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
64 argv[0]);
65 exitcxx . . $ exit(3ca292c338df9bb3).(1);
66 }
67 } else {
68 /* By default */
69 use_backend = TCPcxx . . $ TCP.;
70 n_loop = 100000;
71 }
72
73 if (use_backend == TCPcxx . . $ TCP.) {
74 ctx = modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).("127.0.0.1", 1502);
75 } else {
76 ctx = modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).("/dev/ttyUSB1", 115200, 'N', 8, 1);
77 modbus_set_slavecxx . . $ modbus_set_slave(2a7e0e283938d456).(ctx, 1);
78 }
79 if (modbus_connectcxx . . $ modbus_connect(c57adc5f48380e39).(ctx) == -1) {
80 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Connection failed: %s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
81 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
82 return -1;
83 }
84
85 /* Allocate and initialize the memory to store the status */
86 tab_bit = (uint8_tcxx . . $ uint8_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`! * sizeof(uint8_tcxx . . $ uint8_t#));
87 memsetcxx . . $ memset(d1de1e4fb215f1c6).(tab_bit, 0, MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`! * sizeof(uint8_tcxx . . $ uint8_t#));
88
89 /* Allocate and initialize the memory to store the registers */
90 tab_reg = (uint16_tcxx . . $ uint16_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`! * sizeof(uint16_tcxx . . $ uint16_t#));
91 memsetcxx . . $ memset(d1de1e4fb215f1c6).(tab_reg, 0, MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`! * sizeof(uint16_tcxx . . $ uint16_t#));
92
93 printfcxx . . $ printf(581fddd72a8db8fc).("READ BITS\n\n");
94
95 nb_points = MODBUS_MAX_READ_BITScxx . . $ `src/modbus.h:83:9`!;
96 start = gettime_mscxx . . $ gettime_ms(db52760e79bf9054).();
97 for (i = 0; i < n_loop; i++) {
98 rc = modbus_read_bitscxx . . $ modbus_read_bits(11f4da49ebe31404).(ctx, 0, nb_points, tab_bit);
99 if (rc == -1) {
100 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
101 return -1;
102 }
103 }
104 end = gettime_mscxx . . $ gettime_ms(db52760e79bf9054).();
105 elapsed = end - start;
106
107 rate = (uint64_tcxx . . $ uint64_t#) n_loop * nb_points * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
108 printfcxx . . $ printf(581fddd72a8db8fc).("Transfer rate in points/seconds:\n");
109 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " points/s\n", rate);
110 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
111
112 bytes = n_loop * ((nb_points / 8) + ((nb_points % 8) ? 1 : 0));
113 rate = (uint64_tcxx . . $ uint64_t#) bytes / 1024 * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
114 printfcxx . . $ printf(581fddd72a8db8fc).("Values:\n");
115 printfcxx . . $ printf(581fddd72a8db8fc).("* %d x %d values\n", n_loop, nb_points);
116 printfcxx . . $ printf(581fddd72a8db8fc).("* %.3f ms for %d bytes\n", elapsed, bytes);
117 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " KiB/s\n", rate);
118 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
119
120 /* TCP: Query and response header and values */
121 bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
122 printfcxx . . $ printf(581fddd72a8db8fc).("Values and TCP Modbus overhead:\n");
123 printfcxx . . $ printf(581fddd72a8db8fc).("* %d x %d bytes\n", n_loop, bytes);
124 bytes = n_loop * bytes;
125 rate = (uint64_tcxx . . $ uint64_t#) bytes / 1024 * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
126 printfcxx . . $ printf(581fddd72a8db8fc).("* %.3f ms for %d bytes\n", elapsed, bytes);
127 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " KiB/s\n", rate);
128 printfcxx . . $ printf(581fddd72a8db8fc).("\n\n");
129
130 printfcxx . . $ printf(581fddd72a8db8fc).("READ REGISTERS\n\n");
131
132 nb_points = MODBUS_MAX_READ_REGISTERScxx . . $ `src/modbus.h:93:9`!;
133 start = gettime_mscxx . . $ gettime_ms(db52760e79bf9054).();
134 for (i = 0; i < n_loop; i++) {
135 rc = modbus_read_registerscxx . . $ modbus_read_registers(e941881bef6bc78c).(ctx, 0, nb_points, tab_reg);
136 if (rc == -1) {
137 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
138 return -1;
139 }
140 }
141 end = gettime_mscxx . . $ gettime_ms(db52760e79bf9054).();
142 elapsed = end - start;
143
144 rate = (uint64_tcxx . . $ uint64_t#) n_loop * nb_points * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
145 printfcxx . . $ printf(581fddd72a8db8fc).("Transfer rate in points/seconds:\n");
146 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " registers/s\n", rate);
147 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
148
149 bytes = n_loop * nb_points * sizeof(uint16_tcxx . . $ uint16_t#);
150 rate = (uint64_tcxx . . $ uint64_t#) bytes / 1024 * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
151 printfcxx . . $ printf(581fddd72a8db8fc).("Values:\n");
152 printfcxx . . $ printf(581fddd72a8db8fc).("* %d x %d values\n", n_loop, nb_points);
153 printfcxx . . $ printf(581fddd72a8db8fc).("* %.3f ms for %d bytes\n", elapsed, bytes);
154 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " KiB/s\n", rate);
155 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
156
157 /* TCP:Query and response header and values */
158 bytes = 12 + 9 + (nb_points * sizeof(uint16_tcxx . . $ uint16_t#));
159 printfcxx . . $ printf(581fddd72a8db8fc).("Values and TCP Modbus overhead:\n");
160 printfcxx . . $ printf(581fddd72a8db8fc).("* %d x %d bytes\n", n_loop, bytes);
161 bytes = n_loop * bytes;
162 rate = (uint64_tcxx . . $ uint64_t#) bytes / 1024 * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
163 printfcxx . . $ printf(581fddd72a8db8fc).("* %.3f ms for %d bytes\n", elapsed, bytes);
164 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " KiB/s\n", rate);
165 printfcxx . . $ printf(581fddd72a8db8fc).("\n\n");
166
167 printfcxx . . $ printf(581fddd72a8db8fc).("WRITE AND READ REGISTERS\n\n");
168
169 nb_points = MODBUS_MAX_WR_WRITE_REGISTERScxx . . $ `src/modbus.h:95:9`!;
170 start = gettime_mscxx . . $ gettime_ms(db52760e79bf9054).();
171 for (i = 0; i < n_loop; i++) {
172 rc = modbus_write_and_read_registerscxx . . $ modbus_write_and_read_registers(c6a0b9119112ae8e).(
173 ctx, 0, nb_points, tab_reg, 0, nb_points, tab_reg);
174 if (rc == -1) {
175 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s\n", modbus_strerrorcxx . . $ modbus_strerror(f7ae7e56280f18ae).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
176 return -1;
177 }
178 }
179 end = gettime_mscxx . . $ gettime_ms(db52760e79bf9054).();
180 elapsed = end - start;
181
182 rate = (uint64_tcxx . . $ uint64_t#) n_loop * nb_points * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
183 printfcxx . . $ printf(581fddd72a8db8fc).("Transfer rate in points/seconds:\n");
184 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " registers/s\n", rate);
185 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
186
187 bytes = n_loop * nb_points * sizeof(uint16_tcxx . . $ uint16_t#);
188 rate = (uint64_tcxx . . $ uint64_t#) bytes / 1024 * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
189 printfcxx . . $ printf(581fddd72a8db8fc).("Values:\n");
190 printfcxx . . $ printf(581fddd72a8db8fc).("* %d x %d values\n", n_loop, nb_points);
191 printfcxx . . $ printf(581fddd72a8db8fc).("* %.3f ms for %d bytes\n", elapsed, bytes);
192 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " KiB/s\n", rate);
193 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
194
195 /* TCP:Query and response header and values */
196 bytes = 12 + 9 + (nb_points * sizeof(uint16_tcxx . . $ uint16_t#));
197 printfcxx . . $ printf(581fddd72a8db8fc).("Values and TCP Modbus overhead:\n");
198 printfcxx . . $ printf(581fddd72a8db8fc).("* %d x %d bytes\n", n_loop, bytes);
199 bytes = n_loop * bytes;
200 rate = (uint64_tcxx . . $ uint64_t#) bytes / 1024 * G_MSEC_PER_SECcxx . . $ `tests/bandwidth-client.c:20:9`! / (end - start);
201 printfcxx . . $ printf(581fddd72a8db8fc).("* %.3f ms for %d bytes\n", elapsed, bytes);
202 printfcxx . . $ printf(581fddd72a8db8fc).("* %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`! " KiB/s\n", rate);
203 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
204
205 /* Free the memory */
206 freecxx . . $ free(5d22bdacc48458e8).(tab_bit);
207 freecxx . . $ free(5d22bdacc48458e8).(tab_reg);
208
209 /* Close the connection */
210 modbus_closecxx . . $ modbus_close(24b4870c26038e9).(ctx);
211 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
212
213 return 0;
214 }