unit-test-server.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright © 2008-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. #include <sys/socket.h>
  24. #include "unit-test.h"
  25. enum {
  26. TCP,
  27. TCP_PI,
  28. RTU
  29. };
  30. int main(int argc, char*argv[])
  31. {
  32. int s = -1;
  33. modbus_t *ctx;
  34. modbus_mapping_t *mb_mapping;
  35. int rc;
  36. int i;
  37. int use_backend;
  38. uint8_t *query;
  39. int header_length;
  40. if (argc > 1) {
  41. if (strcmp(argv[1], "tcp") == 0) {
  42. use_backend = TCP;
  43. } else if (strcmp(argv[1], "tcppi") == 0) {
  44. use_backend = TCP_PI;
  45. } else if (strcmp(argv[1], "rtu") == 0) {
  46. use_backend = RTU;
  47. } else {
  48. printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
  49. return -1;
  50. }
  51. } else {
  52. /* By default */
  53. use_backend = TCP;
  54. }
  55. if (use_backend == TCP) {
  56. ctx = modbus_new_tcp("127.0.0.1", 1502);
  57. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  58. } else if (use_backend == TCP_PI) {
  59. ctx = modbus_new_tcp_pi("::0", "1502");
  60. query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
  61. } else {
  62. ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
  63. modbus_set_slave(ctx, SERVER_ID);
  64. query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
  65. }
  66. header_length = modbus_get_header_length(ctx);
  67. modbus_set_debug(ctx, TRUE);
  68. mb_mapping = modbus_mapping_new(
  69. UT_BITS_ADDRESS + UT_BITS_NB,
  70. UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
  71. UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
  72. UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
  73. if (mb_mapping == NULL) {
  74. fprintf(stderr, "Failed to allocate the mapping: %s\n",
  75. modbus_strerror(errno));
  76. modbus_free(ctx);
  77. return -1;
  78. }
  79. /* Unit tests of modbus_mapping_new (tests would not be sufficient if two nb_* were identical) */
  80. if (mb_mapping->nb_bits != UT_BITS_ADDRESS + UT_BITS_NB) {
  81. printf("Invalid nb bits (%d != %d)\n", UT_BITS_ADDRESS + UT_BITS_NB, mb_mapping->nb_bits);
  82. modbus_free(ctx);
  83. return -1;
  84. }
  85. if (mb_mapping->nb_input_bits != UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB) {
  86. printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB);
  87. modbus_free(ctx);
  88. return -1;
  89. }
  90. if (mb_mapping->nb_registers != UT_REGISTERS_ADDRESS + UT_REGISTERS_NB) {
  91. printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS + UT_REGISTERS_NB);
  92. modbus_free(ctx);
  93. return -1;
  94. }
  95. if (mb_mapping->nb_input_registers != UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB) {
  96. printf("Invalid bb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
  97. modbus_free(ctx);
  98. return -1;
  99. }
  100. /* Examples from PI_MODBUS_300.pdf.
  101. Only the read-only input values are assigned. */
  102. /** INPUT STATUS **/
  103. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
  104. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
  105. UT_INPUT_BITS_TAB);
  106. /** INPUT REGISTERS **/
  107. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  108. mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
  109. UT_INPUT_REGISTERS_TAB[i];;
  110. }
  111. if (use_backend == TCP) {
  112. s = modbus_tcp_listen(ctx, 1);
  113. modbus_tcp_accept(ctx, &s);
  114. } else if (use_backend == TCP_PI) {
  115. s = modbus_tcp_pi_listen(ctx, 1);
  116. modbus_tcp_pi_accept(ctx, &s);
  117. } else {
  118. rc = modbus_connect(ctx);
  119. if (rc == -1) {
  120. fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
  121. modbus_free(ctx);
  122. return -1;
  123. }
  124. }
  125. for (;;) {
  126. do {
  127. rc = modbus_receive(ctx, query);
  128. /* Filtered queries return 0 */
  129. } while (rc == 0);
  130. if (rc == -1) {
  131. /* Connection closed by the client or error */
  132. break;
  133. }
  134. /* Read holding registers */
  135. if (query[header_length] == 0x03) {
  136. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
  137. == UT_REGISTERS_NB_SPECIAL) {
  138. printf("Set an incorrect number of values\n");
  139. MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
  140. UT_REGISTERS_NB_SPECIAL - 1);
  141. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  142. == UT_REGISTERS_ADDRESS_SPECIAL) {
  143. printf("Reply to this special register address by an exception\n");
  144. modbus_reply_exception(ctx, query,
  145. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  146. continue;
  147. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  148. == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
  149. const int RAW_REQ_LENGTH = 5;
  150. uint8_t raw_req[] = {
  151. (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
  152. 0x03,
  153. 0x02, 0x00, 0x00
  154. };
  155. printf("Reply with an invalid TID or slave\n");
  156. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  157. continue;
  158. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  159. == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
  160. printf("Sleep 0.5 s before replying\n");
  161. usleep(500000);
  162. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  163. == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
  164. /* Test low level only available in TCP mode */
  165. /* Catch the reply and send reply byte a byte */
  166. uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
  167. int req_length = 11;
  168. int w_s = modbus_get_socket(ctx);
  169. /* Copy TID */
  170. req[1] = query[1];
  171. for (i=0; i < req_length; i++) {
  172. printf("(%.2X)", req[i]);
  173. usleep(500);
  174. send(w_s, req + i, 1, MSG_NOSIGNAL);
  175. }
  176. continue;
  177. }
  178. }
  179. rc = modbus_reply(ctx, query, rc, mb_mapping);
  180. if (rc == -1) {
  181. break;
  182. }
  183. }
  184. printf("Quit the loop: %s\n", modbus_strerror(errno));
  185. if (use_backend == TCP) {
  186. if (s != -1) {
  187. close(s);
  188. }
  189. }
  190. modbus_mapping_free(mb_mapping);
  191. free(query);
  192. /* For RTU */
  193. modbus_close(ctx);
  194. modbus_free(ctx);
  195. return 0;
  196. }