| 1 | /* |
| 2 | * Copyright © Stéphane Raimbault <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #ifndef _MSC_VER |
| 9 | #include <sys/time.h> |
| 10 | #include <unistd.h> |
| 11 | #endif |
| 12 | #include <errno.h> |
| 13 | #include <inttypes.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <time.h> |
| 17 | |
| 18 | #include <modbus.h> |
| 19 | |
| 20 | #define G_MSEC_PER_SEC 1000 |
| 21 | |
| 22 | static uint32_t gettime_ms(void) |
| 23 | { |
| 24 | struct timeval tv; |
| 25 | #if !defined(_MSC_VER) |
| 26 | gettimeofday(&tv, NULL); |
| 27 | return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 28 | #else |
| 29 | return GetTickCount(); |
| 30 | #endif |
| 31 | } |
| 32 | |
| 33 | enum { |
| 34 | TCP, |
| 35 | RTU |
| 36 | }; |
| 37 | |
| 38 | /* Tests based on PI-MBUS-300 documentation */ |
| 39 | int main(int argc, char *argv[]) |
| 40 | { |
| 41 | uint8_t *tab_bit; |
| 42 | uint16_t *tab_reg; |
| 43 | modbus_t *ctx; |
| 44 | int i; |
| 45 | int nb_points; |
| 46 | double elapsed; |
| 47 | uint32_t start; |
| 48 | uint32_t end; |
| 49 | uint32_t bytes; |
| 50 | uint64_t rate; |
| 51 | int rc; |
| 52 | int n_loop; |
| 53 | int use_backend; |
| 54 | |
| 55 | if (argc > 1) { |
| 56 | if (strcmp(argv[1], "tcp") == 0) { |
| 57 | use_backend = TCP; |
| 58 | n_loop = 100000; |
| 59 | } else if (strcmp(argv[1], "rtu") == 0) { |
| 60 | use_backend = RTU; |
| 61 | n_loop = 100; |
| 62 | } else { |
| 63 | printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwidth\n\n", |
| 64 | argv[0]); |
| 65 | exit(1); |
| 66 | } |
| 67 | } else { |
| 68 | /* By default */ |
| 69 | use_backend = TCP; |
| 70 | n_loop = 100000; |
| 71 | } |
| 72 | |
| 73 | if (use_backend == TCP) { |
| 74 | ctx = modbus_new_tcp("127.0.0.1", 1502); |
| 75 | } else { |
| 76 | ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1); |
| 77 | modbus_set_slave(ctx, 1); |
| 78 | } |
| 79 | if (modbus_connect(ctx) == -1) { |
| 80 | fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno)); |
| 81 | modbus_free(ctx); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | /* Allocate and initialize the memory to store the status */ |
| 86 | tab_bit = (uint8_t *) malloc(MODBUS_MAX_READ_BITS * sizeof(uint8_t)); |
| 87 | memset(tab_bit, 0, MODBUS_MAX_READ_BITS * sizeof(uint8_t)); |
| 88 | |
| 89 | /* Allocate and initialize the memory to store the registers */ |
| 90 | tab_reg = (uint16_t *) malloc(MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t)); |
| 91 | memset(tab_reg, 0, MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t)); |
| 92 | |
| 93 | printf("READ BITS\n\n"); |
| 94 | |
| 95 | nb_points = MODBUS_MAX_READ_BITS; |
| 96 | start = gettime_ms(); |
| 97 | for (i = 0; i < n_loop; i++) { |
| 98 | rc = modbus_read_bits(ctx, 0, nb_points, tab_bit); |
| 99 | if (rc == -1) { |
| 100 | fprintf(stderr, "%s\n", modbus_strerror(errno)); |
| 101 | return -1; |
| 102 | } |
| 103 | } |
| 104 | end = gettime_ms(); |
| 105 | elapsed = end - start; |
| 106 | |
| 107 | rate = (uint64_t) n_loop * nb_points * G_MSEC_PER_SEC / (end - start); |
| 108 | printf("Transfer rate in points/seconds:\n"); |
| 109 | printf("* %" PRIu64 " points/s\n", rate); |
| 110 | printf("\n"); |
| 111 | |
| 112 | bytes = n_loop * ((nb_points / 8) + ((nb_points % 8) ? 1 : 0)); |
| 113 | rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 114 | printf("Values:\n"); |
| 115 | printf("* %d x %d values\n", n_loop, nb_points); |
| 116 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 117 | printf("* %" PRIu64 " KiB/s\n", rate); |
| 118 | printf("\n"); |
| 119 | |
| 120 | /* TCP: Query and response header and values */ |
| 121 | bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0); |
| 122 | printf("Values and TCP Modbus overhead:\n"); |
| 123 | printf("* %d x %d bytes\n", n_loop, bytes); |
| 124 | bytes = n_loop * bytes; |
| 125 | rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 126 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 127 | printf("* %" PRIu64 " KiB/s\n", rate); |
| 128 | printf("\n\n"); |
| 129 | |
| 130 | printf("READ REGISTERS\n\n"); |
| 131 | |
| 132 | nb_points = MODBUS_MAX_READ_REGISTERS; |
| 133 | start = gettime_ms(); |
| 134 | for (i = 0; i < n_loop; i++) { |
| 135 | rc = modbus_read_registers(ctx, 0, nb_points, tab_reg); |
| 136 | if (rc == -1) { |
| 137 | fprintf(stderr, "%s\n", modbus_strerror(errno)); |
| 138 | return -1; |
| 139 | } |
| 140 | } |
| 141 | end = gettime_ms(); |
| 142 | elapsed = end - start; |
| 143 | |
| 144 | rate = (uint64_t) n_loop * nb_points * G_MSEC_PER_SEC / (end - start); |
| 145 | printf("Transfer rate in points/seconds:\n"); |
| 146 | printf("* %" PRIu64 " registers/s\n", rate); |
| 147 | printf("\n"); |
| 148 | |
| 149 | bytes = n_loop * nb_points * sizeof(uint16_t); |
| 150 | rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 151 | printf("Values:\n"); |
| 152 | printf("* %d x %d values\n", n_loop, nb_points); |
| 153 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 154 | printf("* %" PRIu64 " KiB/s\n", rate); |
| 155 | printf("\n"); |
| 156 | |
| 157 | /* TCP:Query and response header and values */ |
| 158 | bytes = 12 + 9 + (nb_points * sizeof(uint16_t)); |
| 159 | printf("Values and TCP Modbus overhead:\n"); |
| 160 | printf("* %d x %d bytes\n", n_loop, bytes); |
| 161 | bytes = n_loop * bytes; |
| 162 | rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 163 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 164 | printf("* %" PRIu64 " KiB/s\n", rate); |
| 165 | printf("\n\n"); |
| 166 | |
| 167 | printf("WRITE AND READ REGISTERS\n\n"); |
| 168 | |
| 169 | nb_points = MODBUS_MAX_WR_WRITE_REGISTERS; |
| 170 | start = gettime_ms(); |
| 171 | for (i = 0; i < n_loop; i++) { |
| 172 | rc = modbus_write_and_read_registers( |
| 173 | ctx, 0, nb_points, tab_reg, 0, nb_points, tab_reg); |
| 174 | if (rc == -1) { |
| 175 | fprintf(stderr, "%s\n", modbus_strerror(errno)); |
| 176 | return -1; |
| 177 | } |
| 178 | } |
| 179 | end = gettime_ms(); |
| 180 | elapsed = end - start; |
| 181 | |
| 182 | rate = (uint64_t) n_loop * nb_points * G_MSEC_PER_SEC / (end - start); |
| 183 | printf("Transfer rate in points/seconds:\n"); |
| 184 | printf("* %" PRIu64 " registers/s\n", rate); |
| 185 | printf("\n"); |
| 186 | |
| 187 | bytes = n_loop * nb_points * sizeof(uint16_t); |
| 188 | rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 189 | printf("Values:\n"); |
| 190 | printf("* %d x %d values\n", n_loop, nb_points); |
| 191 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 192 | printf("* %" PRIu64 " KiB/s\n", rate); |
| 193 | printf("\n"); |
| 194 | |
| 195 | /* TCP:Query and response header and values */ |
| 196 | bytes = 12 + 9 + (nb_points * sizeof(uint16_t)); |
| 197 | printf("Values and TCP Modbus overhead:\n"); |
| 198 | printf("* %d x %d bytes\n", n_loop, bytes); |
| 199 | bytes = n_loop * bytes; |
| 200 | rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start); |
| 201 | printf("* %.3f ms for %d bytes\n", elapsed, bytes); |
| 202 | printf("* %" PRIu64 " KiB/s\n", rate); |
| 203 | printf("\n"); |
| 204 | |
| 205 | /* Free the memory */ |
| 206 | free(tab_bit); |
| 207 | free(tab_reg); |
| 208 | |
| 209 | /* Close the connection */ |
| 210 | modbus_close(ctx); |
| 211 | modbus_free(ctx); |
| 212 | |
| 213 | return 0; |
| 214 | } |