random-test-client.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #ifndef _MSC_VER
  19. #include <unistd.h>
  20. #endif
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <modbus.h>
  25. /* The goal of this program is to check all major functions of
  26. libmodbus:
  27. - write_coil
  28. - read_bits
  29. - write_coils
  30. - write_register
  31. - read_registers
  32. - write_registers
  33. - read_registers
  34. All these functions are called with random values on a address
  35. range defined by the following defines.
  36. */
  37. #define LOOP 1
  38. #define SERVER_ID 17
  39. #define ADDRESS_START 0
  40. #define ADDRESS_END 99
  41. /* At each loop, the program works in the range ADDRESS_START to
  42. * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on.
  43. */
  44. int main(void)
  45. {
  46. modbus_t *ctx;
  47. int rc;
  48. int nb_fail;
  49. int nb_loop;
  50. int addr;
  51. int nb;
  52. uint8_t *tab_rq_bits;
  53. uint8_t *tab_rp_bits;
  54. uint16_t *tab_rq_registers;
  55. uint16_t *tab_rw_rq_registers;
  56. uint16_t *tab_rp_registers;
  57. /* RTU */
  58. /*
  59. ctx = modbus_new_rtu("/dev/ttyUSB0", 19200, 'N', 8, 1);
  60. modbus_set_slave(ctx, SERVER_ID);
  61. */
  62. /* TCP */
  63. ctx = modbus_new_tcp("127.0.0.1", 1502);
  64. modbus_set_debug(ctx, TRUE);
  65. if (modbus_connect(ctx) == -1) {
  66. fprintf(stderr, "Connection failed: %s\n",
  67. modbus_strerror(errno));
  68. modbus_free(ctx);
  69. return -1;
  70. }
  71. /* Allocate and initialize the different memory spaces */
  72. nb = ADDRESS_END - ADDRESS_START;
  73. tab_rq_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
  74. memset(tab_rq_bits, 0, nb * sizeof(uint8_t));
  75. tab_rp_bits = (uint8_t *) malloc(nb * sizeof(uint8_t));
  76. memset(tab_rp_bits, 0, nb * sizeof(uint8_t));
  77. tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  78. memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
  79. tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  80. memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
  81. tab_rw_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
  82. memset(tab_rw_rq_registers, 0, nb * sizeof(uint16_t));
  83. nb_loop = nb_fail = 0;
  84. while (nb_loop++ < LOOP) {
  85. for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
  86. int i;
  87. /* Random numbers (short) */
  88. for (i=0; i<nb; i++) {
  89. tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
  90. tab_rw_rq_registers[i] = ~tab_rq_registers[i];
  91. tab_rq_bits[i] = tab_rq_registers[i] % 2;
  92. }
  93. nb = ADDRESS_END - addr;
  94. /* WRITE BIT */
  95. rc = modbus_write_bit(ctx, addr, tab_rq_bits[0]);
  96. if (rc != 1) {
  97. printf("ERROR modbus_write_bit (%d)\n", rc);
  98. printf("Address = %d, value = %d\n", addr, tab_rq_bits[0]);
  99. nb_fail++;
  100. } else {
  101. rc = modbus_read_bits(ctx, addr, 1, tab_rp_bits);
  102. if (rc != 1 || tab_rq_bits[0] != tab_rp_bits[0]) {
  103. printf("ERROR modbus_read_bits single (%d)\n", rc);
  104. printf("address = %d\n", addr);
  105. nb_fail++;
  106. }
  107. }
  108. /* MULTIPLE BITS */
  109. rc = modbus_write_bits(ctx, addr, nb, tab_rq_bits);
  110. if (rc != nb) {
  111. printf("ERROR modbus_write_bits (%d)\n", rc);
  112. printf("Address = %d, nb = %d\n", addr, nb);
  113. nb_fail++;
  114. } else {
  115. rc = modbus_read_bits(ctx, addr, nb, tab_rp_bits);
  116. if (rc != nb) {
  117. printf("ERROR modbus_read_bits\n");
  118. printf("Address = %d, nb = %d\n", addr, nb);
  119. nb_fail++;
  120. } else {
  121. for (i=0; i<nb; i++) {
  122. if (tab_rp_bits[i] != tab_rq_bits[i]) {
  123. printf("ERROR modbus_read_bits\n");
  124. printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
  125. addr, tab_rq_bits[i], tab_rq_bits[i],
  126. tab_rp_bits[i], tab_rp_bits[i]);
  127. nb_fail++;
  128. }
  129. }
  130. }
  131. }
  132. /* SINGLE REGISTER */
  133. rc = modbus_write_register(ctx, addr, tab_rq_registers[0]);
  134. if (rc != 1) {
  135. printf("ERROR modbus_write_register (%d)\n", rc);
  136. printf("Address = %d, value = %d (0x%X)\n",
  137. addr, tab_rq_registers[0], tab_rq_registers[0]);
  138. nb_fail++;
  139. } else {
  140. rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
  141. if (rc != 1) {
  142. printf("ERROR modbus_read_registers single (%d)\n", rc);
  143. printf("Address = %d\n", addr);
  144. nb_fail++;
  145. } else {
  146. if (tab_rq_registers[0] != tab_rp_registers[0]) {
  147. printf("ERROR modbus_read_registers single\n");
  148. printf("Address = %d, value = %d (0x%X) != %d (0x%X)\n",
  149. addr, tab_rq_registers[0], tab_rq_registers[0],
  150. tab_rp_registers[0], tab_rp_registers[0]);
  151. nb_fail++;
  152. }
  153. }
  154. }
  155. /* MULTIPLE REGISTERS */
  156. rc = modbus_write_registers(ctx, addr, nb, tab_rq_registers);
  157. if (rc != nb) {
  158. printf("ERROR modbus_write_registers (%d)\n", rc);
  159. printf("Address = %d, nb = %d\n", addr, nb);
  160. nb_fail++;
  161. } else {
  162. rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
  163. if (rc != nb) {
  164. printf("ERROR modbus_read_registers (%d)\n", rc);
  165. printf("Address = %d, nb = %d\n", addr, nb);
  166. nb_fail++;
  167. } else {
  168. for (i=0; i<nb; i++) {
  169. if (tab_rq_registers[i] != tab_rp_registers[i]) {
  170. printf("ERROR modbus_read_registers\n");
  171. printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
  172. addr, 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. /* R/W MULTIPLE REGISTERS */
  180. rc = modbus_write_and_read_registers(ctx,
  181. addr, nb, tab_rw_rq_registers,
  182. addr, nb, tab_rp_registers);
  183. if (rc != nb) {
  184. printf("ERROR modbus_read_and_write_registers (%d)\n", rc);
  185. printf("Address = %d, nb = %d\n", addr, nb);
  186. nb_fail++;
  187. } else {
  188. for (i=0; i<nb; i++) {
  189. if (tab_rp_registers[i] != tab_rw_rq_registers[i]) {
  190. printf("ERROR modbus_read_and_write_registers READ\n");
  191. printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
  192. addr, tab_rp_registers[i], tab_rw_rq_registers[i],
  193. tab_rp_registers[i], tab_rw_rq_registers[i]);
  194. nb_fail++;
  195. }
  196. }
  197. rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
  198. if (rc != nb) {
  199. printf("ERROR modbus_read_registers (%d)\n", rc);
  200. printf("Address = %d, nb = %d\n", addr, nb);
  201. nb_fail++;
  202. } else {
  203. for (i=0; i<nb; i++) {
  204. if (tab_rw_rq_registers[i] != tab_rp_registers[i]) {
  205. printf("ERROR modbus_read_and_write_registers WRITE\n");
  206. printf("Address = %d, value %d (0x%X) != %d (0x%X)\n",
  207. addr, tab_rw_rq_registers[i], tab_rw_rq_registers[i],
  208. tab_rp_registers[i], tab_rp_registers[i]);
  209. nb_fail++;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. printf("Test: ");
  216. if (nb_fail)
  217. printf("%d FAILS\n", nb_fail);
  218. else
  219. printf("SUCCESS\n");
  220. }
  221. /* Free the memory */
  222. free(tab_rq_bits);
  223. free(tab_rp_bits);
  224. free(tab_rq_registers);
  225. free(tab_rp_registers);
  226. free(tab_rw_rq_registers);
  227. /* Close the connection */
  228. modbus_close(ctx);
  229. modbus_free(ctx);
  230. return 0;
  231. }