random-test-client.c 7.3 KB

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