random-test-master.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright © 2001-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 <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 99
  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;
  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, "127.0.0.1", 1502, SLAVE);
  57. modbus_set_debug(&mb_param, TRUE);
  58. if (modbus_connect(&mb_param) == -1) {
  59. printf("ERROR Connection failed\n");
  60. exit(1);
  61. }
  62. /* Allocate and initialize the different memory spaces */
  63. nb = ADDRESS_END - ADDRESS_START;
  64. tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
  65. memset(tab_rq_status, 0, nb * sizeof(uint8_t));
  66. tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
  67. memset(tab_rp_status, 0, nb * sizeof(uint8_t));
  68. tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  69. memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
  70. tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  71. memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
  72. nb_loop = nb_fail = 0;
  73. while (nb_loop++ < LOOP) {
  74. for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
  75. int i;
  76. /* Random numbers (short) */
  77. for (i=0; i<nb; 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 = ADDRESS_END - addr;
  82. /* SINGLE COIL */
  83. ret = force_single_coil(&mb_param, 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, 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, addr, nb, tab_rq_status);
  100. if (ret != nb) {
  101. printf("ERROR force_multiple_coils (%d)\n", ret);
  102. printf("Slave = %d, address = %d, nb = %d\n",
  103. SLAVE, addr, nb);
  104. nb_fail++;
  105. } else {
  106. ret = read_coil_status(&mb_param, addr, nb, tab_rp_status);
  107. if (ret != nb) {
  108. printf("ERROR read_coil_status\n");
  109. printf("Slave = %d, address = %d, nb = %d\n",
  110. SLAVE, addr, nb);
  111. nb_fail++;
  112. } else {
  113. for (i=0; i<nb; i++) {
  114. if (tab_rp_status[i] != tab_rq_status[i]) {
  115. printf("ERROR read_coil_status\n");
  116. printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
  117. SLAVE, addr,
  118. tab_rq_status[i], tab_rq_status[i],
  119. tab_rp_status[i], tab_rp_status[i]);
  120. nb_fail++;
  121. }
  122. }
  123. }
  124. }
  125. /* SINGLE REGISTER */
  126. ret = preset_single_register(&mb_param, addr, tab_rq_registers[0]);
  127. if (ret != 1) {
  128. printf("ERROR preset_single_register (%d)\n", ret);
  129. printf("Slave = %d, address = %d, value = %d (0x%X)\n",
  130. SLAVE, addr, tab_rq_registers[0], tab_rq_registers[0]);
  131. nb_fail++;
  132. } else {
  133. ret = read_holding_registers(&mb_param, 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\n");
  142. printf("Slave = %d, address = %d, value = %d (0x%X) != %d (0x%X)\n",
  143. SLAVE, addr,
  144. tab_rq_registers[0], tab_rq_registers[0],
  145. tab_rp_registers[0], tab_rp_registers[0]);
  146. nb_fail++;
  147. }
  148. }
  149. }
  150. /* MULTIPLE REGISTERS */
  151. ret = preset_multiple_registers(&mb_param, addr, nb,
  152. tab_rq_registers);
  153. if (ret != nb) {
  154. printf("ERROR preset_multiple_registers (%d)\n", ret);
  155. printf("Slave = %d, address = %d, nb = %d\n",
  156. SLAVE, addr, nb);
  157. nb_fail++;
  158. } else {
  159. ret = read_holding_registers(&mb_param, addr, nb,
  160. tab_rp_registers);
  161. if (ret != nb) {
  162. printf("ERROR read_holding_registers (%d)\n", ret);
  163. printf("Slave = %d, address = %d, nb = %d\n",
  164. SLAVE, addr, nb);
  165. nb_fail++;
  166. } else {
  167. for (i=0; i<nb; i++) {
  168. if (tab_rq_registers[i] != tab_rp_registers[i]) {
  169. printf("ERROR read_holding_registers\n");
  170. printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
  171. SLAVE, addr,
  172. tab_rq_registers[i], tab_rq_registers[i],
  173. tab_rp_registers[i], tab_rp_registers[i]);
  174. nb_fail++;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. printf("Test: ");
  181. if (nb_fail)
  182. printf("%d FAILS\n", nb_fail);
  183. else
  184. printf("SUCCESS\n");
  185. }
  186. /* Free the memory */
  187. free(tab_rq_status);
  188. free(tab_rp_status);
  189. free(tab_rq_registers);
  190. free(tab_rp_registers);
  191. /* Close the connection */
  192. modbus_close(&mb_param);
  193. return 0;
  194. }