bandwidth-client.c 6.0 KB

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