unit-test-server.c 7.4 KB

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