Commit b8c0558c192cd349124aff5faac8a3f1f0e32362
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)
Showing
12 changed files
with
175 additions
and
101 deletions
src/modbus-private.h
| @@ -87,8 +87,6 @@ typedef struct _modbus_backend { | @@ -87,8 +87,6 @@ typedef struct _modbus_backend { | ||
| 87 | int (*connect) (modbus_t *ctx); | 87 | int (*connect) (modbus_t *ctx); |
| 88 | void (*close) (modbus_t *ctx); | 88 | void (*close) (modbus_t *ctx); |
| 89 | int (*flush) (modbus_t *ctx); | 89 | int (*flush) (modbus_t *ctx); |
| 90 | - int (*listen) (modbus_t *ctx, int nb_connection); | ||
| 91 | - int (*accept) (modbus_t *ctx, int *socket); | ||
| 92 | int (*select) (modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length); | 90 | int (*select) (modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length); |
| 93 | int (*filter_request) (modbus_t *ctx, int slave); | 91 | int (*filter_request) (modbus_t *ctx, int slave); |
| 94 | } modbus_backend_t; | 92 | } modbus_backend_t; |
src/modbus-rtu.c
| @@ -707,40 +707,12 @@ int _modbus_rtu_flush(modbus_t *ctx) | @@ -707,40 +707,12 @@ int _modbus_rtu_flush(modbus_t *ctx) | ||
| 707 | #if defined(_WIN32) | 707 | #if defined(_WIN32) |
| 708 | modbus_rtu_t *ctx_rtu = ctx->backend_data; | 708 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 709 | ctx_rtu->w_ser.n_bytes = 0; | 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 | #else | 711 | #else |
| 712 | return tcflush(ctx->s, TCIOFLUSH); | 712 | return tcflush(ctx->s, TCIOFLUSH); |
| 713 | #endif | 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 | int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length) | 716 | int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length) |
| 745 | { | 717 | { |
| 746 | int s_rc; | 718 | int s_rc; |
| @@ -835,8 +807,6 @@ const modbus_backend_t _modbus_rtu_backend = { | @@ -835,8 +807,6 @@ const modbus_backend_t _modbus_rtu_backend = { | ||
| 835 | _modbus_rtu_connect, | 807 | _modbus_rtu_connect, |
| 836 | _modbus_rtu_close, | 808 | _modbus_rtu_close, |
| 837 | _modbus_rtu_flush, | 809 | _modbus_rtu_flush, |
| 838 | - _modbus_rtu_listen, | ||
| 839 | - _modbus_rtu_accept, | ||
| 840 | _modbus_rtu_select, | 810 | _modbus_rtu_select, |
| 841 | _modbus_rtu_filter_request | 811 | _modbus_rtu_filter_request |
| 842 | }; | 812 | }; |
src/modbus-tcp.c
| @@ -284,7 +284,7 @@ int _modbus_tcp_flush(modbus_t *ctx) | @@ -284,7 +284,7 @@ int _modbus_tcp_flush(modbus_t *ctx) | ||
| 284 | } | 284 | } |
| 285 | 285 | ||
| 286 | /* Listens for any request from one or many modbus masters in TCP */ | 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 | int new_socket; | 289 | int new_socket; |
| 290 | int yes; | 290 | int yes; |
| @@ -330,7 +330,7 @@ int _modbus_tcp_listen(modbus_t *ctx, int nb_connection) | @@ -330,7 +330,7 @@ int _modbus_tcp_listen(modbus_t *ctx, int nb_connection) | ||
| 330 | /* On success, the function return a non-negative integer that is a descriptor | 330 | /* On success, the function return a non-negative integer that is a descriptor |
| 331 | for the accepted socket. On error, -1 is returned, and errno is set | 331 | for the accepted socket. On error, -1 is returned, and errno is set |
| 332 | appropriately. */ | 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 | struct sockaddr_in addr; | 335 | struct sockaddr_in addr; |
| 336 | socklen_t addrlen; | 336 | socklen_t addrlen; |
| @@ -414,8 +414,6 @@ const modbus_backend_t _modbus_tcp_backend = { | @@ -414,8 +414,6 @@ const modbus_backend_t _modbus_tcp_backend = { | ||
| 414 | _modbus_tcp_connect, | 414 | _modbus_tcp_connect, |
| 415 | _modbus_tcp_close, | 415 | _modbus_tcp_close, |
| 416 | _modbus_tcp_flush, | 416 | _modbus_tcp_flush, |
| 417 | - _modbus_tcp_listen, | ||
| 418 | - _modbus_tcp_accept, | ||
| 419 | _modbus_tcp_select, | 417 | _modbus_tcp_select, |
| 420 | _modbus_tcp_filter_request | 418 | _modbus_tcp_filter_request |
| 421 | }; | 419 | }; |
src/modbus-tcp.h
| @@ -38,5 +38,7 @@ | @@ -38,5 +38,7 @@ | ||
| 38 | #define MODBUS_TCP_MAX_ADU_LENGTH 260 | 38 | #define MODBUS_TCP_MAX_ADU_LENGTH 260 |
| 39 | 39 | ||
| 40 | modbus_t* modbus_new_tcp(const char *ip_address, int port); | 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 | #endif /* _MODBUS_TCP_H_ */ | 44 | #endif /* _MODBUS_TCP_H_ */ |
src/modbus.c
| @@ -1416,16 +1416,6 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping) | @@ -1416,16 +1416,6 @@ void modbus_mapping_free(modbus_mapping_t *mb_mapping) | ||
| 1416 | free(mb_mapping); | 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 | #ifndef HAVE_STRLCPY | 1419 | #ifndef HAVE_STRLCPY |
| 1430 | /* | 1420 | /* |
| 1431 | /* Function strlcpy was originally developed by | 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,8 +168,6 @@ modbus_mapping_t* modbus_mapping_new(int nb_coil_status, int nb_input_status, | ||
| 168 | int nb_holding_registers, int nb_input_registers); | 168 | int nb_holding_registers, int nb_input_registers); |
| 169 | void modbus_mapping_free(modbus_mapping_t *mb_mapping); | 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 | int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req); | 171 | int modbus_receive(modbus_t *ctx, int sockfd, uint8_t *req); |
| 174 | int modbus_reply(modbus_t *ctx, const uint8_t *req, | 172 | int modbus_reply(modbus_t *ctx, const uint8_t *req, |
| 175 | int req_length, modbus_mapping_t *mb_mapping); | 173 | int req_length, modbus_mapping_t *mb_mapping); |
tests/bandwidth-client.c
| @@ -25,9 +25,6 @@ | @@ -25,9 +25,6 @@ | ||
| 25 | 25 | ||
| 26 | #include <modbus.h> | 26 | #include <modbus.h> |
| 27 | 27 | ||
| 28 | -/* Tests based on PI-MBUS-300 documentation */ | ||
| 29 | -#define NB_LOOPS 100000 | ||
| 30 | - | ||
| 31 | #define G_MSEC_PER_SEC 1000 | 28 | #define G_MSEC_PER_SEC 1000 |
| 32 | 29 | ||
| 33 | uint32_t gettime_ms(void) | 30 | uint32_t gettime_ms(void) |
| @@ -38,7 +35,13 @@ uint32_t gettime_ms(void) | @@ -38,7 +35,13 @@ uint32_t gettime_ms(void) | ||
| 38 | return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000; | 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 | uint8_t *tab_bit; | 46 | uint8_t *tab_bit; |
| 44 | uint16_t *tab_reg; | 47 | uint16_t *tab_reg; |
| @@ -51,9 +54,32 @@ int main(void) | @@ -51,9 +54,32 @@ int main(void) | ||
| 51 | uint32_t bytes; | 54 | uint32_t bytes; |
| 52 | uint32_t rate; | 55 | uint32_t rate; |
| 53 | int rc; | 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 | if (modbus_connect(ctx) == -1) { | 83 | if (modbus_connect(ctx) == -1) { |
| 58 | fprintf(stderr, "Connexion failed: %s\n", | 84 | fprintf(stderr, "Connexion failed: %s\n", |
| 59 | modbus_strerror(errno)); | 85 | modbus_strerror(errno)); |
| @@ -73,7 +99,7 @@ int main(void) | @@ -73,7 +99,7 @@ int main(void) | ||
| 73 | 99 | ||
| 74 | nb_points = MODBUS_MAX_READ_BITS; | 100 | nb_points = MODBUS_MAX_READ_BITS; |
| 75 | start = gettime_ms(); | 101 | start = gettime_ms(); |
| 76 | - for (i=0; i<NB_LOOPS; i++) { | 102 | + for (i=0; i<n_loop; i++) { |
| 77 | rc = modbus_read_bits(ctx, 0, nb_points, tab_bit); | 103 | rc = modbus_read_bits(ctx, 0, nb_points, tab_bit); |
| 78 | if (rc == -1) { | 104 | if (rc == -1) { |
| 79 | fprintf(stderr, "%s\n", modbus_strerror(errno)); | 105 | fprintf(stderr, "%s\n", modbus_strerror(errno)); |
| @@ -83,15 +109,15 @@ int main(void) | @@ -83,15 +109,15 @@ int main(void) | ||
| 83 | end = gettime_ms(); | 109 | end = gettime_ms(); |
| 84 | elapsed = end - start; | 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 | printf("Transfert rate in points/seconds:\n"); | 113 | printf("Transfert rate in points/seconds:\n"); |
| 88 | printf("* %d points/s\n", rate); | 114 | printf("* %d points/s\n", rate); |
| 89 | printf("\n"); | 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 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 118 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 93 | printf("Values:\n"); | 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 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 121 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 96 | printf("* %d KiB/s\n", rate); | 122 | printf("* %d KiB/s\n", rate); |
| 97 | printf("\n"); | 123 | printf("\n"); |
| @@ -99,8 +125,8 @@ int main(void) | @@ -99,8 +125,8 @@ int main(void) | ||
| 99 | /* TCP: Query and reponse header and values */ | 125 | /* TCP: Query and reponse header and values */ |
| 100 | bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0); | 126 | bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0); |
| 101 | printf("Values and TCP Modbus overhead:\n"); | 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 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 130 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 105 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 131 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 106 | printf("* %d KiB/s\n", rate); | 132 | printf("* %d KiB/s\n", rate); |
| @@ -110,7 +136,7 @@ int main(void) | @@ -110,7 +136,7 @@ int main(void) | ||
| 110 | 136 | ||
| 111 | nb_points = MODBUS_MAX_READ_REGISTERS; | 137 | nb_points = MODBUS_MAX_READ_REGISTERS; |
| 112 | start = gettime_ms(); | 138 | start = gettime_ms(); |
| 113 | - for (i=0; i<NB_LOOPS; i++) { | 139 | + for (i=0; i<n_loop; i++) { |
| 114 | rc = modbus_read_registers(ctx, 0, nb_points, tab_reg); | 140 | rc = modbus_read_registers(ctx, 0, nb_points, tab_reg); |
| 115 | if (rc == -1) { | 141 | if (rc == -1) { |
| 116 | fprintf(stderr, "%s\n", modbus_strerror(errno)); | 142 | fprintf(stderr, "%s\n", modbus_strerror(errno)); |
| @@ -120,15 +146,15 @@ int main(void) | @@ -120,15 +146,15 @@ int main(void) | ||
| 120 | end = gettime_ms(); | 146 | end = gettime_ms(); |
| 121 | elapsed = end - start; | 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 | printf("Transfert rate in points/seconds:\n"); | 150 | printf("Transfert rate in points/seconds:\n"); |
| 125 | printf("* %d registers/s\n", rate); | 151 | printf("* %d registers/s\n", rate); |
| 126 | printf("\n"); | 152 | printf("\n"); |
| 127 | 153 | ||
| 128 | - bytes = NB_LOOPS * nb_points * sizeof(uint16_t); | 154 | + bytes = n_loop * nb_points * sizeof(uint16_t); |
| 129 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 155 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 130 | printf("Values:\n"); | 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 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 158 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 133 | printf("* %d KiB/s\n", rate); | 159 | printf("* %d KiB/s\n", rate); |
| 134 | printf("\n"); | 160 | printf("\n"); |
| @@ -136,8 +162,8 @@ int main(void) | @@ -136,8 +162,8 @@ int main(void) | ||
| 136 | /* TCP:Query and reponse header and values */ | 162 | /* TCP:Query and reponse header and values */ |
| 137 | bytes = 12 + 9 + (nb_points * sizeof(uint16_t)); | 163 | bytes = 12 + 9 + (nb_points * sizeof(uint16_t)); |
| 138 | printf("Values and TCP Modbus overhead:\n"); | 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 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 167 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 142 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 168 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 143 | printf("* %d KiB/s\n", rate); | 169 | printf("* %d KiB/s\n", rate); |
| @@ -147,7 +173,7 @@ int main(void) | @@ -147,7 +173,7 @@ int main(void) | ||
| 147 | 173 | ||
| 148 | nb_points = MODBUS_MAX_RW_WRITE_REGISTERS; | 174 | nb_points = MODBUS_MAX_RW_WRITE_REGISTERS; |
| 149 | start = gettime_ms(); | 175 | start = gettime_ms(); |
| 150 | - for (i=0; i<NB_LOOPS; i++) { | 176 | + for (i=0; i<n_loop; i++) { |
| 151 | rc = modbus_read_and_write_registers(ctx, | 177 | rc = modbus_read_and_write_registers(ctx, |
| 152 | 0, nb_points, tab_reg, | 178 | 0, nb_points, tab_reg, |
| 153 | 0, nb_points, tab_reg); | 179 | 0, nb_points, tab_reg); |
| @@ -159,15 +185,15 @@ int main(void) | @@ -159,15 +185,15 @@ int main(void) | ||
| 159 | end = gettime_ms(); | 185 | end = gettime_ms(); |
| 160 | elapsed = end - start; | 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 | printf("Transfert rate in points/seconds:\n"); | 189 | printf("Transfert rate in points/seconds:\n"); |
| 164 | printf("* %d registers/s\n", rate); | 190 | printf("* %d registers/s\n", rate); |
| 165 | printf("\n"); | 191 | printf("\n"); |
| 166 | 192 | ||
| 167 | - bytes = NB_LOOPS * nb_points * sizeof(uint16_t); | 193 | + bytes = n_loop * nb_points * sizeof(uint16_t); |
| 168 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 194 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 169 | printf("Values:\n"); | 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 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 197 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 172 | printf("* %d KiB/s\n", rate); | 198 | printf("* %d KiB/s\n", rate); |
| 173 | printf("\n"); | 199 | printf("\n"); |
| @@ -175,8 +201,8 @@ int main(void) | @@ -175,8 +201,8 @@ int main(void) | ||
| 175 | /* TCP:Query and reponse header and values */ | 201 | /* TCP:Query and reponse header and values */ |
| 176 | bytes = 12 + 9 + (nb_points * sizeof(uint16_t)); | 202 | bytes = 12 + 9 + (nb_points * sizeof(uint16_t)); |
| 177 | printf("Values and TCP Modbus overhead:\n"); | 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 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); | 206 | rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 181 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); | 207 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 182 | printf("* %d KiB/s\n", rate); | 208 | printf("* %d KiB/s\n", rate); |
tests/bandwidth-server-many-up.c
| @@ -66,7 +66,7 @@ int main(void) | @@ -66,7 +66,7 @@ int main(void) | ||
| 66 | return -1; | 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 | signal(SIGINT, close_sigint); | 71 | signal(SIGINT, close_sigint); |
| 72 | 72 |
tests/bandwidth-server-one.c
| @@ -23,14 +23,44 @@ | @@ -23,14 +23,44 @@ | ||
| 23 | 23 | ||
| 24 | #include <modbus.h> | 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 | int socket; | 33 | int socket; |
| 29 | modbus_t *ctx; | 34 | modbus_t *ctx; |
| 30 | modbus_mapping_t *mb_mapping; | 35 | modbus_mapping_t *mb_mapping; |
| 31 | int rc; | 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 | mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, | 65 | mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, |
| 36 | MODBUS_MAX_READ_REGISTERS, 0); | 66 | MODBUS_MAX_READ_REGISTERS, 0); |
| @@ -41,9 +71,6 @@ int main(void) | @@ -41,9 +71,6 @@ int main(void) | ||
| 41 | return -1; | 71 | return -1; |
| 42 | } | 72 | } |
| 43 | 73 | ||
| 44 | - socket = modbus_listen(ctx, 1); | ||
| 45 | - modbus_accept(ctx, &socket); | ||
| 46 | - | ||
| 47 | for(;;) { | 74 | for(;;) { |
| 48 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 75 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 49 | 76 |
tests/random-test-server.c
| @@ -39,8 +39,8 @@ int main(void) | @@ -39,8 +39,8 @@ int main(void) | ||
| 39 | return -1; | 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 | for (;;) { | 45 | for (;;) { |
| 46 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 46 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
tests/unit-test-client.c
| @@ -24,13 +24,17 @@ | @@ -24,13 +24,17 @@ | ||
| 24 | 24 | ||
| 25 | #include "unit-test.h" | 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 | uint8_t *tab_rp_bits; | 34 | uint8_t *tab_rp_bits; |
| 30 | uint16_t *tab_rp_registers; | 35 | uint16_t *tab_rp_registers; |
| 31 | uint16_t *tab_rp_registers_bad; | 36 | uint16_t *tab_rp_registers_bad; |
| 32 | modbus_t *ctx; | 37 | modbus_t *ctx; |
| 33 | - int is_mode_rtu = FALSE; | ||
| 34 | int i; | 38 | int i; |
| 35 | uint8_t value; | 39 | uint8_t value; |
| 36 | int address; | 40 | int address; |
| @@ -39,21 +43,37 @@ int main(void) | @@ -39,21 +43,37 @@ int main(void) | ||
| 39 | float real; | 43 | float real; |
| 40 | struct timeval timeout_begin_old; | 44 | struct timeval timeout_begin_old; |
| 41 | struct timeval timeout_begin_new; | 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 | if (ctx == NULL) { | 67 | if (ctx == NULL) { |
| 52 | - fprintf(stderr, "Unable to initialize TCP Modbus\n"); | 68 | + fprintf(stderr, "Unable to allocate libmodbus context\n"); |
| 53 | return -1; | 69 | return -1; |
| 54 | } | 70 | } |
| 55 | modbus_set_debug(ctx, TRUE); | 71 | modbus_set_debug(ctx, TRUE); |
| 56 | 72 | ||
| 73 | + if (use_backend == RTU) { | ||
| 74 | + modbus_set_slave(ctx, SERVER_ID); | ||
| 75 | + } | ||
| 76 | + | ||
| 57 | if (modbus_connect(ctx) == -1) { | 77 | if (modbus_connect(ctx) == -1) { |
| 58 | fprintf(stderr, "Connection failed: %s\n", | 78 | fprintf(stderr, "Connection failed: %s\n", |
| 59 | modbus_strerror(errno)); | 79 | modbus_strerror(errno)); |
| @@ -496,9 +516,10 @@ int main(void) | @@ -496,9 +516,10 @@ int main(void) | ||
| 496 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 516 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 497 | UT_REGISTERS_NB_POINTS, | 517 | UT_REGISTERS_NB_POINTS, |
| 498 | tab_rp_registers); | 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 | /* No response in RTU mode */ | 520 | /* No response in RTU mode */ |
| 521 | + printf("1/4 No response from slave %d: ", 18); | ||
| 522 | + | ||
| 502 | if (rc == -1 && errno == ETIMEDOUT) { | 523 | if (rc == -1 && errno == ETIMEDOUT) { |
| 503 | printf("OK\n"); | 524 | printf("OK\n"); |
| 504 | } else { | 525 | } else { |
| @@ -507,6 +528,8 @@ int main(void) | @@ -507,6 +528,8 @@ int main(void) | ||
| 507 | } | 528 | } |
| 508 | } else { | 529 | } else { |
| 509 | /* Response in TCP mode */ | 530 | /* Response in TCP mode */ |
| 531 | + printf("1/4 Response from slave %d: ", 18); | ||
| 532 | + | ||
| 510 | if (rc == UT_REGISTERS_NB_POINTS) { | 533 | if (rc == UT_REGISTERS_NB_POINTS) { |
| 511 | printf("OK\n"); | 534 | printf("OK\n"); |
| 512 | } else { | 535 | } else { |
| @@ -515,7 +538,12 @@ int main(void) | @@ -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 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 547 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 520 | UT_REGISTERS_NB_POINTS, | 548 | UT_REGISTERS_NB_POINTS, |
| 521 | tab_rp_registers); | 549 | tab_rp_registers); |
| @@ -528,7 +556,7 @@ int main(void) | @@ -528,7 +556,7 @@ int main(void) | ||
| 528 | } | 556 | } |
| 529 | 557 | ||
| 530 | /* Restore slave */ | 558 | /* Restore slave */ |
| 531 | - if (is_mode_rtu) { | 559 | + if (use_backend == RTU) { |
| 532 | modbus_set_slave(ctx, SERVER_ID); | 560 | modbus_set_slave(ctx, SERVER_ID); |
| 533 | } else { | 561 | } else { |
| 534 | modbus_set_slave(ctx, MODBUS_TCP_SLAVE); | 562 | modbus_set_slave(ctx, MODBUS_TCP_SLAVE); |
| @@ -542,7 +570,7 @@ int main(void) | @@ -542,7 +570,7 @@ int main(void) | ||
| 542 | goto close; | 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 | || tab_rp_bits[0] == 0xFF) { | 574 | || tab_rp_bits[0] == 0xFF) { |
| 547 | printf("OK\n"); | 575 | printf("OK\n"); |
| 548 | } else { | 576 | } else { |
tests/unit-test-server.c
| @@ -27,15 +27,41 @@ | @@ -27,15 +27,41 @@ | ||
| 27 | /* Copied from modbus-private.h */ | 27 | /* Copied from modbus-private.h */ |
| 28 | #define HEADER_LENGTH_TCP 7 | 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 | int socket; | 37 | int socket; |
| 33 | modbus_t *ctx; | 38 | modbus_t *ctx; |
| 34 | modbus_mapping_t *mb_mapping; | 39 | modbus_mapping_t *mb_mapping; |
| 35 | int rc; | 40 | int rc; |
| 36 | int i; | 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 | modbus_set_debug(ctx, TRUE); | 65 | modbus_set_debug(ctx, TRUE); |
| 40 | modbus_set_error_recovery(ctx, TRUE); | 66 | modbus_set_error_recovery(ctx, TRUE); |
| 41 | 67 | ||
| @@ -65,8 +91,17 @@ int main(void) | @@ -65,8 +91,17 @@ int main(void) | ||
| 65 | UT_INPUT_REGISTERS_TAB[i];; | 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 | for (;;) { | 106 | for (;;) { |
| 72 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 107 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| @@ -94,7 +129,9 @@ int main(void) | @@ -94,7 +129,9 @@ int main(void) | ||
| 94 | 129 | ||
| 95 | printf("Quit the loop: %s\n", modbus_strerror(errno)); | 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 | modbus_mapping_free(mb_mapping); | 135 | modbus_mapping_free(mb_mapping); |
| 99 | modbus_free(ctx); | 136 | modbus_free(ctx); |
| 100 | 137 |