bandwidth-client.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* Tests based on PI-MBUS-300 documentation */
  26. #define NB_LOOPS 100000
  27. #define G_MSEC_PER_SEC 1000
  28. uint32_t gettime_ms(void)
  29. {
  30. struct timeval tv;
  31. gettimeofday (&tv, NULL);
  32. return (uint32_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
  33. }
  34. int main(void)
  35. {
  36. uint8_t *tab_bit;
  37. uint16_t *tab_reg;
  38. modbus_t *ctx;
  39. int i;
  40. int nb_points;
  41. double elapsed;
  42. uint32_t start;
  43. uint32_t end;
  44. uint32_t bytes;
  45. uint32_t rate;
  46. int rc;
  47. /* TCP */
  48. ctx = modbus_new_tcp("127.0.0.1", 1502);
  49. if (modbus_connect(ctx) == -1) {
  50. fprintf(stderr, "Connexion failed: %s\n",
  51. modbus_strerror(errno));
  52. modbus_free(ctx);
  53. return -1;
  54. }
  55. /* Allocate and initialize the memory to store the status */
  56. tab_bit = (uint8_t *) malloc(MODBUS_MAX_BITS * sizeof(uint8_t));
  57. memset(tab_bit, 0, MODBUS_MAX_BITS * sizeof(uint8_t));
  58. /* Allocate and initialize the memory to store the registers */
  59. tab_reg = (uint16_t *) malloc(MODBUS_MAX_REGISTERS * sizeof(uint16_t));
  60. memset(tab_reg, 0, MODBUS_MAX_REGISTERS * sizeof(uint16_t));
  61. printf("READ BITS\n\n");
  62. nb_points = MODBUS_MAX_BITS;
  63. start = gettime_ms();
  64. for (i=0; i<NB_LOOPS; i++) {
  65. rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
  66. if (rc == -1) {
  67. fprintf(stderr, "%s\n", modbus_strerror(errno));
  68. return -1;
  69. }
  70. }
  71. end = gettime_ms();
  72. elapsed = end - start;
  73. rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  74. printf("Transfert rate in points/seconds:\n");
  75. printf("* %'d points/s\n", rate);
  76. printf("\n");
  77. bytes = NB_LOOPS * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  78. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  79. printf("Values:\n");
  80. printf("* %d x %d values\n", NB_LOOPS, nb_points);
  81. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  82. printf("* %'d KiB/s\n", rate);
  83. printf("\n");
  84. /* TCP: Query and reponse header and values */
  85. bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  86. printf("Values and TCP Modbus overhead:\n");
  87. printf("* %d x %d bytes\n", NB_LOOPS, bytes);
  88. bytes = NB_LOOPS * bytes;
  89. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  90. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  91. printf("* %'d KiB/s\n", rate);
  92. printf("\n\n");
  93. printf("READ REGISTERS\n\n");
  94. nb_points = MODBUS_MAX_REGISTERS;
  95. start = gettime_ms();
  96. for (i=0; i<NB_LOOPS; i++) {
  97. rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
  98. if (rc == -1) {
  99. fprintf(stderr, "%s\n", modbus_strerror(errno));
  100. return -1;
  101. }
  102. }
  103. end = gettime_ms();
  104. elapsed = end - start;
  105. rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  106. printf("Transfert rate in points/seconds:\n");
  107. printf("* %'d registers/s\n", rate);
  108. printf("\n");
  109. bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
  110. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  111. printf("Values:\n");
  112. printf("* %d x %d values\n", NB_LOOPS, nb_points);
  113. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  114. printf("* %'d KiB/s\n", rate);
  115. printf("\n");
  116. /* TCP:Query and reponse header and values */
  117. bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
  118. printf("Values and TCP Modbus overhead:\n");
  119. printf("* %d x %d bytes\n", NB_LOOPS, bytes);
  120. bytes = NB_LOOPS * bytes;
  121. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  122. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  123. printf("* %'d KiB/s\n", rate);
  124. printf("\n");
  125. /* Free the memory */
  126. free(tab_bit);
  127. free(tab_reg);
  128. /* Close the connection */
  129. modbus_close(ctx);
  130. modbus_free(ctx);
  131. return 0;
  132. }