2 * Copyright © Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: BSD-3-Clause
14 # include <winsock2.h>
16 # include <sys/socket.h>
21 # define MSG_NOSIGNAL 0
24 #include "unit-test.h"
32 int main(int argc
, char*argv
[])
36 modbus_mapping_t
*mb_mapping
;
44 if (strcmp(argv
[1], "tcp") == 0) {
46 } else if (strcmp(argv
[1], "tcppi") == 0) {
48 } else if (strcmp(argv
[1], "rtu") == 0) {
51 printf("Usage:\n %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv
[0]);
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
);
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
);
70 header_length
= modbus_get_header_length(ctx
);
72 modbus_set_debug(ctx
, TRUE
);
74 mb_mapping
= modbus_mapping_new_start_address(
75 UT_BITS_ADDRESS
, UT_BITS_NB
,
76 UT_INPUT_BITS_ADDRESS
, UT_INPUT_BITS_NB
,
77 UT_REGISTERS_ADDRESS
, UT_REGISTERS_NB_MAX
,
78 UT_INPUT_REGISTERS_ADDRESS
, UT_INPUT_REGISTERS_NB
);
79 if (mb_mapping
== NULL
) {
80 fprintf(stderr
, "Failed to allocate the mapping: %s\n",
81 modbus_strerror(errno
));
86 /* Examples from PI_MODBUS_300.pdf.
87 Only the read-only input values are assigned. */
89 /* Initialize input values that's can be only done server side. */
90 modbus_set_bits_from_bytes(mb_mapping
->tab_input_bits
, 0, UT_INPUT_BITS_NB
,
93 /* Initialize values of INPUT REGISTERS */
94 for (i
=0; i
< UT_INPUT_REGISTERS_NB
; i
++) {
95 mb_mapping
->tab_input_registers
[i
] = UT_INPUT_REGISTERS_TAB
[i
];
98 if (use_backend
== TCP
) {
99 s
= modbus_tcp_listen(ctx
, 1);
100 modbus_tcp_accept(ctx
, &s
);
101 } else if (use_backend
== TCP_PI
) {
102 s
= modbus_tcp_pi_listen(ctx
, 1);
103 modbus_tcp_pi_accept(ctx
, &s
);
105 rc
= modbus_connect(ctx
);
107 fprintf(stderr
, "Unable to connect %s\n", modbus_strerror(errno
));
115 rc
= modbus_receive(ctx
, query
);
116 /* Filtered queries return 0 */
119 /* The connection is not closed on errors which require on reply such as
121 if (rc
== -1 && errno
!= EMBBADCRC
) {
126 /* Special server behavior to test client */
127 if (query
[header_length
] == 0x03) {
128 /* Read holding registers */
130 if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 3)
131 == UT_REGISTERS_NB_SPECIAL
) {
132 printf("Set an incorrect number of values\n");
133 MODBUS_SET_INT16_TO_INT8(query
, header_length
+ 3,
134 UT_REGISTERS_NB_SPECIAL
- 1);
135 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
136 == UT_REGISTERS_ADDRESS_SPECIAL
) {
137 printf("Reply to this special register address by an exception\n");
138 modbus_reply_exception(ctx
, query
,
139 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY
);
141 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
142 == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE
) {
143 const int RAW_REQ_LENGTH
= 5;
144 uint8_t raw_req
[] = {
145 (use_backend
== RTU
) ? INVALID_SERVER_ID
: 0xFF,
150 printf("Reply with an invalid TID or slave\n");
151 modbus_send_raw_request(ctx
, raw_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
153 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
154 == UT_REGISTERS_ADDRESS_SLEEP_500_MS
) {
155 printf("Sleep 0.5 s before replying\n");
157 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
158 == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS
) {
159 /* Test low level only available in TCP mode */
160 /* Catch the reply and send reply byte a byte */
161 uint8_t req
[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
163 int w_s
= modbus_get_socket(ctx
);
165 fprintf(stderr
, "Unable to get a valid socket in special test\n");
171 for (i
=0; i
< req_length
; i
++) {
172 printf("(%.2X)", req
[i
]);
174 rc
= send(w_s
, (const char*)(req
+ i
), 1, MSG_NOSIGNAL
);
183 rc
= modbus_reply(ctx
, query
, rc
, mb_mapping
);
189 printf("Quit the loop: %s\n", modbus_strerror(errno
));
191 if (use_backend
== TCP
) {
196 modbus_mapping_free(mb_mapping
);