test-master-random.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2008 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * Licensed under the GNU General Public License Version 2
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <modbus/modbus.h>
  24. /* The goal of this program is to check all major functions of
  25. libmodbus:
  26. - force_single_coil
  27. - read_coil_status
  28. - force_multiple_coils
  29. - preset_single_register
  30. - read_holding_registers
  31. - preset_multiple_registers
  32. - read_holding_registers
  33. All these functions are called with random values on a address
  34. range defined by following defines.
  35. This program is also really useful to test your remote target unit.
  36. */
  37. #define LOOP 1
  38. #define SLAVE 0x11
  39. #define ADDR_MIN 0
  40. #define ADDR_MAX 499
  41. #define FIELDS 500
  42. int main(void)
  43. {
  44. int ok, fail;
  45. int loop_nb;
  46. int addr;
  47. int field_nb;
  48. int *tab_rq;
  49. int *tab_rq_bits;
  50. int *tab_rp;
  51. modbus_param_t mb_param;
  52. /* RTU parity : none, even, odd */
  53. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  54. /* TCP */
  55. modbus_init_tcp(&mb_param, "192.168.0.100", MODBUS_TCP_DEFAULT_PORT);
  56. modbus_set_debug(&mb_param, TRUE);
  57. modbus_connect(&mb_param);
  58. /* Allocate and initialize the different memory spaces */
  59. tab_rq = (int *) malloc(FIELDS * sizeof(int));
  60. memset(tab_rq, 0, FIELDS * sizeof(int));
  61. tab_rq_bits = (int *) malloc(FIELDS * sizeof(int));
  62. memset(tab_rq_bits, 0, FIELDS * sizeof(int));
  63. tab_rp = (int *) malloc(FIELDS * sizeof(int));
  64. memset(tab_rp, 0, FIELDS * sizeof(int));
  65. loop_nb = ok = fail = 0;
  66. while (loop_nb++ < LOOP) {
  67. for (addr=ADDR_MIN; addr <= ADDR_MAX; addr++) {
  68. for (field_nb=1; field_nb<=FIELDS; field_nb++) {
  69. int i;
  70. /* Random numbers (short) */
  71. for (i=0; i<field_nb; i++) {
  72. tab_rq[i] = (int) (16536.0*rand()/(RAND_MAX+1.0));
  73. tab_rq_bits[i] = (i) % 2;
  74. }
  75. /* SINGLE COIL */
  76. ok = force_single_coil(&mb_param, SLAVE, addr, tab_rq_bits[0]);
  77. if (ok != 1) {
  78. printf("ERROR force_single_coil (%d)\n", ok);
  79. printf("Slave = %d, address = %d, value = %d\n",
  80. SLAVE, addr, tab_rq_bits[0]);
  81. fail++;
  82. } else {
  83. ok = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp);
  84. if (ok != 1 || tab_rq_bits[0] != tab_rp[0]) {
  85. printf("ERROR read_coil_status single (%d)\n", ok);
  86. printf("Slave = %d, address = %d\n",
  87. SLAVE, addr);
  88. fail++;
  89. }
  90. }
  91. /* MULTIPLE COILS */
  92. ok = force_multiple_coils(&mb_param, SLAVE, addr, field_nb, tab_rq_bits);
  93. if (ok != field_nb) {
  94. printf("ERROR force_multiple_coils (%d)\n", ok);
  95. printf("Slave = %d, address = %d, field_nb = %d\n",
  96. SLAVE, addr, field_nb);
  97. fail++;
  98. } else {
  99. ok = read_coil_status(&mb_param, SLAVE, addr,
  100. field_nb, tab_rp);
  101. if (ok != field_nb) {
  102. printf("ERROR read_coil_status\n");
  103. printf("Slave = %d, address = %d, field_nb = %d\n",
  104. SLAVE, addr, field_nb);
  105. fail++;
  106. } else {
  107. for (i=0; i<field_nb; i++) {
  108. if (tab_rp[i] != tab_rq_bits[i]) {
  109. printf("ERROR read_coil_status ");
  110. printf("(%d != %d)\n", tab_rp[i], tab_rq_bits[i]);
  111. printf("Slave = %d, address = %d\n",
  112. SLAVE, addr);
  113. fail++;
  114. }
  115. }
  116. }
  117. }
  118. /* SINGLE REGISTER */
  119. ok = preset_single_register(&mb_param, SLAVE, addr, tab_rq[0]);
  120. if (ok != 1) {
  121. printf("ERROR preset_single_register (%d)\n", ok);
  122. printf("Slave = %d, address = %d, value = %d\n",
  123. SLAVE, addr, tab_rq[0]);
  124. fail++;
  125. } else {
  126. ok = read_holding_registers(&mb_param, SLAVE,
  127. addr, 1, tab_rp);
  128. if (ok != 1) {
  129. printf("ERROR read_holding_registers single (%d)\n", ok);
  130. printf("Slave = %d, address = %d\n",
  131. SLAVE, addr);
  132. fail++;
  133. } else {
  134. if (tab_rq[0] != tab_rp[0]) {
  135. printf("ERROR read_holding_registers single ");
  136. printf("(%d != %d)\n",
  137. tab_rq[0], tab_rp[0]);
  138. printf("Slave = %d, address = %d\n",
  139. SLAVE, addr);
  140. fail++;
  141. }
  142. }
  143. }
  144. /* MULTIPLE REGISTERS */
  145. ok = preset_multiple_registers(&mb_param, SLAVE,
  146. addr, field_nb, tab_rq);
  147. if (ok != field_nb) {
  148. printf("ERROR preset_multiple_registers (%d)\n", ok);
  149. printf("Slave = %d, address = %d, field_nb = %d\n",
  150. SLAVE, addr, field_nb);
  151. fail++;
  152. } else {
  153. ok = read_holding_registers(&mb_param, SLAVE,
  154. addr, field_nb, tab_rp);
  155. if (ok != field_nb) {
  156. printf("ERROR read_holding_registers (%d)\n", ok);
  157. printf("Slave = %d, address = %d, field_nb = %d\n",
  158. SLAVE, addr, field_nb);
  159. fail++;
  160. } else {
  161. for (i=0; i<field_nb; i++) {
  162. if (tab_rq[i] != tab_rp[i]) {
  163. printf("ERROR read_holding_registers ");
  164. printf("(%d != %d)\n",
  165. tab_rq[i], tab_rp[i]);
  166. printf("Slave = %d, address = %d\n",
  167. SLAVE, addr);
  168. fail++;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. if (fail)
  175. printf("Address : %d - Fails sum : %d\n", addr, fail);
  176. else
  177. printf("Address : %d - OK\n", addr);
  178. }
  179. }
  180. /* Free the memory */
  181. free(tab_rp);
  182. free(tab_rq);
  183. free(tab_rq_bits);
  184. /* Close the connection */
  185. modbus_close(&mb_param);
  186. return 0;
  187. }