random-test-master.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 3
  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);
  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. printf("%d = %d\n", i, tab_rq_status[i]);
  81. }
  82. nb = ADDRESS_END - addr;
  83. /* SINGLE COIL */
  84. ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]);
  85. if (ret != 1) {
  86. printf("ERROR force_single_coil (%d)\n", ret);
  87. printf("Slave = %d, address = %d, value = %d\n",
  88. SLAVE, addr, tab_rq_status[0]);
  89. nb_fail++;
  90. } else {
  91. ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status);
  92. if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) {
  93. printf("ERROR read_coil_status single (%d)\n", ret);
  94. printf("Slave = %d, address = %d\n",
  95. SLAVE, addr);
  96. nb_fail++;
  97. }
  98. }
  99. /* MULTIPLE COILS */
  100. ret = force_multiple_coils(&mb_param, SLAVE, addr, nb, tab_rq_status);
  101. if (ret != nb) {
  102. printf("ERROR force_multiple_coils (%d)\n", ret);
  103. printf("Slave = %d, address = %d, nb = %d\n",
  104. SLAVE, addr, nb);
  105. nb_fail++;
  106. } else {
  107. ret = read_coil_status(&mb_param, SLAVE, addr, nb, tab_rp_status);
  108. if (ret != nb) {
  109. printf("ERROR read_coil_status\n");
  110. printf("Slave = %d, address = %d, nb = %d\n",
  111. SLAVE, addr, nb);
  112. nb_fail++;
  113. } else {
  114. for (i=0; i<nb; i++) {
  115. printf("i = %d, tab_rp_status[i] = %d, tab_rq_status[i] = %d\n",
  116. i, tab_rp_status[i], tab_rq_status[i]);
  117. if (tab_rp_status[i] != tab_rq_status[i]) {
  118. printf("ERROR read_coil_status ");
  119. printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]);
  120. printf("Slave = %d, address = %d\n",
  121. SLAVE, addr);
  122. nb_fail++;
  123. }
  124. }
  125. }
  126. }
  127. break;
  128. /* SINGLE REGISTER */
  129. ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]);
  130. if (ret != 1) {
  131. printf("ERROR preset_single_register (%d)\n", ret);
  132. printf("Slave = %d, address = %d, value = %d\n",
  133. SLAVE, addr, tab_rq_registers[0]);
  134. nb_fail++;
  135. } else {
  136. ret = read_holding_registers(&mb_param, SLAVE,
  137. addr, 1, tab_rp_registers);
  138. if (ret != 1) {
  139. printf("ERROR read_holding_registers single (%d)\n", ret);
  140. printf("Slave = %d, address = %d\n",
  141. SLAVE, addr);
  142. nb_fail++;
  143. } else {
  144. if (tab_rq_registers[0] != tab_rp_registers[0]) {
  145. printf("ERROR read_holding_registers single ");
  146. printf("(%d != %d)\n",
  147. tab_rq_registers[0], tab_rp_registers[0]);
  148. printf("Slave = %d, address = %d\n",
  149. SLAVE, addr);
  150. nb_fail++;
  151. }
  152. }
  153. }
  154. /* MULTIPLE REGISTERS */
  155. ret = preset_multiple_registers(&mb_param, SLAVE,
  156. addr, nb, tab_rq_registers);
  157. if (ret != nb) {
  158. printf("ERROR preset_multiple_registers (%d)\n", ret);
  159. printf("Slave = %d, address = %d, nb = %d\n",
  160. SLAVE, addr, nb);
  161. nb_fail++;
  162. } else {
  163. ret = read_holding_registers(&mb_param, SLAVE,
  164. addr, nb, tab_rp_registers);
  165. if (ret != nb) {
  166. printf("ERROR read_holding_registers (%d)\n", ret);
  167. printf("Slave = %d, address = %d, nb = %d\n",
  168. SLAVE, addr, nb);
  169. nb_fail++;
  170. } else {
  171. for (i=0; i<nb; i++) {
  172. if (tab_rq_registers[i] != tab_rp_registers[i]) {
  173. printf("ERROR read_holding_registers ");
  174. printf("(%d != %d)\n",
  175. tab_rq_registers[i], tab_rp_registers[i]);
  176. printf("Slave = %d, address = %d\n",
  177. SLAVE, addr);
  178. nb_fail++;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. if (nb_fail)
  185. printf("Address: %d - nb of fails: %d\n", addr, nb_fail);
  186. else
  187. printf("Address: %d - OK\n", addr);
  188. }
  189. /* Free the memory */
  190. free(tab_rq_status);
  191. free(tab_rp_status);
  192. free(tab_rq_registers);
  193. free(tab_rp_registers);
  194. /* Close the connection */
  195. modbus_close(&mb_param);
  196. return 0;
  197. }