unit-test-server.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_start_address(
  66. UT_BITS_ADDRESS, UT_BITS_NB,
  67. UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
  68. UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX,
  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. /* Examples from PI_MODBUS_300.pdf.
  77. Only the read-only input values are assigned. */
  78. /* Initialize input values that's can be only done server side. */
  79. modbus_set_bits_from_bytes(mb_mapping->tab_input_bits, 0, UT_INPUT_BITS_NB,
  80. UT_INPUT_BITS_TAB);
  81. /* Initialize values of INPUT REGISTERS */
  82. for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
  83. mb_mapping->tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i];;
  84. }
  85. if (use_backend == TCP) {
  86. s = modbus_tcp_listen(ctx, 1);
  87. modbus_tcp_accept(ctx, &s);
  88. } else if (use_backend == TCP_PI) {
  89. s = modbus_tcp_pi_listen(ctx, 1);
  90. modbus_tcp_pi_accept(ctx, &s);
  91. } else {
  92. rc = modbus_connect(ctx);
  93. if (rc == -1) {
  94. fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
  95. modbus_free(ctx);
  96. return -1;
  97. }
  98. }
  99. for (;;) {
  100. do {
  101. rc = modbus_receive(ctx, query);
  102. /* Filtered queries return 0 */
  103. } while (rc == 0);
  104. /* The connection is not closed on errors which require on reply such as
  105. bad CRC in RTU. */
  106. if (rc == -1 && errno != EMBBADCRC) {
  107. /* Quit */
  108. break;
  109. }
  110. /* Special server behavior to test client */
  111. if (query[header_length] == 0x03) {
  112. /* Read holding registers */
  113. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
  114. == UT_REGISTERS_NB_SPECIAL) {
  115. printf("Set an incorrect number of values\n");
  116. MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
  117. UT_REGISTERS_NB_SPECIAL - 1);
  118. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  119. == UT_REGISTERS_ADDRESS_SPECIAL) {
  120. printf("Reply to this special register address by an exception\n");
  121. modbus_reply_exception(ctx, query,
  122. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  123. continue;
  124. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  125. == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
  126. const int RAW_REQ_LENGTH = 5;
  127. uint8_t raw_req[] = {
  128. (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
  129. 0x03,
  130. 0x02, 0x00, 0x00
  131. };
  132. printf("Reply with an invalid TID or slave\n");
  133. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  134. continue;
  135. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  136. == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
  137. printf("Sleep 0.5 s before replying\n");
  138. usleep(500000);
  139. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  140. == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
  141. /* Test low level only available in TCP mode */
  142. /* Catch the reply and send reply byte a byte */
  143. uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
  144. int req_length = 11;
  145. int w_s = modbus_get_socket(ctx);
  146. if (w_s == -1) {
  147. fprintf(stderr, "Unable to get a valid socket in special test\n");
  148. continue;
  149. }
  150. /* Copy TID */
  151. req[1] = query[1];
  152. for (i=0; i < req_length; i++) {
  153. printf("(%.2X)", req[i]);
  154. usleep(5000);
  155. rc = send(w_s, (const char*)(req + i), 1, MSG_NOSIGNAL);
  156. if (rc == -1) {
  157. break;
  158. }
  159. }
  160. continue;
  161. }
  162. }
  163. rc = modbus_reply(ctx, query, rc, mb_mapping);
  164. if (rc == -1) {
  165. break;
  166. }
  167. }
  168. printf("Quit the loop: %s\n", modbus_strerror(errno));
  169. if (use_backend == TCP) {
  170. if (s != -1) {
  171. close(s);
  172. }
  173. }
  174. modbus_mapping_free(mb_mapping);
  175. free(query);
  176. /* For RTU */
  177. modbus_close(ctx);
  178. modbus_free(ctx);
  179. return 0;
  180. }