2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
18 #define close closesocket
26 int main(int argc
, char *argv
[])
30 modbus_mapping_t
*mb_mapping
= NULL
;
36 if (strcmp(argv
[1], "tcp") == 0) {
38 } else if (strcmp(argv
[1], "rtu") == 0) {
41 printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n",
50 if (use_backend
== TCP
) {
51 ctx
= modbus_new_tcp("127.0.0.1", 1502);
52 s
= modbus_tcp_listen(ctx
, 1);
53 modbus_tcp_accept(ctx
, &s
);
56 ctx
= modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
57 modbus_set_slave(ctx
, 1);
62 modbus_mapping_new(MODBUS_MAX_READ_BITS
, 0, MODBUS_MAX_READ_REGISTERS
, 0);
63 if (mb_mapping
== NULL
) {
64 fprintf(stderr
, "Failed to allocate the mapping: %s\n", modbus_strerror(errno
));
70 uint8_t query
[MODBUS_TCP_MAX_ADU_LENGTH
];
72 rc
= modbus_receive(ctx
, query
);
74 modbus_reply(ctx
, query
, rc
, mb_mapping
);
75 } else if (rc
== -1) {
76 /* Connection closed by the client or error */
81 printf("Quit the loop: %s\n", modbus_strerror(errno
));
83 modbus_mapping_free(mb_mapping
);
87 /* For RTU, skipped by TCP (no TCP connect) */