bandwidth-master.c 5.2 KB

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