test-master-random.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright © 2001-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 <modbus/modbus.h>
  22. /* The goal of this program is to check all major functions of
  23. libmodbus:
  24. - force_single_coil
  25. - read_coil_status
  26. - force_multiple_coils
  27. - preset_single_register
  28. - read_holding_registers
  29. - preset_multiple_registers
  30. - read_holding_registers
  31. All these functions are called with random values on a address
  32. range defined by the following defines.
  33. */
  34. #define LOOP 1
  35. #define SLAVE 0x11
  36. #define ADDRESS_START 0
  37. #define ADDRESS_END 499
  38. /* At each loop, the program works in the range ADDRESS_START to
  39. * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
  40. */
  41. int main(void)
  42. {
  43. int ret;
  44. int nb_fail;
  45. int nb_loop;
  46. int addr;
  47. int nb_points_total;
  48. uint8_t *tab_rq_status;
  49. uint8_t *tab_rp_status;
  50. uint16_t *tab_rq_registers;
  51. uint16_t *tab_rp_registers;
  52. modbus_param_t mb_param;
  53. /* RTU parity : none, even, odd */
  54. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  55. /* TCP */
  56. modbus_init_tcp(&mb_param, "192.168.0.100", MODBUS_TCP_DEFAULT_PORT);
  57. modbus_set_debug(&mb_param, TRUE);
  58. modbus_connect(&mb_param);
  59. /* Allocate and initialize the different memory spaces */
  60. nb_points_total = ADDRESS_END - ADDRESS_START;
  61. tab_rq_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t));
  62. memset(tab_rq_status, 0, nb_points_total * sizeof(uint8_t));
  63. tab_rp_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t));
  64. memset(tab_rp_status, 0, nb_points_total * sizeof(uint8_t));
  65. tab_rq_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t));
  66. memset(tab_rq_registers, 0, nb_points_total * sizeof(uint16_t));
  67. tab_rp_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t));
  68. memset(tab_rp_status, 0, nb_points_total * sizeof(uint16_t));
  69. nb_loop = nb_fail = 0;
  70. while (nb_loop++ < LOOP) {
  71. for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
  72. int i;
  73. int nb_points;
  74. /* Random numbers (short) */
  75. for (i=0; i<nb_points_total; i++) {
  76. tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
  77. tab_rq_status[i] = tab_rq_registers[i] % 2;
  78. }
  79. nb_points = ADDRESS_END - addr;
  80. /* SINGLE COIL */
  81. ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]);
  82. if (ret != 1) {
  83. printf("ERROR force_single_coil (%d)\n", ret);
  84. printf("Slave = %d, address = %d, value = %d\n",
  85. SLAVE, addr, tab_rq_status[0]);
  86. nb_fail++;
  87. } else {
  88. ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status);
  89. if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) {
  90. printf("ERROR read_coil_status single (%d)\n", ret);
  91. printf("Slave = %d, address = %d\n",
  92. SLAVE, addr);
  93. nb_fail++;
  94. }
  95. }
  96. /* MULTIPLE COILS */
  97. ret = force_multiple_coils(&mb_param, SLAVE, addr, nb_points, tab_rq_status);
  98. if (ret != nb_points) {
  99. printf("ERROR force_multiple_coils (%d)\n", ret);
  100. printf("Slave = %d, address = %d, nb_points = %d\n",
  101. SLAVE, addr, nb_points);
  102. nb_fail++;
  103. } else {
  104. ret = read_coil_status(&mb_param, SLAVE, addr, nb_points, tab_rp_status);
  105. if (ret != nb_points) {
  106. printf("ERROR read_coil_status\n");
  107. printf("Slave = %d, address = %d, nb_points = %d\n",
  108. SLAVE, addr, nb_points);
  109. nb_fail++;
  110. } else {
  111. for (i=0; i<nb_points; i++) {
  112. if (tab_rp_status[i] != tab_rq_status[i]) {
  113. printf("ERROR read_coil_status ");
  114. printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]);
  115. printf("Slave = %d, address = %d\n",
  116. SLAVE, addr);
  117. nb_fail++;
  118. }
  119. }
  120. }
  121. }
  122. /* SINGLE REGISTER */
  123. ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]);
  124. if (ret != 1) {
  125. printf("ERROR preset_single_register (%d)\n", ret);
  126. printf("Slave = %d, address = %d, value = %d\n",
  127. SLAVE, addr, tab_rq_registers[0]);
  128. nb_fail++;
  129. } else {
  130. ret = read_holding_registers(&mb_param, SLAVE,
  131. addr, 1, tab_rp_registers);
  132. if (ret != 1) {
  133. printf("ERROR read_holding_registers single (%d)\n", ret);
  134. printf("Slave = %d, address = %d\n",
  135. SLAVE, addr);
  136. nb_fail++;
  137. } else {
  138. if (tab_rq_registers[0] != tab_rp_registers[0]) {
  139. printf("ERROR read_holding_registers single ");
  140. printf("(%d != %d)\n",
  141. tab_rq_registers[0], tab_rp_registers[0]);
  142. printf("Slave = %d, address = %d\n",
  143. SLAVE, addr);
  144. nb_fail++;
  145. }
  146. }
  147. }
  148. /* MULTIPLE REGISTERS */
  149. ret = preset_multiple_registers(&mb_param, SLAVE,
  150. addr, nb_points, tab_rq_registers);
  151. if (ret != nb_points) {
  152. printf("ERROR preset_multiple_registers (%d)\n", ret);
  153. printf("Slave = %d, address = %d, nb_points = %d\n",
  154. SLAVE, addr, nb_points);
  155. nb_fail++;
  156. } else {
  157. ret = read_holding_registers(&mb_param, SLAVE,
  158. addr, nb_points, tab_rp_registers);
  159. if (ret != nb_points) {
  160. printf("ERROR read_holding_registers (%d)\n", ret);
  161. printf("Slave = %d, address = %d, nb_points = %d\n",
  162. SLAVE, addr, nb_points);
  163. nb_fail++;
  164. } else {
  165. for (i=0; i<nb_points; i++) {
  166. if (tab_rq_registers[i] != tab_rp_registers[i]) {
  167. printf("ERROR read_holding_registers ");
  168. printf("(%d != %d)\n",
  169. tab_rq_registers[i], tab_rp_registers[i]);
  170. printf("Slave = %d, address = %d\n",
  171. SLAVE, addr);
  172. nb_fail++;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. if (nb_fail)
  179. printf("Address: %d - nb of fails: %d\n", addr, nb_fail);
  180. else
  181. printf("Address: %d - OK\n", addr);
  182. }
  183. /* Free the memory */
  184. free(tab_rp_status);
  185. free(tab_rq_status);
  186. free(tab_rp_registers);
  187. free(tab_rq_registers);
  188. /* Close the connection */
  189. modbus_close(&mb_param);
  190. return 0;
  191. }