Commit b8c0558c192cd349124aff5faac8a3f1f0e32362

Authored by Stéphane Raimbault
1 parent ddbd7266

Rename modbus_[listen|accept] to modbus_tcp_[listen|accept]

These functions have no meaning in RTU so it's better to specialize
the names and remove them from the backend.

- remove the functions from the backend
- update tests to handle RTU mode (master and slave)
- add command line options to tests (rtu or tcp)
src/modbus-private.h
... ... @@ -87,8 +87,6 @@ typedef struct _modbus_backend {
87 87 int (*connect) (modbus_t *ctx);
88 88 void (*close) (modbus_t *ctx);
89 89 int (*flush) (modbus_t *ctx);
90   - int (*listen) (modbus_t *ctx, int nb_connection);
91   - int (*accept) (modbus_t *ctx, int *socket);
92 90 int (*select) (modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length);
93 91 int (*filter_request) (modbus_t *ctx, int slave);
94 92 } modbus_backend_t;
... ...
src/modbus-rtu.c
... ... @@ -707,40 +707,12 @@ int _modbus_rtu_flush(modbus_t *ctx)
707 707 #if defined(_WIN32)
708 708 modbus_rtu_t *ctx_rtu = ctx->backend_data;
709 709 ctx_rtu->w_ser.n_bytes = 0;
710   - return ( FlushFileBuffers(ctx_rtu->w_ser.fd) == FALSE );
  710 + return (FlushFileBuffers(ctx_rtu->w_ser.fd) == FALSE);
711 711 #else
712 712 return tcflush(ctx->s, TCIOFLUSH);
713 713 #endif
714 714 }
715 715  
716   -int _modbus_rtu_listen(modbus_t *ctx, int nb_connection)
717   -{
718   - if (ctx->debug) {
719   - fprintf(stderr, "Not implemented");
720   - }
721   -
722   - if (ctx->slave == -1) {
723   - if (ctx->debug) {
724   - fprintf(stderr, "The slave ID is not set (you must call modbus_set_slave() first)\n");
725   - }
726   - errno = EINVAL;
727   - return -1;
728   - }
729   -
730   - errno = EINVAL;
731   - return -1;
732   -}
733   -
734   -int _modbus_rtu_accept(modbus_t *ctx, int *socket)
735   -{
736   - if (ctx->debug) {
737   - fprintf(stderr, "Not implemented");
738   - }
739   -
740   - errno = EINVAL;
741   - return -1;
742   -}
743   -
744 716 int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length)
745 717 {
746 718 int s_rc;
... ... @@ -835,8 +807,6 @@ const modbus_backend_t _modbus_rtu_backend = {
835 807 _modbus_rtu_connect,
836 808 _modbus_rtu_close,
837 809 _modbus_rtu_flush,
838   - _modbus_rtu_listen,
839   - _modbus_rtu_accept,
840 810 _modbus_rtu_select,
841 811 _modbus_rtu_filter_request
842 812 };
... ...
src/modbus-tcp.c
... ... @@ -284,7 +284,7 @@ int _modbus_tcp_flush(modbus_t *ctx)
284 284 }
285 285  
286 286 /* Listens for any request from one or many modbus masters in TCP */
287   -int _modbus_tcp_listen(modbus_t *ctx, int nb_connection)
  287 +int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
