unit-test-server.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. break;
  141. }
  142. /* Read holding registers */
  143. if (query[header_length] == 0x03) {
  144. if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
  145. == UT_REGISTERS_NB_SPECIAL) {
  146. printf("Set an incorrect number of values\n");
  147. MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
  148. UT_REGISTERS_NB_SPECIAL - 1);
  149. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  150. == UT_REGISTERS_ADDRESS_SPECIAL) {
  151. printf("Reply to this special register address by an exception\n");
  152. modbus_reply_exception(ctx, query,
  153. MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
  154. continue;
  155. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  156. == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
  157. const int RAW_REQ_LENGTH = 5;
  158. uint8_t raw_req[] = {
  159. (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
  160. 0x03,
  161. 0x02, 0x00, 0x00
  162. };
  163. printf("Reply with an invalid TID or slave\n");
  164. modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
  165. continue;
  166. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  167. == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
  168. printf("Sleep 0.5 s before replying\n");
  169. usleep(500000);
  170. } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
  171. == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
  172. /* Test low level only available in TCP mode */
  173. /* Catch the reply and send reply byte a byte */
  174. uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
  175. int req_length = 11;
  176. int w_s = modbus_get_socket(ctx);
  177. /* Copy TID */
  178. req[1] = query[1];
  179. for (i=0; i < req_length; i++) {
  180. printf("(%.2X)", req[i]);
  181. usleep(500);
  182. send(w_s, req + i, 1, MSG_NOSIGNAL);
  183. }
  184. continue;
  185. }
  186. }
  187. rc = modbus_reply(ctx, query, rc, mb_mapping);
  188. if (rc == -1) {
  189. break;
  190. }
  191. }
  192. printf("Quit the loop: %s\n", modbus_strerror(errno));
  193. if (use_backend == TCP) {
  194. if (s != -1) {
  195. close(s);
  196. }
  197. }
  198. modbus_mapping_free(mb_mapping);
  199. free(query);
  200. /* For RTU */
  201. modbus_close(ctx);
  202. modbus_free(ctx);
  203. return 0;
  204. }