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