random-test-client.c 9.2 KB

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