2 * Copyright © 2008-2013 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define close closesocket
37 int main(int argc
, char *argv
[])
41 modbus_mapping_t
*mb_mapping
= NULL
;
47 if (strcmp(argv
[1], "tcp") == 0) {
49 } else if (strcmp(argv
[1], "rtu") == 0) {
52 printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n", argv
[0]);
60 if (use_backend
== TCP
) {
61 ctx
= modbus_new_tcp("127.0.0.1", 1502);
62 socket
= modbus_tcp_listen(ctx
, 1);
63 modbus_tcp_accept(ctx
, &socket
);
66 ctx
= modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
67 modbus_set_slave(ctx
, 1);
71 mb_mapping
= modbus_mapping_new(MODBUS_MAX_READ_BITS
, 0,
72 MODBUS_MAX_READ_REGISTERS
, 0);
73 if (mb_mapping
== NULL
) {
74 fprintf(stderr
, "Failed to allocate the mapping: %s\n",
75 modbus_strerror(errno
));
81 uint8_t query
[MODBUS_TCP_MAX_ADU_LENGTH
];
83 rc
= modbus_receive(ctx
, query
);
85 modbus_reply(ctx
, query
, rc
, mb_mapping
);
86 } else if (rc
== -1) {
87 /* Connection closed by the client or error */
92 printf("Quit the loop: %s\n", modbus_strerror(errno
));
94 modbus_mapping_free(mb_mapping
);
98 /* For RTU, skipped by TCP (no TCP connect) */