test-master-random.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * Licensed under the GNU General Public License Version 2
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <modbus/modbus.h>
  24. /* The goal of this program is to check all major functions of
  25. libmodbus:
  26. - force_single_coil
  27. - read_coil_status
  28. - force_multiple_coils
  29. - preset_single_register
  30. - read_holding_registers
  31. - preset_multiple_registers
  32. - read_holding_registers
  33. All these functions are called with random values on a address
  34. range defined by the following defines.
  35. */
  36. #define LOOP 1
  37. #define SLAVE 0x11
  38. #define ADDRESS_START 0
  39. #define ADDRESS_END 499
  40. /* At each loop, the program works in the range ADDRESS_START to
  41. * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
  42. */
  43. int main(void)
  44. {
  45. int ret;
  46. int nb_fail;
  47. int nb_loop;
  48. int addr;
  49. int nb_points_total;
  50. uint8_t *tab_rq_status;
  51. uint8_t *tab_rp_status;
  52. uint16_t *tab_rq_registers;
  53. uint16_t *tab_rp_registers;
  54. modbus_param_t mb_param;
  55. /* RTU parity : none, even, odd */
  56. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  57. /* TCP */
  58. modbus_init_tcp(&mb_param, "192.168.0.100", MODBUS_TCP_DEFAULT_PORT);
  59. modbus_set_debug(&mb_param, TRUE);
  60. modbus_connect(&mb_param);
  61. /* Allocate and initialize the different memory spaces */
  62. nb_points_total = ADDRESS_END - ADDRESS_START;
  63. tab_rq_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t));
  64. memset(tab_rq_status, 0, nb_points_total * sizeof(uint8_t));
  65. tab_rp_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t));
  66. memset(tab_rp_status, 0, nb_points_total * sizeof(uint8_t));
  67. tab_rq_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t));
  68. memset(tab_rq_registers, 0, nb_points_total * sizeof(uint16_t));
  69. tab_rp_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t));
  70. memset(tab_rp_status, 0, nb_points_total * sizeof(uint16_t));
  71. nb_loop = nb_fail = 0;
  72. while (nb_loop++ < LOOP) {
  73. for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
  74. int i;
  75. int nb_points;
  76. /* Random numbers (short) */
  77. for (i=0; i<nb_points_total; i++) {
  78. tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
  79. tab_rq_status[i] = tab_rq_registers[i] % 2;
  80. }
  81. nb_points = ADDRESS_END - addr;
  82. /* SINGLE COIL */
  83. ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]);
  84. if (ret != 1) {
  85. printf("ERROR force_single_coil (%d)\n", ret);
  86. printf("Slave = %d, address = %d, value = %d\n",
  87. SLAVE, addr, tab_rq_status[0]);
  88. nb_fail++;
  89. } else {
  90. ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status);
  91. if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) {
  92. printf("ERROR read_coil_status single (%d)\n", ret);
  93. printf("Slave = %d, address = %d\n",
  94. SLAVE, addr);
  95. nb_fail++;
  96. }
  97. }
  98. /* MULTIPLE COILS */
  99. ret = force_multiple_coils(&mb_param, SLAVE, addr, nb_points, tab_rq_status);
  100. if (ret != nb_points) {
  101. printf("ERROR force_multiple_coils (%d)\n", ret);
  102. printf("Slave = %d, address = %d, nb_points = %d\n",
  103. SLAVE, addr, nb_points);
  104. nb_fail++;
  105. } else {
  106. ret = read_coil_status(&mb_param, SLAVE, addr, nb_points, tab_rp_status);
  107. if (ret != nb_points) {
  108. printf("ERROR read_coil_status\n");
  109. printf("Slave = %d, address = %d, nb_points = %d\n",
  110. SLAVE, addr, nb_points);
  111. nb_fail++;
  112. } else {
  113. for (i=0; i<nb_points; i++) {
  114. if (tab_rp_status[i] != tab_rq_status[i]) {
  115. printf("ERROR read_coil_status ");
  116. printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]);
  117. printf("Slave = %d, address = %d\n",
  118. SLAVE, addr);
  119. nb_fail++;
  120. }
  121. }
  122. }
  123. }
  124. /* SINGLE REGISTER */
  125. ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]);
  126. if (ret != 1) {
  127. printf("ERROR preset_single_register (%d)\n", ret);
  128. printf("Slave = %d, address = %d, value = %d\n",
  129. SLAVE, addr, tab_rq_registers[0]);
  130. nb_fail++;
  131. } else {
  132. ret = read_holding_registers(&mb_param, SLAVE,
  133. addr, 1, tab_rp_registers);
  134. if (ret != 1) {
  135. printf("ERROR read_holding_registers single (%d)\n", ret);
  136. printf("Slave = %d, address = %d\n",
  137. SLAVE, addr);
  138. nb_fail++;
  139. } else {
  140. if (tab_rq_registers[0] != tab_rp_registers[0]) {
  141. printf("ERROR read_holding_registers single ");
  142. printf("(%d != %d)\n",
  143. tab_rq_registers[0], tab_rp_registers[0]);
  144. printf("Slave = %d, address = %d\n",
  145. SLAVE, addr);
  146. nb_fail++;
  147. }
  148. }
  149. }
  150. /* MULTIPLE REGISTERS */
  151. ret = preset_multiple_registers(&mb_param, SLAVE,
  152. addr, nb_points, tab_rq_registers);
  153. if (ret != nb_points) {
  154. printf("ERROR preset_multiple_registers (%d)\n", ret);
  155. printf("Slave = %d, address = %d, nb_points = %d\n",
  156. SLAVE, addr, nb_points);
  157. nb_fail++;
  158. } else {
  159. ret = read_holding_registers(&mb_param, SLAVE,
  160. addr, nb_points, tab_rp_registers);
  161. if (ret != nb_points) {
  162. printf("ERROR read_holding_registers (%d)\n", ret);
  163. printf("Slave = %d, address = %d, nb_points = %d\n",
  164. SLAVE, addr, nb_points);
  165. nb_fail++;
  166. } else {
  167. for (i=0; i<nb_points; i++) {
  168. if (tab_rq_registers[i] != tab_rp_registers[i]) {
  169. printf("ERROR read_holding_registers ");
  170. printf("(%d != %d)\n",
  171. tab_rq_registers[i], tab_rp_registers[i]);
  172. printf("Slave = %d, address = %d\n",
  173. SLAVE, addr);
  174. nb_fail++;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. if (nb_fail)
  181. printf("Address: %d - nb of fails: %d\n", addr, nb_fail);
  182. else
  183. printf("Address: %d - OK\n", addr);
  184. }
  185. /* Free the memory */
  186. free(tab_rp_status);
  187. free(tab_rq_status);
  188. free(tab_rp_registers);
  189. free(tab_rq_registers);
  190. /* Close the connection */
  191. modbus_close(&mb_param);
  192. return 0;
  193. }