2 * Copyright © 2008-2014 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(
75 UT_BITS_ADDRESS
+ UT_BITS_NB
,
76 UT_INPUT_BITS_ADDRESS
+ UT_INPUT_BITS_NB
,
77 UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB
,
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 /* Unit tests of modbus_mapping_new (tests would not be sufficient if two
87 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
);
94 if (mb_mapping
->nb_input_bits
!= UT_INPUT_BITS_ADDRESS
+ UT_INPUT_BITS_NB
) {
95 printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS
+ UT_INPUT_BITS_NB
);
100 if (mb_mapping
->nb_registers
!= UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB
) {
101 printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS
+ UT_REGISTERS_NB
);
106 if (mb_mapping
->nb_input_registers
!= UT_INPUT_REGISTERS_ADDRESS
+ UT_INPUT_REGISTERS_NB
) {
107 printf("Invalid nb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS
+ UT_INPUT_REGISTERS_NB
);
112 /* Examples from PI_MODBUS_300.pdf.
113 Only the read-only input values are assigned. */
116 modbus_set_bits_from_bytes(mb_mapping
->tab_input_bits
,
117 UT_INPUT_BITS_ADDRESS
, UT_INPUT_BITS_NB
,
120 /** INPUT REGISTERS **/
121 for (i
=0; i
< UT_INPUT_REGISTERS_NB
; i
++) {
122 mb_mapping
->tab_input_registers
[UT_INPUT_REGISTERS_ADDRESS
+i
] =
123 UT_INPUT_REGISTERS_TAB
[i
];;
126 if (use_backend
== TCP
) {
127 s
= modbus_tcp_listen(ctx
, 1);
128 modbus_tcp_accept(ctx
, &s
);
129 } else if (use_backend
== TCP_PI
) {
130 s
= modbus_tcp_pi_listen(ctx
, 1);
131 modbus_tcp_pi_accept(ctx
, &s
);
133 rc
= modbus_connect(ctx
);
135 fprintf(stderr
, "Unable to connect %s\n", modbus_strerror(errno
));
143 rc
= modbus_receive(ctx
, query
);
144 /* Filtered queries return 0 */
147 /* The connection is not closed on errors which require on reply such as
149 if (rc
== -1 && errno
!= EMBBADCRC
) {
154 /* Special server behavior to test client */
155 if (query
[header_length
] == 0x03) {
156 /* Read holding registers */
158 if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 3)
159 == UT_REGISTERS_NB_SPECIAL
) {
160 printf("Set an incorrect number of values\n");
161 MODBUS_SET_INT16_TO_INT8(query
, header_length
+ 3,
162 UT_REGISTERS_NB_SPECIAL
- 1);
163 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
164 == UT_REGISTERS_ADDRESS_SPECIAL
) {
165 printf("Reply to this special register address by an exception\n");
166 modbus_reply_exception(ctx
, query
,
167 MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY
);
169 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
170 == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE
) {
171 const int RAW_REQ_LENGTH
= 5;
172 uint8_t raw_req
[] = {
173 (use_backend
== RTU
) ? INVALID_SERVER_ID
: 0xFF,
178 printf("Reply with an invalid TID or slave\n");
179 modbus_send_raw_request(ctx
, raw_req
, RAW_REQ_LENGTH
* sizeof(uint8_t));
181 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
182 == UT_REGISTERS_ADDRESS_SLEEP_500_MS
) {
183 printf("Sleep 0.5 s before replying\n");
185 } else if (MODBUS_GET_INT16_FROM_INT8(query
, header_length
+ 1)
186 == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS
) {
187 /* Test low level only available in TCP mode */
188 /* Catch the reply and send reply byte a byte */
189 uint8_t req
[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
191 int w_s
= modbus_get_socket(ctx
);
195 for (i
=0; i
< req_length
; i
++) {
196 printf("(%.2X)", req
[i
]);
198 send(w_s
, (const char*)(req
+ i
), 1, MSG_NOSIGNAL
);
204 rc
= modbus_reply(ctx
, query
, rc
, mb_mapping
);
210 printf("Quit the loop: %s\n", modbus_strerror(errno
));
212 if (use_backend
== TCP
) {
217 modbus_mapping_free(mb_mapping
);