unit-test-server.c 7.6 KB

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