bandwidth-master.c 4.7 KB

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