288 288 {
289 289 int new_socket;
290 290 int yes;
... ... @@ -330,7 +330,7 @@ int _modbus_tcp_listen(modbus_t *ctx, int nb_connection)
330 330 /* On success, the function return a non-negative integer that is a descriptor
331 331 for the accepted socket. On error, -1 is returned, and errno is set
332 332 appropriately. */
333   -int _modbus_tcp_accept(modbus_t *ctx, int *socket)
  333 +int modbus_tcp_accept(modbus_t *ctx, int *socket)
334 334 {
335 335 struct sockaddr_in addr;
336 336 socklen_t addrlen;
... ... @@ -414,8 +414,6 @@ const modbus_backend_t _modbus_tcp_backend = {
414 414 _modbus_tcp_connect,
415 415 _modbus_tcp_close,
416 416 _modbus_tcp_flush,
417   - _modbus_tcp_listen,
418   - _modbus_tcp_accept,
419 417 _modbus_tcp_select,
420 418 _modbus_tcp_filter_request
421 419 };
... ...
src/modbus-tcp.h
... ... @@ -38,5 +38,7 @@
38 38 #define MODBUS_TCP_MAX_ADU_LENGTH 260
39 39  
40 40 modbus_t* modbus_new_tcp(const char *ip_address, int port);
  41 +int modbus_tcp_listen(modbus_t *ctx, int nb_connection);
  42 +int modbus_tcp_accept(modbus_t *ctx, int *socket);
41 43  
42 44 #endif /* _MODBUS_TCP_H_ */
... ...
src/modbus.c
... ... @@ -1416,16 +1416,6 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping)
1416 1416 free(mb_mapping);
1417 1417 }
1418 1418  
1419   -int modbus_listen(modbus_t *ctx, int nb_connection)
1420   -{
1421   - return ctx->backend->listen(ctx, nb_connection);
1422   -}
1423   -
1424   -int modbus_accept(modbus_t *ctx, int *socket)
1425   -{
1426   - return ctx->backend->accept(ctx, socket);
1427   -}
1428   -
1429 1419 #ifndef HAVE_STRLCPY
1430 1420 /*
1431 1421 /* Function strlcpy was originally developed by
... ...
src/modbus.h
... ... @@ -168,8 +168,6 @@ modbus_mapping_t* modbus_mapping_new(int nb_coil_status, int nb_input_status,
168 168 int nb_holding_registers, int nb_input_registers);
169 169 void modbus_mapping_free(modbus_mapping_t *mb_mapping);
170 170  
171   -int modbus_listen(modbus_t *ctx, int nb_connection);
172   -int modbus_accept(modbus_t *ctx, int *socket);
173 171 int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req);
174 172 int modbus_reply(modbus_t *ctx, const uint8_t *req,
175 173 int req_length, modbus_mapping_t *mb_mapping);
... ...
tests/bandwidth-client.c
... ... @@ -25,9 +25,6 @@
25 25  
26 26 #include <modbus.h>
27 27  
28   -/* Tests based on PI-MBUS-300 documentation */
29   -#define NB_LOOPS 100000
30   -
31 28 #define G_MSEC_PER_SEC 1000
32 29  
33 30 uint32_t gettime_ms(void)
... ... @@ -38,7 +35,13 @@ uint32_t gettime_ms(void)
38 35 return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
39 36 }
40 37  
41   -int main(void)
  38 +enum {
  39 + TCP,
  40 + RTU
  41 +};
  42 +
  43 +/* Tests based on PI-MBUS-300 documentation */
  44 +int main(int argc, char *argv[])
