random-test-master.c 10 KB

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