Avoid to shadow 'boolean' on weird platform (Windows)
[libmodbus.git] / tests / bandwidth-server-one.c
blob9627f2ad5175cad8785e87197e86e2b7d1e29bab
1 /*
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/>.
18 #include <stdio.h>
19 #ifndef _MSC_VER
20 #include <unistd.h>
21 #endif
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
26 #include <modbus.h>
28 #if defined(_WIN32)
29 #define close closesocket
30 #endif
32 enum {
33 TCP,
34 RTU
37 int main(int argc, char *argv[])
39 int socket = -1;
40 modbus_t *ctx = NULL;
41 modbus_mapping_t *mb_mapping = NULL;
42 int rc;
43 int use_backend;
45 /* TCP */
46 if (argc > 1) {
47 if (strcmp(argv[1], "tcp") == 0) {
48 use_backend = TCP;
49 } else if (strcmp(argv[1], "rtu") == 0) {
50 use_backend = RTU;
51 } else {
52 printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n", argv[0]);
53 exit(1);
55 } else {
56 /* By default */
57 use_backend = TCP;
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);
65 } else {
66 ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
67 modbus_set_slave(ctx, 1);
68 modbus_connect(ctx);
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));
76 modbus_free(ctx);
77 return -1;
80 for(;;) {
81 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
83 rc = modbus_receive(ctx, query);
84 if (rc > 0) {
85 modbus_reply(ctx, query, rc, mb_mapping);
86 } else if (rc == -1) {
87 /* Connection closed by the client or error */
88 break;
92 printf("Quit the loop: %s\n", modbus_strerror(errno));
94 modbus_mapping_free(mb_mapping);
95 if (socket != -1) {
96 close(socket);
98 /* For RTU, skipped by TCP (no TCP connect) */
99 modbus_close(ctx);
100 modbus_free(ctx);
102 return 0;