42 45 {
43 46 uint8_t *tab_bit;
44 47 uint16_t *tab_reg;
... ... @@ -51,9 +54,32 @@ int main(void)
51 54 uint32_t bytes;
52 55 uint32_t rate;
53 56 int rc;
  57 + int n_loop;
  58 + int use_backend;
  59 +
  60 + if (argc > 1) {
  61 + if (strcmp(argv[1], "tcp") == 0) {
  62 + use_backend = TCP;
  63 + n_loop = 100000;
  64 + } else if (strcmp(argv[1], "rtu") == 0) {
  65 + use_backend = RTU;
  66 + n_loop = 100;
  67 + } else {
  68 + printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n");
  69 + exit(1);
  70 + }
  71 + } else {
  72 + /* By default */
  73 + use_backend = TCP;
  74 + n_loop = 100000;
  75 + }
54 76  
55   - /* TCP */
56   - ctx = modbus_new_tcp("127.0.0.1", 1502);
  77 + if (use_backend == TCP) {
  78 + ctx = modbus_new_tcp("127.0.0.1", 1502);
  79 + } else {
  80 + ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  81 + modbus_set_slave(ctx, 1);
  82 + }
57 83 if (modbus_connect(ctx) == -1) {
58 84 fprintf(stderr, "Connexion failed: %s\n",
59 85 modbus_strerror(errno));
... ... @@ -73,7 +99,7 @@ int main(void)
73 99  
74 100 nb_points = MODBUS_MAX_READ_BITS;
75 101 start = gettime_ms();
76   - for (i=0; i<NB_LOOPS; i++) {
  102 + for (i=0; i<n_loop; i++) {
77 103 rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
78 104 if (rc == -1) {
79 105 fprintf(stderr, "%s\n", modbus_strerror(errno));
... ... @@ -83,15 +109,15 @@ int main(void)
83 109 end = gettime_ms();
84 110 elapsed = end - start;
85 111  
86   - rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  112 + rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
87 113 printf("Transfert rate in points/seconds:\n");
88 114 printf("* %d points/s\n", rate);
89 115 printf("\n");
90 116  
91   - bytes = NB_LOOPS * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  117 + bytes = n_loop * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
92 118 rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
93 119 printf("Values:\n");
94   - printf("* %d x %d values\n", NB_LOOPS, nb_points);
  120 + printf("* %d x %d values\n", n_loop, nb_points);
95 121 printf("* %.3f ms for %d bytes\n", elapsed, bytes);
96 122 printf("* %d KiB/s\n", rate);
97 123 printf("\n");
... ... @@ -99,8 +125,8 @@ int main(void)
99 125 /* TCP: Query and reponse header and values */
100 126 bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
101 127 printf("Values and TCP Modbus overhead:\n");
102   - printf("* %d x %d bytes\n", NB_LOOPS, bytes);
103   - bytes = NB_LOOPS * bytes;
  128 + printf("* %d x %d bytes\n", n_loop, bytes);
  129 + bytes = n_loop * bytes;
104 130 rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
105 131 printf("* %.3f ms for %d bytes\n", elapsed, bytes);
106 132 printf("* %d KiB/s\n", rate);
... ... @@ -110,7 +136,7 @@ int main(void)
110 136  
111 137 nb_points = MODBUS_MAX_READ_REGISTERS;
112 138 start = gettime_ms();
113   - for (i=0; i<NB_LOOPS; i++) {
  139 + for (i=0; i<n_loop; i++) {
114 140 rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
115 141 if (rc == -1) {
116 142 fprintf(stderr, "%s\n", modbus_strerror(errno));
... ... @@ -120,15 +146,15 @@ int main(void)
120 146 end = gettime_ms();
121 147 elapsed = end - start;
122 148  
123   - rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  149 + rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
124 150 printf("Transfert rate in points/seconds:\n");
125 151 printf("* %d registers/s\n", rate);
126 152 printf("\n");
127 153  
128   - bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
  154 + bytes = n_loop * nb_points * sizeof(uint16_t);
129 155 rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
130 156 printf("Values:\n");
131   - printf("* %d x %d values\n", NB_LOOPS, nb_points);
  157 + printf("* %d x %d values\n", n_loop, nb_points);
132 158 printf("* %.3f ms for %d bytes\n", elapsed, bytes);
133 159 printf("* %d KiB/s\n", rate);
134 160 printf("\n");
... ... @@ -136,8 +162,8 @@ int main(void)
136 162 /* TCP:Query and reponse header and values */
137 163 bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
138 164 printf("Values and TCP Modbus overhead:\n");
139   - printf("* %d x %d bytes\n", NB_LOOPS, bytes);
140   - bytes = NB_LOOPS * bytes;
  165 + printf("* %d x %d bytes\n", n_loop, bytes);
  166 + bytes = n_loop * bytes;
141 167 rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
142 168 printf("* %.3f ms for %d bytes\n", elapsed, bytes);
143 169 printf("* %d KiB/s\n", rate);
... ... @@ -147,7 +173,7 @@ int main(void)
147 173  
148 174 nb_points = MODBUS_MAX_RW_WRITE_REGISTERS;
149 175 start = gettime_ms();
150   - for (i=0; i<NB_LOOPS; i++) {
  176 + for (i=0; i<n_loop; i++) {
151 177 rc = modbus_read_and_write_registers(ctx,
152 178 0, nb_points, tab_reg,
153 179 0, nb_points, tab_reg);
... ... @@ -159,15 +185,15 @@ int main(void)
159 185 end = gettime_ms();
160 186 elapsed = end - start;
161 187  
162   - rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  188 + rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
163 189 printf("Transfert rate in points/seconds:\n");
164 190 printf("* %d registers/s\n", rate);
165 191 printf("\n");
166 192  
167   - bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
  193 + bytes = n_loop * nb_points * sizeof(uint16_t);
168 194 rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
169 195 printf("Values:\n");
170   - printf("* %d x %d values\n", NB_LOOPS, nb_points);
  196 + printf("* %d x %d values\n", n_loop, nb_points);
171 197 printf("* %.3f ms for %d bytes\n", elapsed, bytes);
172 198 printf("* %d KiB/s\n", rate);
173 199 printf("\n");
... ... @@ -175,8 +201,8 @@ int main(void)
175 201 /* TCP:Query and reponse header and values */
176 202 bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
177 203 printf("Values and TCP Modbus overhead:\n");
178   - printf("* %d x %d bytes\n", NB_LOOPS, bytes);
179   - bytes = NB_LOOPS * bytes;
  204 + printf("* %d x %d bytes\n", n_loop, bytes);
  205 + bytes = n_loop * bytes;
180 206 rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
181 207 printf("* %.3f ms for %d bytes\n", elapsed, bytes);
182 208 printf("* %d KiB/s\n", rate);
... ...
tests/bandwidth-server-many-up.c
... ... @@ -66,7 +66,7 @@ int main(void)
66 66 return -1;
67 67 }
68 68  
69   - server_socket = modbus_listen(ctx, NB_CONNECTION);
  69 + server_socket = modbus_tcp_listen(ctx, NB_CONNECTION);
70 70  
71 71 signal(SIGINT, close_sigint);
72 72  
... ...
tests/bandwidth-server-one.c
... ... @@ -23,14 +23,44 @@
23 23  
24 24 #include <modbus.h>
25 25  
26   -int main(void)
  26 +enum {
  27 + TCP,
  28 + RTU
  29 +};
  30 +
  31 +int main(int argc, char *argv[])
27 32 {
28 33 int socket;
29 34 modbus_t *ctx;
30 35 modbus_mapping_t *mb_mapping;
31 36 int rc;
  37 + int use_backend;
  38 +
  39 + /* TCP */
  40 + if (argc > 1) {
  41 + if (strcmp(argv[1], "tcp") == 0) {
  42 + use_backend = TCP;
  43 + } else if (strcmp(argv[1], "rtu") == 0) {
  44 + use_backend = RTU;
  45 + } else {
  46 + printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n");
  47 + exit(1);
  48 + }
  49 + } else {
  50 + /* By default */
  51 + use_backend = TCP;
  52 + }
  53 +
  54 + if (use_backend == TCP) {
  55 + ctx = modbus_new_tcp("127.0.0.1", 1502);
  56 + socket = modbus_tcp_listen(ctx, 1);
  57 + modbus_tcp_accept(ctx, &socket);
32 58  
33   - ctx = modbus_new_tcp("127.0.0.1", 1502);
  59 + } else {
  60 + ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  61 + modbus_set_slave(ctx, 1);
  62 + modbus_connect(ctx);
  63 + }
34 64  
35 65 mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
36 66 MODBUS_MAX_READ_REGISTERS, 0);
... ... @@ -41,9 +71,6 @@ int main(void)
41 71 return -1;
42 72 }
43 73  
44   - socket = modbus_listen(ctx, 1);
45   - modbus_accept(ctx, &socket);
46   -
47 74 for(;;) {
48 75 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
49 76  
... ...
tests/random-test-server.c
... ... @@ -39,8 +39,8 @@ int main(void)
39 39 return -1;
40 40 }
41 41  
42   - socket = modbus_listen(ctx, 1);
43   - modbus_accept(ctx, &socket);
  42 + socket = modbus_tcp_listen(ctx, 1);
  43 + modbus_tcp_accept(ctx, &socket);
44 44  
45 45 for (;;) {
46 46 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
... ...
tests/unit-test-client.c
... ... @@ -24,13 +24,17 @@
24 24  
25 25 #include "unit-test.h"
26 26  
27   -int main(void)
  27 +enum {
  28 + TCP,
  29 + RTU
  30 +};
  31 +
  32 +int main(int argc, char *argv[])
28 33 {
29 34 uint8_t *tab_rp_bits;
30 35 uint16_t *tab_rp_registers;
31 36 uint16_t *tab_rp_registers_bad;
32 37 modbus_t *ctx;
33   - int is_mode_rtu = FALSE;
34 38 int i;
35 39 uint8_t value;
36 40 int address;
... ... @@ -39,21 +43,37 @@ int main(void)
39 43 float real;
40 44 struct timeval timeout_begin_old;
41 45 struct timeval timeout_begin_new;
  46 + int use_backend;
42 47  
43   - /*
44   - ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
45   - modbus_set_slave(ctx, SERVER_ID);
46   - is_mode_rtu = TRUE;
47   - */
  48 + if (argc > 1) {
  49 + if (strcmp(argv[1], "tcp") == 0) {
  50 + use_backend = TCP;
  51 + } else if (strcmp(argv[1], "rtu") == 0) {
  52 + use_backend = RTU;
  53 + } else {
  54 + printf("Usage:\n %s [tcp|rtu] - Modbus client for unit testing\n\n");
  55 + exit(1);
  56 + }
  57 + } else {
  58 + /* By default */
  59 + use_backend = TCP;
  60 + }
48 61  
49   - /* TCP */
50   - ctx = modbus_new_tcp("127.0.0.1", 1502);
  62 + if (use_backend == TCP) {
  63 + ctx = modbus_new_tcp("127.0.0.1", 1502);
  64 + } else {
  65 + ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  66 + }
51 67 if (ctx == NULL) {
52   - fprintf(stderr, "Unable to initialize TCP Modbus\n");
  68 + fprintf(stderr, "Unable to allocate libmodbus context\n");
53 69 return -1;
54 70 }
55 71 modbus_set_debug(ctx, TRUE);
56 72  
  73 + if (use_backend == RTU) {
  74 + modbus_set_slave(ctx, SERVER_ID);
  75 + }
  76 +
57 77 if (modbus_connect(ctx) == -1) {
58 78 fprintf(stderr, "Connection failed: %s\n",
59 79 modbus_strerror(errno));
... ... @@ -496,9 +516,10 @@ int main(void)
496 516 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
497 517 UT_REGISTERS_NB_POINTS,
498 518 tab_rp_registers);
499   - printf("1/4 No or response from slave %d: ", 18);
500   - if (is_mode_rtu) {
  519 + if (use_backend == RTU) {
501 520 /* No response in RTU mode */
  521 + printf("1/4 No response from slave %d: ", 18);
  522 +
502 523 if (rc == -1 && errno == ETIMEDOUT) {
503 524 printf("OK\n");
504 525 } else {
... ... @@ -507,6 +528,8 @@ int main(void)
507 528 }
508 529 } else {
509 530 /* Response in TCP mode */
  531 + printf("1/4 Response from slave %d: ", 18);
  532 +
510 533 if (rc == UT_REGISTERS_NB_POINTS) {
511 534 printf("OK\n");
512 535 } else {
... ... @@ -515,7 +538,12 @@ int main(void)
515 538 }
516 539 }
517 540  
518   - modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  541 + rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS);
  542 + if (rc == -1) {
  543 + printf("Invalid broacast address\n");
  544 + goto close;
  545 + }
  546 +
519 547 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
520 548 UT_REGISTERS_NB_POINTS,
521 549 tab_rp_registers);
... ... @@ -528,7 +556,7 @@ int main(void)
528 556 }
529 557  
530 558 /* Restore slave */
531   - if (is_mode_rtu) {
  559 + if (use_backend == RTU) {
532 560 modbus_set_slave(ctx, SERVER_ID);
533 561 } else {
534 562 modbus_set_slave(ctx, MODBUS_TCP_SLAVE);
... ... @@ -542,7 +570,7 @@ int main(void)
542 570 goto close;
543 571 }
544 572  
545   - if ((is_mode_rtu && tab_rp_bits[0] == SERVER_ID)
  573 + if (((use_backend == RTU) && (tab_rp_bits[0] == SERVER_ID))
546 574 || tab_rp_bits[0] == 0xFF) {
547 575 printf("OK\n");
548 576 } else {
... ...
tests/unit-test-server.c
... ... @@ -27,15 +27,41 @@
27 27 /* Copied from modbus-private.h */
28 28 #define HEADER_LENGTH_TCP 7
29 29  
30   -int main(void)
  30 +enum {
  31 + TCP,
  32 + RTU
  33 +};
  34 +
  35 +int main(int argc, char*argv[])
31 36 {
32 37 int socket;
33 38 modbus_t *ctx;
34 39 modbus_mapping_t *mb_mapping;
35 40 int rc;
36 41 int i;
  42 + int use_backend;
  43 +
  44 + if (argc > 1) {
  45 + if (strcmp(argv[1], "tcp") == 0) {
  46 + use_backend = TCP;
  47 + } else if (strcmp(argv[1], "rtu") == 0) {
  48 + use_backend = RTU;
  49 + } else {
  50 + printf("Usage:\n %s [tcp|rtu] - Modbus server for unit testing\n\n");
  51 + return -1;
  52 + }
  53 + } else {
  54 + /* By default */
  55 + use_backend = TCP;
  56 + }
  57 +
  58 + if (use_backend == TCP) {
  59 + ctx = modbus_new_tcp("127.0.0.1", 1502);
  60 + } else {
  61 + ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  62 + modbus_set_slave(ctx, SERVER_ID);
  63 + }
37 64  
38   - ctx = modbus_new_tcp("127.0.0.1", 1502);
39 65 modbus_set_debug(ctx, TRUE);
40 66 modbus_set_error_recovery(ctx, TRUE);
41 67  
... ... @@ -65,8 +91,17 @@ int main(void)
65 91 UT_INPUT_REGISTERS_TAB[i];;
66 92 }
67 93  
68   - socket = modbus_listen(ctx, 1);
69   - modbus_accept(ctx, &socket);
  94 + if (use_backend == TCP) {
  95 + socket = modbus_tcp_listen(ctx, 1);
  96 + modbus_tcp_accept(ctx, &socket);
  97 + } else {
  98 + rc = modbus_connect(ctx);
  99 + if (rc == -1) {
  100 + fprintf(stderr, "Unable to connect\n", modbus_strerror(errno));
  101 + modbus_free(ctx);
  102 + return -1;
  103 + }
  104 + }
70 105  
71 106 for (;;) {
72 107 uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
... ... @@ -94,7 +129,9 @@ int main(void)
94 129  
95 130 printf("Quit the loop: %s\n", modbus_strerror(errno));
96 131  
97   - close(socket);
  132 + if (use_backend == TCP) {
  133 + close(socket);
  134 + }
98 135 modbus_mapping_free(mb_mapping);
99 136 modbus_free(ctx);
100 137  
... ...