random-test-master.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 MY_ID 1
  37. #define SERVER_ID 17
  38. #define ADDRESS_START 0
  39. #define ADDRESS_END 99
  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 rc;
  46. int nb_fail;
  47. int nb_loop;
  48. int addr;
  49. int nb;
  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. /*
  56. modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, 'N', 8, 1, MY_ID);
  57. */
  58. /* TCP */
  59. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  60. modbus_set_debug(&mb_param, TRUE);
  61. if (modbus_connect(&mb_param) == -1) {
  62. fprintf(stderr, "Connection failed: %s\n",
  63. modbus_strerror(errno));
  64. return -1;
  65. }
  66. /* Allocate and initialize the different memory spaces */
  67. nb = ADDRESS_END - ADDRESS_START;
  68. tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
  69. memset(tab_rq_status, 0, nb * sizeof(uint8_t));
  70. tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
  71. memset(tab_rp_status, 0, nb * sizeof(uint8_t));
  72. tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  73. memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
  74. tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  75. memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
  76. nb_loop = nb_fail = 0;
  77. while (nb_loop++ < LOOP) {
  78. for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
  79. int i;
  80. /* Random numbers (short) */
  81. for (i=0; i<nb; i++) {
  82. tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
  83. tab_rq_status[i] = tab_rq_registers[i] % 2;
  84. }
  85. nb = ADDRESS_END - addr;
  86. /* SINGLE COIL */
  87. rc = force_single_coil(&mb_param, SERVER_ID, addr, tab_rq_status[0]);
  88. if (rc != 1) {
  89. printf("ERROR force_single_coil (%d)\n", rc);
  90. printf("Slave = %d, address = %d, value = %d\n",
  91. SERVER_ID, addr, tab_rq_status[0]);
  92. nb_fail++;
  93. } else {
  94. rc = read_coil_status(&mb_param, SERVER_ID, addr, 1, tab_rp_status);
  95. if (rc != 1 || tab_rq_status[0] != tab_rp_status[0]) {
  96. printf("ERROR read_coil_status single (%d)\n", rc);
  97. printf("Slave = %d, address = %d\n",
  98. SERVER_ID, addr);
  99. nb_fail++;
  100. }
  101. }
  102. /* MULTIPLE COILS */
  103. rc = force_multiple_coils(&mb_param, SERVER_ID, addr, nb, tab_rq_status);
  104. if (rc != nb) {
  105. printf("ERROR force_multiple_coils (%d)\n", rc);
  106. printf("Slave = %d, address = %d, nb = %d\n",
  107. SERVER_ID, addr, nb);
  108. nb_fail++;
  109. } else {
  110. rc = read_coil_status(&mb_param, SERVER_ID, addr, nb, tab_rp_status);
  111. if (rc != nb) {
  112. printf("ERROR read_coil_status\n");
  113. printf("Slave = %d, address = %d, nb = %d\n",
  114. SERVER_ID, addr, nb);
  115. nb_fail++;
  116. } else {
  117. for (i=0; i<nb; i++) {
  118. if (tab_rp_status[i] != tab_rq_status[i]) {
  119. printf("ERROR read_coil_status\n");
  120. printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
  121. SERVER_ID, addr,
  122. tab_rq_status[i], tab_rq_status[i],
  123. tab_rp_status[i], tab_rp_status[i]);
  124. nb_fail++;
  125. }
  126. }
  127. }
  128. }
  129. /* SINGLE REGISTER */
  130. rc = preset_single_register(&mb_param, SERVER_ID, addr, tab_rq_registers[0]);
  131. if (rc != 1) {
  132. printf("ERROR preset_single_register (%d)\n", rc);
  133. printf("Slave = %d, address = %d, value = %d (0x%X)\n",
  134. SERVER_ID, addr, tab_rq_registers[0], tab_rq_registers[0]);
  135. nb_fail++;
  136. } else {
  137. rc = read_holding_registers(&mb_param, SERVER_ID, addr, 1, tab_rp_registers);
  138. if (rc != 1) {
  139. printf("ERROR read_holding_registers single (%d)\n", rc);
  140. printf("Slave = %d, address = %d\n",
  141. SERVER_ID, addr);
  142. nb_fail++;
  143. } else {
  144. if (tab_rq_registers[0] != tab_rp_registers[0]) {
  145. printf("ERROR read_holding_registers single\n");
  146. printf("Slave = %d, address = %d, value = %d (0x%X) != %d (0x%X)\n",
  147. SERVER_ID, addr,
  148. tab_rq_registers[0], tab_rq_registers[0],
  149. tab_rp_registers[0], tab_rp_registers[0]);
  150. nb_fail++;
  151. }
  152. }
  153. }
  154. /* MULTIPLE REGISTERS */
  155. rc = preset_multiple_registers(&mb_param, SERVER_ID, addr, nb,
  156. tab_rq_registers);
  157. if (rc != nb) {
  158. printf("ERROR preset_multiple_registers (%d)\n", rc);
  159. printf("Slave = %d, address = %d, nb = %d\n",
  160. SERVER_ID, addr, nb);
  161. nb_fail++;
  162. } else {
  163. rc = read_holding_registers(&mb_param, SERVER_ID, addr, nb,
  164. tab_rp_registers);
  165. if (rc != nb) {
  166. printf("ERROR read_holding_registers (%d)\n", rc);
  167. printf("Slave = %d, address = %d, nb = %d\n",
  168. SERVER_ID, 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\n");
  174. printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
  175. SERVER_ID, addr,
  176. tab_rq_registers[i], tab_rq_registers[i],
  177. tab_rp_registers[i], tab_rp_registers[i]);
  178. nb_fail++;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. printf("Test: ");
  185. if (nb_fail)
  186. printf("%d FAILS\n", nb_fail);
  187. else
  188. printf("SUCCESS\n");
  189. }
  190. /* Free the memory */
  191. free(tab_rq_status);
  192. free(tab_rp_status);
  193. free(tab_rq_registers);
  194. free(tab_rp_registers);
  195. /* Close the connection */
  196. modbus_close(&mb_param);
  197. return 0;
  198. }