unit-test-server.c 7.0 KB

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