Debian Codegraph @

src/modbus-rtu.c

raw symbols SCIP
1 /*
2 * Copyright © Stéphane Raimbault <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #ifndef _MSC_VER
13 #include <unistd.h>
14 #endif
15 #if defined(HAVE_STRUCT_TERMIOS2cxx . . $ `./../config.h:101:9`!)
16 #include <sys/ioctl.h>
17 #endif
18 #include "modbus-private.h"cxx . . $ `<file>/src/modbus-private.h`/
19 #include <assert.h>
20
21 #include "modbus-rtu-private.h"cxx . . $ `<file>/src/modbus-rtu-private.h`/
22 #include "modbus-rtu.h"cxx . . $ `<file>/src/modbus-rtu.h`/
23
24 #if HAVE_DECL_TIOCSRS485cxx . . $ `config.h:19:9`! || HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
25 #include <sys/ioctl.h>
26 #endif
27
28 #if HAVE_DECL_TIOCSRS485cxx . . $ `config.h:19:9`!
29 #include <linux/serial.h>
30 #endif
31
32 /* Table of CRC values for high-order byte */
33 static const uint8_tcxx . . $ uint8_t# table_crc_hicxx . . $ table_crc_hi.[] = {
34 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
35 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
36 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
37 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
38 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
39 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
40 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1,
41 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
42 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
43 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
44 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
45 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
46 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
47 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
48 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
49 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
50 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
51 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
52 0x00, 0xC1, 0x81, 0x40};
53
54 /* Table of CRC values for low-order byte */
55 static const uint8_tcxx . . $ uint8_t# table_crc_locxx . . $ table_crc_lo.[] = {
56 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5,
57 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B,
58 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE,
59 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6,
60 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
61 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
62 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8,
63 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C,
64 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21,
65 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
66 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A,
67 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
68 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7,
69 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51,
70 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
71 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98,
72 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D,
73 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
74 0x41, 0x81, 0x80, 0x40};
75
76 /* Define the slave ID of the remote device to talk in master mode or set the
77 * internal slave ID in slave mode */
78 static int _modbus_set_slavecxx . . $ _modbus_set_slave(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int slave)
79 {
80 int max_slave = (ctx->quirkscxx . . $ _modbus#quirks. & MODBUS_QUIRK_MAX_SLAVEcxx . . $ MODBUS_QUIRK_MAX_SLAVE.) ? 255 : 247;
81
82 /* Broadcast address is 0 (MODBUS_BROADCAST_ADDRESS) */
83 if (slave >= 0 && slave <= max_slave) {
84 ctx->slavecxx . . $ _modbus#slave. = slave;
85 } else {
86 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
87 return -1;
88 }
89
90 return 0;
91 }
92
93 /* Builds a RTU request header */
94 static int _modbus_rtu_build_request_basiscxx . . $ _modbus_rtu_build_request_basis(abc53b0a0396e541).(
95 modbus_tcxx . . $ modbus_t# *ctx, int function, int addr, int nb, uint8_tcxx . . $ uint8_t# *req)
96 {
97 assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(ctx->slave != -1);
98 req[0] = ctx->slavecxx . . $ _modbus#slave.;
99 req[1] = function;
100 req[2] = addr >> 8;
101 req[3] = addr & 0x00ff;
102 req[4] = nb >> 8;
103 req[5] = nb & 0x00ff;
104
105 return _MODBUS_RTU_PRESET_REQ_LENGTHcxx . . $ `src/modbus-rtu-private.h:30:9`!;
106 }
107
108 /* Builds a RTU response header */
109 static int _modbus_rtu_build_response_basiscxx . . $ _modbus_rtu_build_response_basis(acd9c6258f33b2f4).(sft_tcxx . . $ sft_t# *sft, uint8_tcxx . . $ uint8_t# *rsp)
110 {
111 /* In this case, the slave is certainly valid because a check is already
112 * done in _modbus_rtu_listen */
113 rsp[0] = sft->slavecxx . . $ _sft#slave.;
114 rsp[1] = sft->functioncxx . . $ _sft#function.;
115
116 return _MODBUS_RTU_PRESET_RSP_LENGTHcxx . . $ `src/modbus-rtu-private.h:31:9`!;
117 }
118
119 static uint16_tcxx . . $ uint16_t# crc16cxx . . $ crc16(cfcbdd83c4648882).(uint8_tcxx . . $ uint8_t# *buffer, uint16_tcxx . . $ uint16_t# buffer_length)
120 {
121 uint8_tcxx . . $ uint8_t# crc_hi = 0xFF; /* high CRC byte initialized */
122 uint8_tcxx . . $ uint8_t# crc_lo = 0xFF; /* low CRC byte initialized */
123 unsigned int i; /* will index into CRC lookup */
124
125 /* pass through message buffer */
126 while (buffer_length--) {
127 i = crc_lo ^ *buffer++; /* calculate the CRC */
128 crc_lo = crc_hi ^ table_crc_hicxx . . $ table_crc_hi.[i];
129 crc_hi = table_crc_locxx . . $ table_crc_lo.[i];
130 }
131
132 return (crc_hi << 8 | crc_lo);
133 }
134
135 static int _modbus_rtu_get_response_tidcxx . . $ _modbus_rtu_get_response_tid(b84ccf4e0bb14b06).(const uint8_tcxx . . $ uint8_t# *req)
136 {
137 /* No TID */
138 return 0;
139 }
140
141 static int _modbus_rtu_send_msg_precxx . . $ _modbus_rtu_send_msg_pre(7405b416d0535413).(uint8_tcxx . . $ uint8_t# *req, int req_length)
142 {
143 uint16_tcxx . . $ uint16_t# crc = crc16cxx . . $ crc16(cfcbdd83c4648882).(req, req_length);
144
145 /* According to the MODBUS specs (p. 14), the low order byte of the CRC comes
146 * first in the RTU message */
147 req[req_length++] = crc & 0x00FF;
148 req[req_length++] = crc >> 8;
149
150 return req_length;
151 }
152
153 #if defined(_WIN32)
154
155 /* This simple implementation is sort of a substitute of the select() call,
156 * working this way: the win32_ser_select() call tries to read some data from
157 * the serial port, setting the timeout as the select() call would. Data read is
158 * stored into the receive buffer, that is then consumed by the win32_ser_read()
159 * call. So win32_ser_select() does both the event waiting and the reading,
160 * while win32_ser_read() only consumes the receive buffer.
161 */
162
163 static void win32_ser_init(struct win32_ser *ws)
164 {
165 /* Clear everything */
166 memset(ws, 0x00, sizeof(struct win32_ser));
167
168 /* Set file handle to invalid */
169 ws->fd = INVALID_HANDLE_VALUE;
170 }
171
172 /* FIXME Try to remove length_to_read -> max_len argument, only used by win32 */
173 static int win32_ser_select(struct win32_ser *ws, int max_len, const struct timeval *tv)
174 {
175 COMMTIMEOUTS comm_to;
176 unsigned int msec = 0;
177
178 /* Check if some data still in the buffer to be consumed */
179 if (ws->n_bytes > 0) {
180 return 1;
181 }
182
183 /* Setup timeouts like select() would do.
184 FIXME Please someone on Windows can look at this?
185 Does it possible to use WaitCommEvent?
186 When tv is NULL, MAXDWORD isn't infinite!
187 */
188 if (tv == NULL) {
189 msec = MAXDWORD;
190 } else {
191 msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
192 if (msec < 1)
193 msec = 1;
194 }
195
196 comm_to.ReadIntervalTimeout = msec;
197 comm_to.ReadTotalTimeoutMultiplier = 0;
198 comm_to.ReadTotalTimeoutConstant = msec;
199 comm_to.WriteTotalTimeoutMultiplier = 0;
200 comm_to.WriteTotalTimeoutConstant = 1000;
201 SetCommTimeouts(ws->fd, &comm_to);
202
203 /* Read some bytes */
204 if ((max_len > PY_BUF_SIZE) || (max_len < 0)) {
205 max_len = PY_BUF_SIZE;
206 }
207
208 if (ReadFile(ws->fd, &ws->buf, max_len, &ws->n_bytes, NULL)) {
209 /* Check if some bytes available */
210 if (ws->n_bytes > 0) {
211 /* Some bytes read */
212 return 1;
213 } else {
214 /* Just timed out */
215 return 0;
216 }
217 } else {
218 /* Some kind of error */
219 return -1;
220 }
221 }
222
223 static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg, unsigned int max_len)
224 {
225 unsigned int n = ws->n_bytes;
226
227 if (max_len < n) {
228 n = max_len;
229 }
230
231 if (n > 0) {
232 memcpy(p_msg, ws->buf, n);
233 }
234
235 ws->n_bytes -= n;
236
237 return n;
238 }
239 #endif
240
241 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
242 static void _modbus_rtu_ioctl_rtscxx . . $ _modbus_rtu_ioctl_rts(28408cbef86636bb).(modbus_tcxx . . $ modbus_t# *ctx, int on)
243 {
244 int fd = ctx->scxx . . $ _modbus#s.;
245 int flags;
246
247 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(fd, TIOCMGETcxx . . $ `<external>/123502480044527469/ioctls.h:40:9`!, &flags) == -1) {
248 if (ctx->debugcxx . . $ _modbus#debug.) {
249 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ERROR Can't get RTS line state (%s)\n", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
250 }
251 return;
252 }
253 if (on) {
254 flags |= TIOCM_RTScxx . . $ `<external>/2577046224257520258/ioctl-types.h:38:9`!;
255 } else {
256 flags &= ~TIOCM_RTScxx . . $ `<external>/2577046224257520258/ioctl-types.h:38:9`!;
257 }
258 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(fd, TIOCMSETcxx . . $ `<external>/123502480044527469/ioctls.h:43:9`!, &flags) == -1) {
259 if (ctx->debugcxx . . $ _modbus#debug.) {
260 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ERROR Can't set RTS line state (%s)\n", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
261 }
262 }
263 }
264 #endif
265
266 static ssize_tcxx . . $ ssize_t# _modbus_rtu_sendcxx . . $ _modbus_rtu_send(ab2063bac0ea77a5).(modbus_tcxx . . $ modbus_t# *ctx, const uint8_tcxx . . $ uint8_t# *req, int req_length)
267 {
268 #if defined(_WIN32)
269 modbus_rtu_t *ctx_rtu = ctx->backend_data;
270 DWORD n_bytes = 0;
271 return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL))
272 ? (ssize_t) n_bytes
273 : -1;
274 #else
275 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
276 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
277 if (ctx_rtu->rtscxx . . $ _modbus_rtu#rts. != MODBUS_RTU_RTS_NONEcxx . . $ `src/modbus-rtu.h:28:9`!) {
278 ssize_tcxx . . $ ssize_t# size;
279
280 if (ctx->debugcxx . . $ _modbus#debug.) {
281 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Sending request using RTS signal\n");
282 }
283
284 uint64_tcxx . . $ uint64_t# total_delay;
285
286 ctx_rtu->set_rtscxx . . $ _modbus_rtu#set_rts.(ctx, ctx_rtu->rtscxx . . $ _modbus_rtu#rts. == MODBUS_RTU_RTS_UPcxx . . $ `src/modbus-rtu.h:29:9`!);
287 usleepcxx . . $ usleep(4c679297bf94b9eb).(ctx_rtu->rts_delaycxx . . $ _modbus_rtu#rts_delay.);
288
289 size = writecxx . . $ write(41184a59d38b4f65).(ctx->scxx . . $ _modbus#s., req, req_length);
290
291 /* Compute the post-send delay in a wide unsigned type to avoid the
292 signed overflow that occurs with a very low baud (large
293 onebyte_time) and a large request, then clamp to a sane maximum. */
294 total_delay = (uint64_tcxx . . $ uint64_t#) ctx_rtu->onebyte_timecxx . . $ _modbus_rtu#onebyte_time. * (uint64_tcxx . . $ uint64_t#) req_length +
295 (uint64_tcxx . . $ uint64_t#) ctx_rtu->rts_delaycxx . . $ _modbus_rtu#rts_delay.;
296 if (total_delay > 1000000000ULL) {
297 total_delay = 1000000000ULL;
298 }
299 /* POSIX allows usleep() to fail with EINVAL for values >= 1 second
300 so sleep in chunks below that limit */
301 while (total_delay > 0) {
302 useconds_tcxx . . $ useconds_t# delay =
303 (total_delay > 999999ULL) ? 999999 : (useconds_tcxx . . $ useconds_t#) total_delay;
304 usleepcxx . . $ usleep(4c679297bf94b9eb).(delay);
305 total_delay -= delay;
306 }
307 ctx_rtu->set_rtscxx . . $ _modbus_rtu#set_rts.(ctx, ctx_rtu->rtscxx . . $ _modbus_rtu#rts. != MODBUS_RTU_RTS_UPcxx . . $ `src/modbus-rtu.h:29:9`!);
308
309 return size;
310 } else {
311 #endif
312 return writecxx . . $ write(41184a59d38b4f65).(ctx->scxx . . $ _modbus#s., req, req_length);
313 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
314 }
315 #endif
316 #endif
317 }
318
319 static int _modbus_rtu_receivecxx . . $ _modbus_rtu_receive(744a031d86f39170).(modbus_tcxx . . $ modbus_t# *ctx, uint8_tcxx . . $ uint8_t# *req)
320 {
321 int rc;
322 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
323
324 if (ctx_rtu->confirmation_to_ignorecxx . . $ _modbus_rtu#confirmation_to_ignore.) {
325 (void) _modbus_receive_msgcxx . . $ _modbus_receive_msg(85c522f07fbdeb64).(ctx, req, MSG_CONFIRMATIONcxx . . $ MSG_CONFIRMATION.);
326 /* Ignore errors and reset the flag */
327 ctx_rtu->confirmation_to_ignorecxx . . $ _modbus_rtu#confirmation_to_ignore. = FALSEcxx . . $ `src/modbus.h:47:9`!;
328 rc = 0;
329 if (ctx->debugcxx . . $ _modbus#debug.) {
330 printfcxx . . $ printf(581fddd72a8db8fc).("Confirmation to ignore\n");
331 }
332 } else {
333 rc = _modbus_receive_msgcxx . . $ _modbus_receive_msg(85c522f07fbdeb64).(ctx, req, MSG_INDICATIONcxx . . $ MSG_INDICATION.);
334 if (rc == 0) {
335 /* The next expected message is a confirmation to ignore */
336 ctx_rtu->confirmation_to_ignorecxx . . $ _modbus_rtu#confirmation_to_ignore. = TRUEcxx . . $ `src/modbus.h:51:9`!;
337 }
338 }
339 return rc;
340 }
341
342 static ssize_tcxx . . $ ssize_t# _modbus_rtu_recvcxx . . $ _modbus_rtu_recv(41755522a7cda552).(modbus_tcxx . . $ modbus_t# *ctx, uint8_tcxx . . $ uint8_t# *rsp, int rsp_length)
343 {
344 #if defined(_WIN32)
345 return win32_ser_read(&((modbus_rtu_t *) ctx->backend_data)->w_ser, rsp, rsp_length);
346 #else
347 return readcxx . . $ read(7e34d0a36f355389).(ctx->scxx . . $ _modbus#s., rsp, rsp_length);
348 #endif
349 }
350
351 static int _modbus_rtu_flushcxx . . $ _modbus_rtu_flush(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *);
352
353 static int _modbus_rtu_pre_check_confirmationcxx . . $ _modbus_rtu_pre_check_confirmation(23551e319a37d2dc).(modbus_tcxx . . $ modbus_t# *ctx,
354 const uint8_tcxx . . $ uint8_t# *req,
355 const uint8_tcxx . . $ uint8_t# *rsp,
356 int rsp_length)
357 {
358 /* Check responding slave is the slave we requested (except for broacast
359 * request) */
360 if (req[0] != rsp[0] && req[0] != MODBUS_BROADCAST_ADDRESScxx . . $ `src/modbus.h:76:9`!) {
361 if (ctx->debugcxx . . $ _modbus#debug.) {
362 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
363 "The responding slave %d isn't the requested slave %d\n",
364 rsp[0],
365 req[0]);
366 }
367 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EMBBADSLAVEcxx . . $ `src/modbus.h:151:9`!;
368 return -1;
369 } else {
370 return 0;
371 }
372 }
373
374 /* The check_crc16 function shall return 0 if the message is ignored and the
375 message length if the CRC is valid. Otherwise it shall return -1 and set
376 errno to EMBBADCRC. */
377 static int _modbus_rtu_check_integritycxx . . $ _modbus_rtu_check_integrity(b8ea090d50367a38).(modbus_tcxx . . $ modbus_t# *ctx, uint8_tcxx . . $ uint8_t# *msg, const int msg_length)
378 {
379 uint16_tcxx . . $ uint16_t# crc_calculated;
380 uint16_tcxx . . $ uint16_t# crc_received;
381 int slave = msg[0];
382
383 crc_calculated = crc16cxx . . $ crc16(cfcbdd83c4648882).(msg, msg_length - 2);
384 crc_received = (msg[msg_length - 1] << 8) | msg[msg_length - 2];
385
386 /* Check CRC of msg */
387 if (crc_calculated != crc_received) {
388 if (ctx->debugcxx . . $ _modbus#debug.) {
389 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
390 "ERROR CRC received 0x%0X != CRC calculated 0x%0X\n",
391 crc_received,
392 crc_calculated);
393 }
394
395 if (ctx->error_recoverycxx . . $ _modbus#error_recovery. & MODBUS_ERROR_RECOVERY_PROTOCOLcxx . . $ MODBUS_ERROR_RECOVERY_PROTOCOL.) {
396 _modbus_rtu_flushcxx . . $ _modbus_rtu_flush(c57adc5f48380e39).(ctx);
397 }
398 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EMBBADCRCcxx . . $ `src/modbus.h:146:9`!;
399 return -1;
400 }
401
402 /* Filter on the Modbus unit identifier (slave) in RTU mode */
403 if (slave != ctx->slavecxx . . $ _modbus#slave. && slave != MODBUS_BROADCAST_ADDRESScxx . . $ `src/modbus.h:76:9`!) {
404 if (ctx->debugcxx . . $ _modbus#debug.) {
405 printfcxx . . $ printf(581fddd72a8db8fc).("Request for slave %d ignored (not %d)\n", slave, ctx->slavecxx . . $ _modbus#slave.);
406 }
407 /* Following call to check_confirmation handles this error */
408 return 0;
409 }
410
411 return msg_length;
412 }
413
414 /* Sets up a serial port for RTU communications */
415 #if defined(_WIN32)
416 static int _modbus_rtu_connect(modbus_t *ctx)
417 {
418 DCB dcb;
419 modbus_rtu_t *ctx_rtu = ctx->backend_data;
420
421 if (ctx->debug) {
422 printf("Opening %s at %d bauds (%c, %d, %d)\n",
423 ctx_rtu->device,
424 ctx_rtu->baud,
425 ctx_rtu->parity,
426 ctx_rtu->data_bit,
427 ctx_rtu->stop_bit);
428 }
429
430 /* Some references here:
431 * http://msdn.microsoft.com/en-us/library/aa450602.aspx
432 */
433 win32_ser_init(&ctx_rtu->w_ser);
434
435 /* ctx_rtu->device should contain a string like "COMxx:" xx being a decimal
436 * number */
437 ctx_rtu->w_ser.fd = CreateFileA(
438 ctx_rtu->device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
439
440 /* Error checking */
441 if (ctx_rtu->w_ser.fd == INVALID_HANDLE_VALUE) {
442 if (ctx->debug) {
443 fprintf(stderr,
444 "ERROR Can't open the device %s (LastError %d)\n",
445 ctx_rtu->device,
446 (int) GetLastError());
447 }
448 return -1;
449 }
450
451 /* Save params */
452 ctx_rtu->old_dcb.DCBlength = sizeof(DCB);
453 if (!GetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb)) {
454 if (ctx->debug) {
455 fprintf(stderr,
456 "ERROR Error getting configuration (LastError %d)\n",
457 (int) GetLastError());
458 }
459 CloseHandle(ctx_rtu->w_ser.fd);
460 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
461 return -1;
462 }
463
464 /* Build new configuration (starting from current settings) */
465 dcb = ctx_rtu->old_dcb;
466
467 /* Speed setting */
468 dcb.BaudRate = ctx_rtu->baud;
469
470 /* Data bits */
471 switch (ctx_rtu->data_bit) {
472 case 5:
473 dcb.ByteSize = 5;
474 break;
475 case 6:
476 dcb.ByteSize = 6;
477 break;
478 case 7:
479 dcb.ByteSize = 7;
480 break;
481 case 8:
482 default:
483 dcb.ByteSize = 8;
484 break;
485 }
486
487 /* Stop bits */
488 if (ctx_rtu->stop_bit == 1)
489 dcb.StopBits = ONESTOPBIT;
490 else /* 2 */
491 dcb.StopBits = TWOSTOPBITS;
492
493 /* Parity */
494 if (ctx_rtu->parity == 'N') {
495 dcb.Parity = NOPARITY;
496 dcb.fParity = FALSE;
497 } else if (ctx_rtu->parity == 'E') {
498 dcb.Parity = EVENPARITY;
499 dcb.fParity = TRUE;
500 } else {
501 /* odd */
502 dcb.Parity = ODDPARITY;
503 dcb.fParity = TRUE;
504 }
505
506 /* Hardware handshaking left as default settings retrieved */
507
508 /* No software handshaking */
509 dcb.fTXContinueOnXoff = TRUE;
510 dcb.fOutX = FALSE;
511 dcb.fInX = FALSE;
512
513 /* Binary mode (it's the only supported on Windows anyway) */
514 dcb.fBinary = TRUE;
515
516 /* Don't want errors to be blocking */
517 dcb.fAbortOnError = FALSE;
518
519 /* Setup port */
520 if (!SetCommState(ctx_rtu->w_ser.fd, &dcb)) {
521 if (ctx->debug) {
522 fprintf(stderr,
523 "ERROR Error setting new configuration (LastError %d)\n",
524 (int) GetLastError());
525 }
526 CloseHandle(ctx_rtu->w_ser.fd);
527 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
528 return -1;
529 }
530
531 return 0;
532 }
533 #else
534
535 #ifndef HAVE_STRUCT_TERMIOS2cxx . . $ `config.h:101:9`!
536 static speed_t _get_termios_speed(int baud, int debug)
537 {
538 speed_t speed;
539
540 switch (baud) {
541 case 110:
542 speed = B110;
543 break;
544 case 300:
545 speed = B300;
546 break;
547 case 600:
548 speed = B600;
549 break;
550 case 1200:
551 speed = B1200;
552 break;
553 case 2400:
554 speed = B2400;
555 break;
556 case 4800:
557 speed = B4800;
558 break;
559 case 9600:
560 speed = B9600;
561 break;
562 case 19200:
563 speed = B19200;
564 break;
565 case 38400:
566 speed = B38400;
567 break;
568 #ifdef B57600
569 case 57600:
570 speed = B57600;
571 break;
572 #endif
573 #ifdef B115200
574 case 115200:
575 speed = B115200;
576 break;
577 #endif
578 #ifdef B230400
579 case 230400:
580 speed = B230400;
581 break;
582 #endif
583 #ifdef B460800
584 case 460800:
585 speed = B460800;
586 break;
587 #endif
588 #ifdef B500000
589 case 500000:
590 speed = B500000;
591 break;
592 #endif
593 #ifdef B576000
594 case 576000:
595 speed = B576000;
596 break;
597 #endif
598 #ifdef B921600
599 case 921600:
600 speed = B921600;
601 break;
602 #endif
603 #ifdef B1000000
604 case 1000000:
605 speed = B1000000;
606 break;
607 #endif
608 #ifdef B1152000
609 case 1152000:
610 speed = B1152000;
611 break;
612 #endif
613 #ifdef B1500000
614 case 1500000:
615 speed = B1500000;
616 break;
617 #endif
618 #ifdef B2500000
619 case 2500000:
620 speed = B2500000;
621 break;
622 #endif
623 #ifdef B3000000
624 case 3000000:
625 speed = B3000000;
626 break;
627 #endif
628 #ifdef B3500000
629 case 3500000:
630 speed = B3500000;
631 break;
632 #endif
633 #ifdef B4000000
634 case 4000000:
635 speed = B4000000;
636 break;
637 #endif
638 default:
639 speed = B9600;
640 if (debug) {
641 fprintf(stderr, "WARNING Unknown baud rate %d (B9600 used)\n", baud);
642 }
643 }
644
645 return speed;
646 }
647 #endif
648
649 /* POSIX */
650 static int _modbus_rtu_connectcxx . . $ _modbus_rtu_connect(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx)
651 {
652 #ifdef HAVE_STRUCT_TERMIOS2cxx . . $ `config.h:101:9`!
653 struct termios2cxx . . $ termios2# tios;
654 #else
655 speed_t speed;
656 struct termios tios;
657 #endif
658 int flags;
659 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
660
661 if (ctx->debugcxx . . $ _modbus#debug.) {
662 printfcxx . . $ printf(581fddd72a8db8fc).("Opening %s at %d bauds (%c, %d, %d)\n",
663 ctx_rtu->devicecxx . . $ _modbus_rtu#device.,
664 ctx_rtu->baudcxx . . $ _modbus_rtu#baud.,
665 ctx_rtu->paritycxx . . $ _modbus_rtu#parity.,
666 ctx_rtu->data_bitcxx . . $ _modbus_rtu#data_bit.,
667 ctx_rtu->stop_bitcxx . . $ _modbus_rtu#stop_bit.);
668 }
669
670 /* The O_NOCTTY flag tells UNIX that this program doesn't want
671 to be the "controlling terminal" for that port. If you
672 don't specify this then any input (such as keyboard abort
673 signals and so forth) will affect your process
674
675 Timeouts are ignored in canonical input mode or when the
676 NONBLOCK option is set on the file via open or fcntl */
677 flags = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_NOCTTYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:53:10`! | O_NONBLOCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:62:10`!;
678 #ifdef O_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:142:10`!
679 flags |= O_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:142:10`!;
680 #endif
681
682 ctx->scxx . . $ _modbus#s. = opencxx . . $ open(e156e3aa1e5c4ad1).(ctx_rtu->devicecxx . . $ _modbus_rtu#device., flags);
683 if (ctx->scxx . . $ _modbus#s. < 0) {
684 if (ctx->debugcxx . . $ _modbus#debug.) {
685 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
686 "ERROR Can't open the device %s (%s)\n",
687 ctx_rtu->devicecxx . . $ _modbus_rtu#device.,
688 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
689 }
690 return -1;
691 }
692
693 /* Save */
694 #ifdef HAVE_STRUCT_TERMIOS2cxx . . $ `config.h:101:9`!
695 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TCGETS2cxx . . $ `<external>/123502480044527469/ioctls.h:61:9`!, &ctx_rtu->old_tioscxx . . $ _modbus_rtu#old_tios.) < 0) {
696 #else
697 if (tcgetattr(ctx->s, &ctx_rtu->old_tios) < 0) {
698 #endif
699 if (ctx->debugcxx . . $ _modbus#debug.) {
700 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
701 "ERROR Can't save the termios settings of %s (%s)\n",
702 ctx_rtu->devicecxx . . $ _modbus_rtu#device.,
703 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
704 }
705 closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.);
706 ctx->scxx . . $ _modbus#s. = -1;
707 return -1;
708 }
709
710 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&tios, 0, sizeof(tios));
711
712 /* C_ISPEED Input baud (new interface)
713 C_OSPEED Output baud (new interface)
714 */
715
716 /* Set the baud rate */
717
718 #ifdef HAVE_STRUCT_TERMIOS2cxx . . $ `config.h:101:9`!
719 tios.c_cflagcxx . . $ termios2#c_cflag. |= BOTHERcxx . . $ `<external>/17989485081294502337/termios.h:59:10`!; /* Allow custom baud rate. */
720 tios.c_ispeedcxx . . $ termios2#c_ispeed. = ctx_rtu->baudcxx . . $ _modbus_rtu#baud.; /* Set input baud rate. */
721 tios.c_ospeedcxx . . $ termios2#c_ospeed. = ctx_rtu->baudcxx . . $ _modbus_rtu#baud.; /* Set output baud rate. */
722 #else
723 /*
724 On MacOS, constants of baud rates are equal to the integer in argument but
725 that's not the case under Linux so we have to find the corresponding
726 constant. Without termios2, the list of possible values is limited
727 (no 14400 for example).
728 */
729 if (9600 == B9600) {
730 speed = ctx_rtu->baud;
731 } else {
732 speed = _get_termios_speed(ctx_rtu->baud, ctx->debug);
733 }
734
735 if ((cfsetispeed(&tios, speed) < 0) || (cfsetospeed(&tios, speed) < 0)) {
736 close(ctx->s);
737 ctx->s = -1;
738 return -1;
739 }
740 #endif
741
742 /* C_CFLAG Control options
743 CLOCAL Local line - do not change "owner" of port
744 CREAD Enable receiver
745 */
746 tios.c_cflagcxx . . $ termios2#c_cflag. |= (CREADcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:30:9`! | CLOCALcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:34:9`!);
747 /* CSIZE, HUPCL, CRTSCTS (hardware flow control) */
748
749 /* Set data bits (5, 6, 7, 8 bits)
750 CSIZE Bit mask for data bits
751 */
752 tios.c_cflagcxx . . $ termios2#c_cflag. &= ~CSIZEcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:24:9`!;
753 switch (ctx_rtu->data_bitcxx . . $ _modbus_rtu#data_bit.) {
754 case 5:
755 tios.c_cflagcxx . . $ termios2#c_cflag. |= CS5cxx . . $ `<external>/934398879100599903/termios-c_cflag.h:25:11`!;
756 break;
757 case 6:
758 tios.c_cflagcxx . . $ termios2#c_cflag. |= CS6cxx . . $ `<external>/934398879100599903/termios-c_cflag.h:26:11`!;
759 break;
760 case 7:
761 tios.c_cflagcxx . . $ termios2#c_cflag. |= CS7cxx . . $ `<external>/934398879100599903/termios-c_cflag.h:27:11`!;
762 break;
763 case 8:
764 default:
765 tios.c_cflagcxx . . $ termios2#c_cflag. |= CS8cxx . . $ `<external>/934398879100599903/termios-c_cflag.h:28:11`!;
766 break;
767 }
768
769 /* Stop bit (1 or 2) */
770 if (ctx_rtu->stop_bitcxx . . $ _modbus_rtu#stop_bit. == 1)
771 tios.c_cflagcxx . . $ termios2#c_cflag. &= ~CSTOPBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:29:9`!;
772 else /* 2 */
773 tios.c_cflagcxx . . $ termios2#c_cflag. |= CSTOPBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:29:9`!;
774
775 /* PARENB Enable parity bit
776 PARODD Use odd parity instead of even */
777 if (ctx_rtu->paritycxx . . $ _modbus_rtu#parity. == 'N') {
778 /* None */
779 tios.c_cflagcxx . . $ termios2#c_cflag. &= ~PARENBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:31:9`!;
780 } else if (ctx_rtu->paritycxx . . $ _modbus_rtu#parity. == 'E') {
781 /* Even */
782 tios.c_cflagcxx . . $ termios2#c_cflag. |= PARENBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:31:9`!;
783 tios.c_cflagcxx . . $ termios2#c_cflag. &= ~PARODDcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:32:9`!;
784 } else {
785 /* Odd */
786 tios.c_cflagcxx . . $ termios2#c_cflag. |= PARENBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:31:9`!;
787 tios.c_cflagcxx . . $ termios2#c_cflag. |= PARODDcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:32:9`!;
788 }
789
790 /* Read the man page of termios if you need more information. */
791
792 /* This field isn't used on POSIX systems
793 tios.c_line = 0;
794 */
795
796 /* C_LFLAG Line options
797
798 ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals
799 ICANON Enable canonical input (else raw)
800 XCASE Map uppercase \lowercase (obsolete)
801 ECHO Enable echoing of input characters
802 ECHOE Echo erase character as BS-SP-BS
803 ECHOK Echo NL after kill character
804 ECHONL Echo NL
805 NOFLSH Disable flushing of input buffers after
806 interrupt or quit characters
807 IEXTEN Enable extended functions
808 ECHOCTL Echo control characters as ^char and delete as ~?
809 ECHOPRT Echo erased character as character erased
810 ECHOKE BS-SP-BS entire line on line kill
811 FLUSHO Output being flushed
812 PENDIN Retype pending input at next read or input char
813 TOSTOP Send SIGTTOU for background output
814
815 Canonical input is line-oriented. Input characters are put
816 into a buffer which can be edited interactively by the user
817 until a CR (carriage return) or LF (line feed) character is
818 received.
819
820 Raw input is unprocessed. Input characters are passed
821 through exactly as they are received, when they are
822 received. Generally you'll deselect the ICANON, ECHO,
823 ECHOE, and ISIG options when using raw input
824 */
825
826 /* Raw input */
827 tios.c_lflagcxx . . $ termios2#c_lflag. &= ~(ICANONcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:25:9`! | ECHOcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:29:9`! | ECHOEcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:30:9`! | ISIGcxx . . $ `<external>/2220421410900157355/termios-c_lflag.h:24:9`!);
828
829 /* C_IFLAG Input options
830
831 Constant Description
832 INPCK Enable parity check
833 IGNPAR Ignore parity errors
834 PARMRK Mark parity errors
835 ISTRIP Strip parity bits
836 IXON Enable software flow control (outgoing)
837 IXOFF Enable software flow control (incoming)
838 IXANY Allow any character to start flow again
839 IGNBRK Ignore break condition
840 BRKINT Send a SIGINT when a break condition is detected
841 INLCR Map NL to CR
842 IGNCR Ignore CR
843 ICRNL Map CR to NL
844 IUCLC Map uppercase to lowercase
845 IMAXBEL Echo BEL on input line too long
846 */
847 if (ctx_rtu->paritycxx . . $ _modbus_rtu#parity. == 'N') {
848 /* None */
849 tios.c_iflagcxx . . $ termios2#c_iflag. &= ~INPCKcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:28:9`!;
850 } else {
851 tios.c_iflagcxx . . $ termios2#c_iflag. |= INPCKcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:28:9`!;
852 }
853
854 /* Software flow control is disabled */
855 tios.c_iflagcxx . . $ termios2#c_iflag. &= ~(IXONcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:35:9`! | IXOFFcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:37:9`! | IXANYcxx . . $ `<external>/8770703639284775376/termios-c_iflag.h:36:9`!);
856
857 /* C_OFLAG Output options
858 OPOST Postprocess output (not set = raw output)
859 ONLCR Map NL to CR-NL
860
861 ONCLR ant others needs OPOST to be enabled
862 */
863
864 /* Raw output */
865 tios.c_oflagcxx . . $ termios2#c_oflag. &= ~OPOSTcxx . . $ `<external>/8246223416874974685/termios-c_oflag.h:24:9`!;
866
867 /* C_CC Control characters
868 VMIN Minimum number of characters to read
869 VTIME Time to wait for data (tenths of seconds)
870
871 UNIX serial interface drivers provide the ability to
872 specify character and packet timeouts. Two elements of the
873 c_cc array are used for timeouts: VMIN and VTIME. Timeouts
874 are ignored in canonical input mode or when the NONBLOCK
875 option is set on the file via open or fcntl.
876
877 VMIN specifies the minimum number of characters to read. If
878 it is set to 0, then the VTIME value specifies the time to
879 wait for every character read. Note that this does not mean
880 that a read call for N bytes will wait for N characters to
881 come in. Rather, the timeout will apply to the first
882 character and the read call will return the number of
883 characters immediately available (up to the number you
884 request).
885
886 If VMIN is non-zero, VTIME specifies the time to wait for
887 the first character read. If a character is read within the
888 time given, any read will block (wait) until all VMIN
889 characters are read. That is, once the first character is
890 read, the serial interface driver expects to receive an
891 entire packet of characters (VMIN bytes total). If no
892 character is read within the time allowed, then the call to
893 read returns 0. This method allows you to tell the serial
894 driver you need exactly N bytes and any read call will
895 return 0 or N bytes. However, the timeout only applies to
896 the first character read, so if for some reason the driver
897 misses one character inside the N byte packet then the read
898 call could block forever waiting for additional input
899 characters.
900
901 VTIME specifies the amount of time to wait for incoming
902 characters in tenths of seconds. If VTIME is set to 0 (the
903 default), reads will block (wait) indefinitely unless the
904 NONBLOCK option is set on the port with open or fcntl.
905 */
906 /* Unused because we use open with the NONBLOCK option */
907 tios.c_cccxx . . $ termios2#c_cc.[VMINcxx . . $ `<external>/5505922754558120548/termios-c_cc.h:30:9`!] = 0;
908 tios.c_cccxx . . $ termios2#c_cc.[VTIMEcxx . . $ `<external>/5505922754558120548/termios-c_cc.h:29:9`!] = 0;
909
910 #ifdef HAVE_STRUCT_TERMIOS2cxx . . $ `config.h:101:9`!
911 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TCSETS2cxx . . $ `<external>/123502480044527469/ioctls.h:62:9`!, &tios) < 0) {
912 closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.);
913 ctx->scxx . . $ _modbus#s. = -1;
914 return -1;
915 }
916 if (ctx->debugcxx . . $ _modbus#debug.) {
917 ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TCGETS2cxx . . $ `<external>/123502480044527469/ioctls.h:61:9`!, &tios);
918 if (tios.c_ispeedcxx . . $ termios2#c_ispeed. != (unsigned int)ctx_rtu->baudcxx . . $ _modbus_rtu#baud.) {
919 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
920 "WARNING Failed to set baud rate %d (%d used)\n",
921 ctx_rtu->baudcxx . . $ _modbus_rtu#baud.,
922 tios.c_ispeedcxx . . $ termios2#c_ispeed.);
923 }
924 }
925 #else
926 if (tcsetattr(ctx->s, TCSANOW, &tios) < 0) {
927 close(ctx->s);
928 ctx->s = -1;
929 return -1;
930 }
931 #endif
932
933 return 0;
934 }
935 #endif
936
937 // FIXME Temporary solution before rewriting Windows RTU backend
938 static unsigned int _modbus_rtu_is_connectedcxx . . $ _modbus_rtu_is_connected(4468d237960bb42).(modbus_tcxx . . $ modbus_t# *ctx)
939 {
940 #if defined(_WIN32)
941 modbus_rtu_t *ctx_rtu = ctx->backend_data;
942
943 /* Check if file handle is valid */
944 return ctx_rtu->w_ser.fd != INVALID_HANDLE_VALUE;
945 #else
946 return ctx->scxx . . $ _modbus#s. >= 0;
947 #endif
948 }
949
950 int modbus_rtu_set_serial_modecxx . . $ modbus_rtu_set_serial_mode(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int mode)
951 {
952 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
953 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
954 return -1;
955 }
956
957 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
958 #if HAVE_DECL_TIOCSRS485cxx . . $ `config.h:19:9`!
959 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
960 struct serial_rs485cxx . . $ serial_rs485# rs485conf;
961
962 if (mode == MODBUS_RTU_RS485cxx . . $ `src/modbus-rtu.h:23:9`!) {
963 // Get
964 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TIOCGRS485cxx . . $ `<external>/123502480044527469/ioctls.h:65:9`!, &rs485conf) < 0) {
965 return -1;
966 }
967 // Set
968 rs485conf.flagscxx . . $ serial_rs485#flags. |= SER_RS485_ENABLEDcxx . . $ `<external>/812923183512385953/serial.h:145:9`!;
969 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TIOCSRS485cxx . . $ `<external>/123502480044527469/ioctls.h:67:9`!, &rs485conf) < 0) {
970 return -1;
971 }
972
973 ctx_rtu->serial_modecxx . . $ _modbus_rtu#serial_mode. = MODBUS_RTU_RS485cxx . . $ `src/modbus-rtu.h:23:9`!;
974 return 0;
975 } else if (mode == MODBUS_RTU_RS232cxx . . $ `src/modbus-rtu.h:22:9`!) {
976 /* Turn off RS485 mode only if required */
977 if (ctx_rtu->serial_modecxx . . $ _modbus_rtu#serial_mode. == MODBUS_RTU_RS485cxx . . $ `src/modbus-rtu.h:23:9`!) {
978 /* The ioctl call is avoided because it can fail on some RS232 ports */
979 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TIOCGRS485cxx . . $ `<external>/123502480044527469/ioctls.h:65:9`!, &rs485conf) < 0) {
980 return -1;
981 }
982 rs485conf.flagscxx . . $ serial_rs485#flags. &= ~SER_RS485_ENABLEDcxx . . $ `<external>/812923183512385953/serial.h:145:9`!;
983 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TIOCSRS485cxx . . $ `<external>/123502480044527469/ioctls.h:67:9`!, &rs485conf) < 0) {
984 return -1;
985 }
986 }
987 ctx_rtu->serial_modecxx . . $ _modbus_rtu#serial_mode. = MODBUS_RTU_RS232cxx . . $ `src/modbus-rtu.h:22:9`!;
988 return 0;
989 }
990 #else
991 if (ctx->debug) {
992 fprintf(stderr, "This function isn't supported on your platform\n");
993 }
994 errno = ENOTSUP;
995 return -1;
996 #endif
997 }
998
999 /* Wrong backend and invalid mode specified */
1000 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1001 return -1;
1002 }
1003
1004 int modbus_rtu_get_serial_modecxx . . $ modbus_rtu_get_serial_mode(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx)
1005 {
1006 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1007 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1008 return -1;
1009 }
1010
1011 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
1012 #if HAVE_DECL_TIOCSRS485cxx . . $ `config.h:19:9`!
1013 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
1014 return ctx_rtu->serial_modecxx . . $ _modbus_rtu#serial_mode.;
1015 #else
1016 if (ctx->debug) {
1017 fprintf(stderr, "This function isn't supported on your platform\n");
1018 }
1019 errno = ENOTSUP;
1020 return -1;
1021 #endif
1022 } else {
1023 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1024 return -1;
1025 }
1026 }
1027
1028 int modbus_rtu_get_rtscxx . . $ modbus_rtu_get_rts(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx)
1029 {
1030 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1031 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1032 return -1;
1033 }
1034
1035 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
1036 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
1037 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
1038 return ctx_rtu->rtscxx . . $ _modbus_rtu#rts.;
1039 #else
1040 if (ctx->debug) {
1041 fprintf(stderr, "This function isn't supported on your platform\n");
1042 }
1043 errno = ENOTSUP;
1044 return -1;
1045 #endif
1046 } else {
1047 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1048 return -1;
1049 }
1050 }
1051
1052 int modbus_rtu_set_rtscxx . . $ modbus_rtu_set_rts(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int mode)
1053 {
1054 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1055 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1056 return -1;
1057 }
1058
1059 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
1060 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
1061 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
1062
1063 if (mode == MODBUS_RTU_RTS_NONEcxx . . $ `src/modbus-rtu.h:28:9`! || mode == MODBUS_RTU_RTS_UPcxx . . $ `src/modbus-rtu.h:29:9`! ||
1064 mode == MODBUS_RTU_RTS_DOWNcxx . . $ `src/modbus-rtu.h:30:9`!) {
1065 ctx_rtu->rtscxx . . $ _modbus_rtu#rts. = mode;
1066
1067 /* Set the RTS bit in order to not reserve the RS485 bus */
1068 ctx_rtu->set_rtscxx . . $ _modbus_rtu#set_rts.(ctx, ctx_rtu->rtscxx . . $ _modbus_rtu#rts. != MODBUS_RTU_RTS_UPcxx . . $ `src/modbus-rtu.h:29:9`!);
1069
1070 return 0;
1071 } else {
1072 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1073 return -1;
1074 }
1075 #else
1076 if (ctx->debug) {
1077 fprintf(stderr, "This function isn't supported on your platform\n");
1078 }
1079 errno = ENOTSUP;
1080 return -1;
1081 #endif
1082 }
1083 /* Wrong backend or invalid mode specified */
1084 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1085 return -1;
1086 }
1087
1088 int modbus_rtu_set_custom_rtscxx . . $ modbus_rtu_set_custom_rts(365d2f35a3816095).(modbus_tcxx . . $ modbus_t# *ctx, void (*set_rts)(modbus_tcxx . . $ modbus_t# *ctx, int on))
1089 {
1090 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || set_rts == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1091 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1092 return -1;
1093 }
1094
1095 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
1096 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
1097 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
1098 ctx_rtu->set_rtscxx . . $ _modbus_rtu#set_rts. = set_rts;
1099 return 0;
1100 #else
1101 if (ctx->debug) {
1102 fprintf(stderr, "This function isn't supported on your platform\n");
1103 }
1104 errno = ENOTSUP;
1105 return -1;
1106 #endif
1107 } else {
1108 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1109 return -1;
1110 }
1111 }
1112
1113 int modbus_rtu_get_rts_delaycxx . . $ modbus_rtu_get_rts_delay(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx)
1114 {
1115 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1116 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1117 return -1;
1118 }
1119
1120 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
1121 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
1122 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu;
1123 ctx_rtu = (modbus_rtu_tcxx . . $ modbus_rtu_t# *) ctx->backend_datacxx . . $ _modbus#backend_data.;
1124 return ctx_rtu->rts_delaycxx . . $ _modbus_rtu#rts_delay.;
1125 #else
1126 if (ctx->debug) {
1127 fprintf(stderr, "This function isn't supported on your platform\n");
1128 }
1129 errno = ENOTSUP;
1130 return -1;
1131 #endif
1132 } else {
1133 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1134 return -1;
1135 }
1136 }
1137
1138 int modbus_rtu_set_rts_delaycxx . . $ modbus_rtu_set_rts_delay(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int us)
1139 {
1140 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || us < 0) {
1141 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1142 return -1;
1143 }
1144
1145 if (ctx->backendcxx . . $ _modbus#backend.->backend_typecxx . . $ _modbus_backend#backend_type. == _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.) {
1146 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
1147 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu;
1148 ctx_rtu = (modbus_rtu_tcxx . . $ modbus_rtu_t# *) ctx->backend_datacxx . . $ _modbus#backend_data.;
1149 ctx_rtu->rts_delaycxx . . $ _modbus_rtu#rts_delay. = us;
1150 return 0;
1151 #else
1152 if (ctx->debug) {
1153 fprintf(stderr, "This function isn't supported on your platform\n");
1154 }
1155 errno = ENOTSUP;
1156 return -1;
1157 #endif
1158 } else {
1159 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1160 return -1;
1161 }
1162 }
1163
1164 static void _modbus_rtu_closecxx . . $ _modbus_rtu_close(24b4870c26038e9).(modbus_tcxx . . $ modbus_t# *ctx)
1165 {
1166 /* Restore line settings and close file descriptor in RTU mode */
1167 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu = ctx->backend_datacxx . . $ _modbus#backend_data.;
1168
1169 #if defined(_WIN32)
1170 if (ctx_rtu->w_ser.fd != INVALID_HANDLE_VALUE) {
1171 /* Revert settings */
1172 if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb) && ctx->debug) {
1173 fprintf(stderr,
1174 "ERROR Couldn't revert to configuration (LastError %d)\n",
1175 (int) GetLastError());
1176 }
1177
1178 if (!CloseHandle(ctx_rtu->w_ser.fd) && ctx->debug) {
1179 fprintf(stderr,
1180 "ERROR Error while closing handle (LastError %d)\n",
1181 (int) GetLastError());
1182 }
1183 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
1184 }
1185 #elif defined(HAVE_STRUCT_TERMIOS2cxx . . $ `config.h:101:9`!)
1186 if (ctx->scxx . . $ _modbus#s. >= 0) {
1187 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(ctx->scxx . . $ _modbus#s., TCSETS2cxx . . $ `<external>/123502480044527469/ioctls.h:62:9`!, &ctx_rtu->old_tioscxx . . $ _modbus_rtu#old_tios.) < 0 && ctx->debugcxx . . $ _modbus#debug.) {
1188 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
1189 "ERROR Can't restore the termios settings (%s)\n",
1190 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
1191 }
1192 closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.);
1193 ctx->scxx . . $ _modbus#s. = -1;
1194 }
1195 #else
1196 if (ctx->s >= 0) {
1197 if (tcsetattr(ctx->s, TCSANOW, &ctx_rtu->old_tios) < 0 && ctx->debug) {
1198 fprintf(stderr,
1199 "ERROR Can't restore the termios settings (%s)\n",
1200 strerror(errno));
1201 }
1202 close(ctx->s);
1203 ctx->s = -1;
1204 }
1205 #endif
1206 }
1207
1208 static int _modbus_rtu_flushcxx . . $ _modbus_rtu_flush(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx)
1209 {
1210 #if defined(_WIN32)
1211 modbus_rtu_t *ctx_rtu = ctx->backend_data;
1212 ctx_rtu->w_ser.n_bytes = 0;
1213 return (PurgeComm(ctx_rtu->w_ser.fd, PURGE_RXCLEAR) == FALSE);
1214 #else
1215 return tcflushcxx . . $ tcflush(9b79fb6aee4c0440).(ctx->scxx . . $ _modbus#s., TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!);
1216 #endif
1217 }
1218
1219 static int
1220 _modbus_rtu_selectcxx . . $ _modbus_rtu_select(da6c858c7786b0d5).(modbus_tcxx . . $ modbus_t# *ctx, fd_setcxx . . $ fd_set# *rset, struct timevalcxx . . $ timeval# *tv, int length_to_read)
1221 {
1222 int s_rc;
1223 #if defined(_WIN32)
1224 s_rc = win32_ser_select(
1225 &((modbus_rtu_t *) ctx->backend_data)->w_ser, length_to_read, tv);
1226 if (s_rc == 0) {
1227 errno = ETIMEDOUT;
1228 return -1;
1229 }
1230
1231 if (s_rc < 0) {
1232 return -1;
1233 }
1234 #else
1235 while ((s_rc = selectcxx . . $ select(6d24c7514c8a5b88).(ctx->scxx . . $ _modbus#s. + 1, rset, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, tv)) == -1) {
1236 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) {
1237 if (ctx->debugcxx . . $ _modbus#debug.) {
1238 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "A non blocked signal was caught\n");
1239 }
1240 /* Necessary after an error */
1241 FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(rset);
1242 if (ctx->scxx . . $ _modbus#s. < 0 || ctx->scxx . . $ _modbus#s. >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) {
1243 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1244 return -1;
1245 }
1246 FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(ctx->s, rset);
1247 } else {
1248 return -1;
1249 }
1250 }
1251
1252 if (s_rc == 0) {
1253 /* Timeout */
1254 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!;
1255 return -1;
1256 }
1257 #endif
1258
1259 return s_rc;
1260 }
1261
1262 static void _modbus_rtu_freecxx . . $ _modbus_rtu_free(24b4870c26038e9).(modbus_tcxx . . $ modbus_t# *ctx)
1263 {
1264 if (ctx->backend_datacxx . . $ _modbus#backend_data.) {
1265 freecxx . . $ free(5d22bdacc48458e8).(((modbus_rtu_tcxx . . $ modbus_rtu_t# *) ctx->backend_datacxx . . $ _modbus#backend_data.)->devicecxx . . $ _modbus_rtu#device.);
1266 freecxx . . $ free(5d22bdacc48458e8).(ctx->backend_datacxx . . $ _modbus#backend_data.);
1267 }
1268
1269 freecxx . . $ free(5d22bdacc48458e8).(ctx);
1270 }
1271
1272 // clang-format off
1273 const modbus_backend_tcxx . . $ modbus_backend_t# _modbus_rtu_backendcxx . . $ _modbus_rtu_backend. = {
1274 _MODBUS_BACKEND_TYPE_RTUcxx . . $ _MODBUS_BACKEND_TYPE_RTU.,
1275 _MODBUS_RTU_HEADER_LENGTHcxx . . $ `src/modbus-rtu-private.h:29:9`!,
1276 _MODBUS_RTU_CHECKSUM_LENGTHcxx . . $ `src/modbus-rtu-private.h:33:9`!,
1277 MODBUS_RTU_MAX_ADU_LENGTHcxx . . $ `src/modbus-rtu.h:17:9`!,
1278 _modbus_set_slavecxx . . $ _modbus_set_slave(2a7e0e283938d456).,
1279 _modbus_rtu_build_request_basiscxx . . $ _modbus_rtu_build_request_basis(abc53b0a0396e541).,
1280 _modbus_rtu_build_response_basiscxx . . $ _modbus_rtu_build_response_basis(acd9c6258f33b2f4).,
1281 _modbus_rtu_get_response_tidcxx . . $ _modbus_rtu_get_response_tid(b84ccf4e0bb14b06).,
1282 _modbus_rtu_send_msg_precxx . . $ _modbus_rtu_send_msg_pre(7405b416d0535413).,
1283 _modbus_rtu_sendcxx . . $ _modbus_rtu_send(ab2063bac0ea77a5).,
1284 _modbus_rtu_receivecxx . . $ _modbus_rtu_receive(744a031d86f39170).,
1285 _modbus_rtu_recvcxx . . $ _modbus_rtu_recv(41755522a7cda552).,
1286 _modbus_rtu_check_integritycxx . . $ _modbus_rtu_check_integrity(b8ea090d50367a38).,
1287 _modbus_rtu_pre_check_confirmationcxx . . $ _modbus_rtu_pre_check_confirmation(23551e319a37d2dc).,
1288 _modbus_rtu_connectcxx . . $ _modbus_rtu_connect(c57adc5f48380e39).,
1289 _modbus_rtu_is_connectedcxx . . $ _modbus_rtu_is_connected(4468d237960bb42).,
1290 _modbus_rtu_closecxx . . $ _modbus_rtu_close(24b4870c26038e9).,
1291 _modbus_rtu_flushcxx . . $ _modbus_rtu_flush(c57adc5f48380e39).,
1292 _modbus_rtu_selectcxx . . $ _modbus_rtu_select(da6c858c7786b0d5).,
1293 _modbus_rtu_freecxx . . $ _modbus_rtu_free(24b4870c26038e9).
1294 };
1295
1296 // clang-format on
1297
1298 modbus_tcxx . . $ modbus_t# *
1299 modbus_new_rtucxx . . $ modbus_new_rtu(3c01f1a556dc1f63).(const char *device, int baud, char parity, int data_bit, int stop_bit)
1300 {
1301 modbus_tcxx . . $ modbus_t# *ctx;
1302 modbus_rtu_tcxx . . $ modbus_rtu_t# *ctx_rtu;
1303
1304 /* Check device argument */
1305 if (device == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || *device == 0) {
1306 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "The device string is empty\n");
1307 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1308 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1309 }
1310
1311 /* Check baud argument */
1312 if (baud <= 0) {
1313 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "The baud rate value must be strictly positive\n");
1314 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1315 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1316 }
1317
1318 /* Check data_bit argument */
1319 if (data_bit < 5 || data_bit > 8) {
1320 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "The number of data bits must be between 5 and 8\n");
1321 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1322 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1323 }
1324
1325 /* Check stop_bit argument */
1326 if (stop_bit != 1 && stop_bit != 2) {
1327 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "The number of stop bits must be 1 or 2\n");
1328 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1329 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1330 }
1331
1332 ctx = (modbus_tcxx . . $ modbus_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(modbus_tcxx . . $ modbus_t#));
1333 if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1334 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1335 }
1336
1337 _modbus_init_commoncxx . . $ _modbus_init_common(24b4870c26038e9).(ctx);
1338 ctx->backendcxx . . $ _modbus#backend. = &_modbus_rtu_backendcxx . . $ _modbus_rtu_backend.;
1339 ctx->backend_datacxx . . $ _modbus#backend_data. = (modbus_rtu_tcxx . . $ modbus_rtu_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(modbus_rtu_tcxx . . $ modbus_rtu_t#));
1340 if (ctx->backend_datacxx . . $ _modbus#backend_data. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1341 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
1342 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
1343 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1344 }
1345 ctx_rtu = (modbus_rtu_tcxx . . $ modbus_rtu_t# *) ctx->backend_datacxx . . $ _modbus#backend_data.;
1346
1347 #if defined(_WIN32)
1348 ctx_rtu->w_ser.fd = INVALID_HANDLE_VALUE;
1349 #endif
1350
1351 /* Device name and \0 */
1352 ctx_rtu->devicecxx . . $ _modbus_rtu#device. = (char *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).((strlencxx . . $ strlen(751346f8406fd082).(device) + 1) * sizeof(char));
1353 if (ctx_rtu->devicecxx . . $ _modbus_rtu#device. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
1354 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
1355 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
1356 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1357 }
1358
1359 #if defined(_WIN32)
1360 strcpy_s(ctx_rtu->device, strlen(device) + 1, device);
1361 #else
1362 strcpycxx . . $ strcpy(b7b3a83273496486).(ctx_rtu->devicecxx . . $ _modbus_rtu#device., device);
1363 #endif
1364
1365 ctx_rtu->baudcxx . . $ _modbus_rtu#baud. = baud;
1366 if (parity == 'N' || parity == 'E' || parity == 'O') {
1367 ctx_rtu->paritycxx . . $ _modbus_rtu#parity. = parity;
1368 } else {
1369 modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx);
1370 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
1371 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
1372 }
1373 ctx_rtu->data_bitcxx . . $ _modbus_rtu#data_bit. = data_bit;
1374 ctx_rtu->stop_bitcxx . . $ _modbus_rtu#stop_bit. = stop_bit;
1375
1376 #if HAVE_DECL_TIOCSRS485cxx . . $ `config.h:19:9`!
1377 /* The RS232 mode has been set by default */
1378 ctx_rtu->serial_modecxx . . $ _modbus_rtu#serial_mode. = MODBUS_RTU_RS232cxx . . $ `src/modbus-rtu.h:22:9`!;
1379 #endif
1380
1381 #if HAVE_DECL_TIOCM_RTScxx . . $ `config.h:15:9`!
1382 /* The RTS use has been set by default */
1383 ctx_rtu->rtscxx . . $ _modbus_rtu#rts. = MODBUS_RTU_RTS_NONEcxx . . $ `src/modbus-rtu.h:28:9`!;
1384
1385 /* Calculate estimated time in micro second to send one byte */
1386 ctx_rtu->onebyte_timecxx . . $ _modbus_rtu#onebyte_time. =
1387 1000000 * (1 + data_bit + (parity == 'N' ? 0 : 1) + stop_bit) / baud;
1388
1389 /* The internal function is used by default to set RTS */
1390 ctx_rtu->set_rtscxx . . $ _modbus_rtu#set_rts. = _modbus_rtu_ioctl_rtscxx . . $ _modbus_rtu_ioctl_rts(28408cbef86636bb).;
1391
1392 /* The delay before and after transmission when toggling the RTS pin */
1393 ctx_rtu->rts_delaycxx . . $ _modbus_rtu#rts_delay. = ctx_rtu->onebyte_timecxx . . $ _modbus_rtu#onebyte_time.;
1394 #endif
1395
1396 ctx_rtu->confirmation_to_ignorecxx . . $ _modbus_rtu#confirmation_to_ignore. = FALSEcxx . . $ `src/modbus.h:47:9`!;
1397
1398 return ctx;
1399 }