bandwidth-client.c 6.1 KB

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