bandwidth-master.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <modbus/modbus.h>
  24. /* Tests based on PI-MBUS-300 documentation */
  25. #define SLAVE 0x11
  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_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, SLAVE);
  49. if (modbus_connect(&mb_param) == -1) {
  50. exit(1);
  51. }
  52. /* Allocate and initialize the memory to store the status */
  53. tab_rp_status = (uint8_t *) malloc(MAX_STATUS * sizeof(uint8_t));
  54. memset(tab_rp_status, 0, MAX_STATUS * sizeof(uint8_t));
  55. /* Allocate and initialize the memory to store the registers */
  56. tab_rp_registers = (uint16_t *) malloc(MAX_REGISTERS * sizeof(uint16_t));
  57. memset(tab_rp_registers, 0, MAX_REGISTERS * sizeof(uint16_t));
  58. printf("READ COIL STATUS\n\n");
  59. nb_points = MAX_STATUS;
  60. start = gettime_ms();
  61. for (i=0; i<NB_LOOPS; i++) {
  62. ret = read_coil_status(&mb_param, 0, nb_points, tab_rp_status);
  63. }
  64. end = gettime_ms();
  65. elapsed = end - start;
  66. rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  67. printf("Transfert rate in points/seconds:\n");
  68. printf("* %'d points/s\n", rate);
  69. printf("\n");
  70. bytes = NB_LOOPS * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  71. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  72. printf("Values:\n");
  73. printf("* %d x %d values\n", NB_LOOPS, nb_points);
  74. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  75. printf("* %'d KiB/s\n", rate);
  76. printf("\n");
  77. /* TCP: Query and reponse header and values */
  78. bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
  79. printf("Values and TCP Modbus overhead:\n");
  80. printf("* %d x %d bytes\n", NB_LOOPS, bytes);
  81. bytes = NB_LOOPS * bytes;
  82. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  83. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  84. printf("* %'d KiB/s\n", rate);
  85. printf("\n\n");
  86. printf("READ HOLDING REGISTERS\n\n");
  87. nb_points = MAX_REGISTERS;
  88. start = gettime_ms();
  89. for (i=0; i<NB_LOOPS; i++) {
  90. ret = read_holding_registers(&mb_param, 0, nb_points, tab_rp_registers);
  91. }
  92. end = gettime_ms();
  93. elapsed = end - start;
  94. rate = (NB_LOOPS * nb_points) * G_MSEC_PER_SEC / (end - start);
  95. printf("Transfert rate in points/seconds:\n");
  96. printf("* %'d registers/s\n", rate);
  97. printf("\n");
  98. bytes = NB_LOOPS * nb_points * sizeof(uint16_t);
  99. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  100. printf("Values:\n");
  101. printf("* %d x %d values\n", NB_LOOPS, nb_points);
  102. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  103. printf("* %'d KiB/s\n", rate);
  104. printf("\n");
  105. /* TCP:Query and reponse header and values */
  106. bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
  107. printf("Values and TCP Modbus overhead:\n");
  108. printf("* %d x %d bytes\n", NB_LOOPS, bytes);
  109. bytes = NB_LOOPS * bytes;
  110. rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
  111. printf("* %.3f ms for %d bytes\n", elapsed, bytes);
  112. printf("* %'d KiB/s\n", rate);
  113. printf("\n");
  114. /* Free the memory */
  115. free(tab_rp_status);
  116. free(tab_rp_registers);
  117. /* Close the connection */
  118. modbus_close(&mb_param);
  119. return 0;
  120. }