test-modbus-master.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2001-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. #define LOOP 1
  25. #define SLAVE 0x11
  26. #define ADDR_MIN 0
  27. #define ADDR_MAX 499
  28. #define FIELDS 500
  29. int main(void)
  30. {
  31. int ok, fail;
  32. int loop_nb;
  33. int addr;
  34. int field_nb;
  35. int *tab_rq;
  36. int *tab_rq_bits;
  37. int *tab_rp;
  38. modbus_param_t mb_param;
  39. /* RTU parity : none, even, odd */
  40. /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
  41. /* TCP */
  42. modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
  43. modbus_set_debug(&mb_param, TRUE);
  44. modbus_connect(&mb_param);
  45. tab_rq = (int *) malloc(FIELDS * sizeof(int));
  46. tab_rq_bits = (int *) malloc(FIELDS * sizeof(int));
  47. tab_rp = (int *) malloc(FIELDS * sizeof(int));
  48. read_coil_status(&mb_param, SLAVE, 0x13, 0x25, tab_rp);
  49. read_input_status(&mb_param, SLAVE, 0xC4, 0x16, tab_rp);
  50. read_holding_registers(&mb_param, SLAVE, 0x6B, 3, tab_rp);
  51. read_input_registers(&mb_param, SLAVE, 0x8, 1, tab_rp);
  52. force_single_coil(&mb_param, SLAVE, 0xAC, ON);
  53. free(tab_rp);
  54. free(tab_rq);
  55. free(tab_rq_bits);
  56. modbus_close(&mb_param);
  57. return 0;
  58. loop_nb = ok = fail = 0;
  59. while (loop_nb++ < LOOP) {
  60. for (addr=ADDR_MIN; addr <= ADDR_MAX; addr++) {
  61. for (field_nb=1; field_nb<=FIELDS; field_nb++) {
  62. int i;
  63. /* Random numbers (short) */
  64. for (i=0; i<field_nb; i++) {
  65. tab_rq[i] = (int) (16536.0*rand()/(RAND_MAX+1.0));
  66. tab_rq_bits[i] = (i) % 2;
  67. }
  68. /* SINGLE COIL */
  69. ok = force_single_coil(&mb_param, SLAVE, addr, tab_rq_bits[0]);
  70. if (ok != 1) {
  71. printf("ERROR force_single_coil (%d)\n", ok);
  72. printf("Slave = %d, address = %d, value = %d\n",
  73. SLAVE, addr, tab_rq_bits[0]);
  74. fail++;
  75. } else {
  76. ok = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp);
  77. if (ok != 1 || tab_rq_bits[0] != tab_rp[0]) {
  78. printf("ERROR read_coil_status single (%d)\n", ok);
  79. printf("Slave = %d, address = %d\n",
  80. SLAVE, addr);
  81. fail++;
  82. }
  83. }
  84. /* MULTIPLE COILS */
  85. ok = force_multiple_coils(&mb_param, SLAVE, addr, field_nb, tab_rq_bits);
  86. if (ok != field_nb) {
  87. printf("ERROR force_multiple_coils (%d)\n", ok);
  88. printf("Slave = %d, address = %d, field_nb = %d\n",
  89. SLAVE, addr, field_nb);
  90. fail++;
  91. } else {
  92. ok = read_coil_status(&mb_param, SLAVE, addr,
  93. field_nb, tab_rp);
  94. if (ok != field_nb) {
  95. printf("ERROR read_coil_status\n");
  96. printf("Slave = %d, address = %d, field_nb = %d\n",
  97. SLAVE, addr, field_nb);
  98. fail++;
  99. } else {
  100. for (i=0; i<field_nb; i++) {
  101. if (tab_rp[i] != tab_rq_bits[i]) {
  102. printf("ERROR read_coil_status ");
  103. printf("(%d != %d)\n", tab_rp[i], tab_rq_bits[i]);
  104. printf("Slave = %d, address = %d\n",
  105. SLAVE, addr);
  106. fail++;
  107. }
  108. }
  109. }
  110. }
  111. /* SINGLE REGISTER */
  112. ok = preset_single_register(&mb_param, SLAVE, addr, tab_rq[0]);
  113. if (ok != 1) {
  114. printf("ERROR preset_single_register (%d)\n", ok);
  115. printf("Slave = %d, address = %d, value = %d\n",
  116. SLAVE, addr, tab_rq[0]);
  117. fail++;
  118. } else {
  119. ok = read_holding_registers(&mb_param, SLAVE,
  120. addr, 1, tab_rp);
  121. if (ok != 1) {
  122. printf("ERROR read_holding_registers single (%d)\n", ok);
  123. printf("Slave = %d, address = %d\n",
  124. SLAVE, addr);
  125. fail++;
  126. } else {
  127. if (tab_rq[0] != tab_rp[0]) {
  128. printf("ERROR read_holding_registers single ");
  129. printf("(%d != %d)\n",
  130. tab_rq[0], tab_rp[0]);
  131. printf("Slave = %d, address = %d\n",
  132. SLAVE, addr);
  133. fail++;
  134. }
  135. }
  136. }
  137. /* MULTIPLE REGISTERS */
  138. ok = preset_multiple_registers(&mb_param, SLAVE,
  139. addr, field_nb, tab_rq);
  140. if (ok != field_nb) {
  141. printf("ERROR preset_multiple_registers (%d)\n", ok);
  142. printf("Slave = %d, address = %d, field_nb = %d\n",
  143. SLAVE, addr, field_nb);
  144. fail++;
  145. } else {
  146. ok = read_holding_registers(&mb_param, SLAVE,
  147. addr, field_nb, tab_rp);
  148. if (ok != field_nb) {
  149. printf("ERROR read_holding_registers (%d)\n", ok);
  150. printf("Slave = %d, address = %d, field_nb = %d\n",
  151. SLAVE, addr, field_nb);
  152. fail++;
  153. } else {
  154. for (i=0; i<field_nb; i++) {
  155. if (tab_rq[i] != tab_rp[i]) {
  156. printf("ERROR read_holding_registers ");
  157. printf("(%d != %d)\n",
  158. tab_rq[i], tab_rp[i]);
  159. printf("Slave = %d, address = %d\n",
  160. SLAVE, addr);
  161. fail++;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. if (fail)
  168. printf("Address : %d - Fails sum : %d\n", addr, fail);
  169. else
  170. printf("Address : %d - OK\n", addr);
  171. }
  172. }
  173. free(tab_rp);
  174. free(tab_rq);
  175. free(tab_rq_bits);
  176. modbus_close(&mb_param);
  177. return 0;
  178. }