unit-test-server.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 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 bb 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. if (rc == -1) {
  129. /* Connection closed by the client or error */
  130. /* We could answer with an exception on EMBBADDATA to indicate
  131. illegal data for example */
  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(500);
  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. }