unit-test-server.c 7.1 KB

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