bandwidth-client.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright © 2008-2010 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 GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <errno.h>
  24. #include <modbus.h>
  25. #define G_MSEC_PER_SEC 1000
  26. uint32_t gettime_ms(void)
  27. {
  28. struct timeval tv;
  29. gettimeofday (&tv, NULL);
  30. return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
  31. }
  32. enum {
  33. TCP,
  34. RTU
  35. };
  36. /* Tests based on PI-MBUS-300 documentation */
  37. int main(int argc, char *argv[])
  38. {
  39. uint8_t *tab_bit;
  40. uint16_t *tab_reg;
  41. modbus_t *ctx;
  42. int i;
  43. int nb_points;
  44. double elapsed;
  45. uint32_t start;
  46. uint32_t end;
  47. uint32_t bytes;
  48. uint32_t rate;
  49. int rc;
  50. int n_loop;
  51. int use_backend;
  52. if (argc > 1) {
  53. if (strcmp(argv[1], "tcp") == 0) {
  54. use_backend = TCP;
  55. n_loop = 100000;
  56. } else if (strcmp(argv[1], "rtu") == 0) {
  57. use_backend = RTU;
  58. n_loop = 100;
  59. } else {
  60. printf("Usage:\n %s [tcp|rtu] - Modbus client to measure data bandwith\n\n");
  61. exit(1);
  62. }
  63. } else {
  64. /* By default */
  65. use_backend = TCP;
  66. n_loop = 100000;
  67. }
  68. if (use_backend == TCP) {
  69. ctx = modbus_new_tcp("127.0.0.1", 1502);
  70. } else {
  71. ctx = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
  72. modbus_set_slave(ctx, 1);
  73. }
  74. if (modbus_connect(ctx) == -1) {
  75. fprintf(stderr, "Connexion failed: %s\n",
  76. modbus_strerror(errno));
  77. modbus_free(ctx);
  78. return -1;
  79. }
  80. /* Allocate and initialize the memory to store the status */
  81. tab_bit = (uint8_t *) malloc(MODBUS_MAX_READ_BITS * sizeof(uint8_t));
  82. memset(tab_bit, 0, MODBUS_MAX_READ_BITS * sizeof(uint8_t));
  83. /* Allocate and initialize the memory to store the registers */
  84. tab_reg = (uint16_t *) malloc(MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t));
  85. memset(tab_reg, 0, MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t));
  86. printf("READ BITS\n\n");
  87. nb_points = MODBUS_MAX_READ_BITS;
  88. start = gettime_ms();
  89. for (i=0; i<n_loop; i++) {
  90. rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
  91. if (rc == -1) {
  92. fprintf(stderr, "%s\n", modbus_strerror(errno));
  93. return -1;
  94. }
  95. }
  96. end = gettime_ms();
  97. elapsed = end - start;
  98. rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
  99. printf("Transfert rate in points/seconds:\n");
  100. printf("* %d points/s\n", rate);
  101. printf("\n");
  102. bytes = n_loop * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  103. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  104. printf("Values:\n");
  105. printf("* %d x %d values\n", n_loop, nb_points);
  106. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  107. printf("* %d KiB/s\n", rate);
  108. printf("\n");
  109. /* TCP: Query and reponse header and values */
  110. bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  111. printf("Values and TCP Modbus overhead:\n");
  112. printf("* %d x %d bytes\n", n_loop, bytes);
  113. bytes = n_loop * bytes;
  114. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  115. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  116. printf("* %d KiB/s\n", rate);
  117. printf("\n\n");
  118. printf("READ REGISTERS\n\n");
  119. nb_points = MODBUS_MAX_READ_REGISTERS;
  120. start = gettime_ms();
  121. for (i=0; i<n_loop; i++) {
  122. rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
  123. if (rc == -1) {
  124. fprintf(stderr, "%s\n", modbus_strerror(errno));
  125. return -1;
  126. }
  127. }
  128. end = gettime_ms();
  129. elapsed = end - start;
  130. rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
  131. printf("Transfert rate in points/seconds:\n");
  132. printf("* %d registers/s\n", rate);
  133. printf("\n");
  134. bytes = n_loop * nb_points * sizeof(uint16_t);
  135. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  136. printf("Values:\n");
  137. printf("* %d x %d values\n", n_loop, nb_points);
  138. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  139. printf("* %d KiB/s\n", rate);
  140. printf("\n");
  141. /* TCP:Query and reponse header and values */
  142. bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
  143. printf("Values and TCP Modbus overhead:\n");
  144. printf("* %d x %d bytes\n", n_loop, bytes);
  145. bytes = n_loop * bytes;
  146. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  147. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  148. printf("* %d KiB/s\n", rate);
  149. printf("\n");
  150. printf("READ AND WRITE REGISTERS\n\n");
  151. nb_points = MODBUS_MAX_RW_WRITE_REGISTERS;
  152. start = gettime_ms();
  153. for (i=0; i<n_loop; i++) {
  154. rc = modbus_read_and_write_registers(ctx,
  155. 0, nb_points, tab_reg,
  156. 0, nb_points, tab_reg);
  157. if (rc == -1) {
  158. fprintf(stderr, "%s\n", modbus_strerror(errno));
  159. return -1;
  160. }
  161. }
  162. end = gettime_ms();
  163. elapsed = end - start;
  164. rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
  165. printf("Transfert rate in points/seconds:\n");
  166. printf("* %d registers/s\n", rate);
  167. printf("\n");
  168. bytes = n_loop * nb_points * sizeof(uint16_t);
  169. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  170. printf("Values:\n");
  171. printf("* %d x %d values\n", n_loop, nb_points);
  172. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  173. printf("* %d KiB/s\n", rate);
  174. printf("\n");
  175. /* TCP:Query and reponse header and values */
  176. bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
  177. printf("Values and TCP Modbus overhead:\n");
  178. printf("* %d x %d bytes\n", n_loop, bytes);
  179. bytes = n_loop * bytes;
  180. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  181. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  182. printf("* %d KiB/s\n", rate);
  183. printf("\n");
  184. /* Free the memory */
  185. free(tab_bit);
  186. free(tab_reg);
  187. /* Close the connection */
  188. modbus_close(ctx);
  189. modbus_free(ctx);
  190. return 0;
  191. }