2 * Copyright © 2008-2014 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", argv
[0]);
49 if (use_backend
== TCP
) {
50 ctx
= modbus_new_tcp("127.0.0.1", 1502);
51 s
= modbus_tcp_listen(ctx
, 1);
52 modbus_tcp_accept(ctx
, &s
);
55 ctx
= modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
56 modbus_set_slave(ctx
, 1);
60 mb_mapping
= modbus_mapping_new(MODBUS_MAX_READ_BITS
, 0,
61 MODBUS_MAX_READ_REGISTERS
, 0);
62 if (mb_mapping
== NULL
) {
63 fprintf(stderr
, "Failed to allocate the mapping: %s\n",
64 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) */