2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
19 #define G_MSEC_PER_SEC 1000
21 static uint32_t gettime_ms(void)
24 #if !defined(_MSC_VER)
25 gettimeofday(&tv
, NULL
);
26 return (uint32_t) tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
28 return GetTickCount();
37 /* Tests based on PI-MBUS-300 documentation */
38 int main(int argc
, char *argv
[])
55 if (strcmp(argv
[1], "tcp") == 0) {
58 } else if (strcmp(argv
[1], "rtu") == 0) {
62 printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
72 if (use_backend
== TCP
) {
73 ctx
= modbus_new_tcp("127.0.0.1", 1502);
75 ctx
= modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
76 modbus_set_slave(ctx
, 1);
78 if (modbus_connect(ctx
) == -1) {
79 fprintf(stderr
, "Connection failed: %s\n", modbus_strerror(errno
));
84 /* Allocate and initialize the memory to store the status */
85 tab_bit
= (uint8_t *) malloc(MODBUS_MAX_READ_BITS
* sizeof(uint8_t));
86 memset(tab_bit
, 0, MODBUS_MAX_READ_BITS
* sizeof(uint8_t));
88 /* Allocate and initialize the memory to store the registers */
89 tab_reg
= (uint16_t *) malloc(MODBUS_MAX_READ_REGISTERS
* sizeof(uint16_t));
90 memset(tab_reg
, 0, MODBUS_MAX_READ_REGISTERS
* sizeof(uint16_t));
92 printf("READ BITS\n\n");
94 nb_points
= MODBUS_MAX_READ_BITS
;
96 for (i
= 0; i
< n_loop
; i
++) {
97 rc
= modbus_read_bits(ctx
, 0, nb_points
, tab_bit
);
99 fprintf(stderr
, "%s\n", modbus_strerror(errno
));
104 elapsed
= end
- start
;
106 rate
= (n_loop
* nb_points
) * G_MSEC_PER_SEC
/ (end
- start
);
107 printf("Transfer rate in points/seconds:\n");
108 printf("* %d points/s\n", rate
);
111 bytes
= n_loop
* (nb_points
/ 8) + ((nb_points
% 8) ? 1 : 0);
112 rate
= bytes
/ 1024 * G_MSEC_PER_SEC
/ (end
- start
);
114 printf("* %d x %d values\n", n_loop
, nb_points
);
115 printf("* %.3f ms for %d bytes\n", elapsed
, bytes
);
116 printf("* %d KiB/s\n", rate
);
119 /* TCP: Query and response header and values */
120 bytes
= 12 + 9 + (nb_points
/ 8) + ((nb_points
% 8) ? 1 : 0);
121 printf("Values and TCP Modbus overhead:\n");
122 printf("* %d x %d bytes\n", n_loop
, bytes
);
123 bytes
= n_loop
* bytes
;
124 rate
= bytes
/ 1024 * G_MSEC_PER_SEC
/ (end
- start
);
125 printf("* %.3f ms for %d bytes\n", elapsed
, bytes
);
126 printf("* %d KiB/s\n", rate
);
129 printf("READ REGISTERS\n\n");
131 nb_points
= MODBUS_MAX_READ_REGISTERS
;
132 start
= gettime_ms();
133 for (i
= 0; i
< n_loop
; i
++) {
134 rc
= modbus_read_registers(ctx
, 0, nb_points
, tab_reg
);
136 fprintf(stderr
, "%s\n", modbus_strerror(errno
));
141 elapsed
= end
- start
;
143 rate
= (n_loop
* nb_points
) * G_MSEC_PER_SEC
/ (end
- start
);
144 printf("Transfer rate in points/seconds:\n");
145 printf("* %d registers/s\n", rate
);
148 bytes
= n_loop
* nb_points
* sizeof(uint16_t);
149 rate
= bytes
/ 1024 * G_MSEC_PER_SEC
/ (end
- start
);
151 printf("* %d x %d values\n", n_loop
, nb_points
);
152 printf("* %.3f ms for %d bytes\n", elapsed
, bytes
);
153 printf("* %d KiB/s\n", rate
);
156 /* TCP:Query and response header and values */
157 bytes
= 12 + 9 + (nb_points
* sizeof(uint16_t));
158 printf("Values and TCP Modbus overhead:\n");
159 printf("* %d x %d bytes\n", n_loop
, bytes
);
160 bytes
= n_loop
* bytes
;
161 rate
= bytes
/ 1024 * G_MSEC_PER_SEC
/ (end
- start
);
162 printf("* %.3f ms for %d bytes\n", elapsed
, bytes
);
163 printf("* %d KiB/s\n", rate
);
166 printf("WRITE AND READ REGISTERS\n\n");
168 nb_points
= MODBUS_MAX_WR_WRITE_REGISTERS
;
169 start
= gettime_ms();
170 for (i
= 0; i
< n_loop
; i
++) {
171 rc
= modbus_write_and_read_registers(
172 ctx
, 0, nb_points
, tab_reg
, 0, nb_points
, tab_reg
);
174 fprintf(stderr
, "%s\n", modbus_strerror(errno
));
179 elapsed
= end
- start
;
181 rate
= (n_loop
* nb_points
) * G_MSEC_PER_SEC
/ (end
- start
);
182 printf("Transfer rate in points/seconds:\n");
183 printf("* %d registers/s\n", rate
);
186 bytes
= n_loop
* nb_points
* sizeof(uint16_t);
187 rate
= bytes
/ 1024 * G_MSEC_PER_SEC
/ (end
- start
);
189 printf("* %d x %d values\n", n_loop
, nb_points
);
190 printf("* %.3f ms for %d bytes\n", elapsed
, bytes
);
191 printf("* %d KiB/s\n", rate
);
194 /* TCP:Query and response header and values */
195 bytes
= 12 + 9 + (nb_points
* sizeof(uint16_t));
196 printf("Values and TCP Modbus overhead:\n");
197 printf("* %d x %d bytes\n", n_loop
, bytes
);
198 bytes
= n_loop
* bytes
;
199 rate
= bytes
/ 1024 * G_MSEC_PER_SEC
/ (end
- start
);
200 printf("* %.3f ms for %d bytes\n", elapsed
, bytes
);
201 printf("* %d KiB/s\n", rate
);
204 /* Free the memory */
208 /* Close the connection */