| 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_TERMIOS2) |
| 16 | #include <sys/ioctl.h> |
| 17 | #endif |
| 18 | #include "modbus-private.h" |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include "modbus-rtu-private.h" |
| 22 | #include "modbus-rtu.h" |
| 23 | |
| 24 | #if HAVE_DECL_TIOCSRS485 || HAVE_DECL_TIOCM_RTS |
| 25 | #include <sys/ioctl.h> |
| 26 | #endif |
| 27 | |
| 28 | #if HAVE_DECL_TIOCSRS485 |
| 29 | #include <linux/serial.h> |
| 30 | #endif |
| 31 | |
| 32 | /* Table of CRC values for high-order byte */ |
| 33 | static const uint8_t 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_t 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_slave(modbus_t *ctx, int slave) |
| 79 | { |
| 80 | int max_slave = (ctx->quirks & MODBUS_QUIRK_MAX_SLAVE) ? 255 : 247; |
| 81 | |
| 82 | /* Broadcast address is 0 (MODBUS_BROADCAST_ADDRESS) */ |
| 83 | if (slave >= 0 && slave <= max_slave) { |
| 84 | ctx->slave = slave; |
| 85 | } else { |
| 86 | errno = EINVAL; |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* Builds a RTU request header */ |
| 94 | static int _modbus_rtu_build_request_basis( |
| 95 | modbus_t *ctx, int function, int addr, int nb, uint8_t *req) |
| 96 | { |
| 97 | assert(ctx->slave != -1); |
| 98 | req[0] = ctx->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_LENGTH; |
| 106 | } |
| 107 | |
| 108 | /* Builds a RTU response header */ |
| 109 | static int _modbus_rtu_build_response_basis(sft_t *sft, 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->slave; |
| 114 | rsp[1] = sft->function; |
| 115 | |
| 116 | return _MODBUS_RTU_PRESET_RSP_LENGTH; |
| 117 | } |
| 118 | |
| 119 | static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length) |
| 120 | { |
| 121 | uint8_t crc_hi = 0xFF; /* high CRC byte initialized */ |
| 122 | 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_hi[i]; |
| 129 | crc_hi = table_crc_lo[i]; |
| 130 | } |
| 131 | |
| 132 | return (crc_hi << 8 | crc_lo); |
| 133 | } |
| 134 | |
| 135 | static int _modbus_rtu_get_response_tid(const uint8_t *req) |
| 136 | { |
| 137 | /* No TID */ |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length) |
| 142 | { |
| 143 | uint16_t crc = crc16(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_RTS |
| 242 | static void _modbus_rtu_ioctl_rts(modbus_t *ctx, int on) |
| 243 | { |
| 244 | int fd = ctx->s; |
| 245 | int flags; |
| 246 | |
| 247 | if (ioctl(fd, TIOCMGET, &flags) == -1) { |
| 248 | if (ctx->debug) { |
| 249 | fprintf(stderr, "ERROR Can't get RTS line state (%s)\n", strerror(errno)); |
| 250 | } |
| 251 | return; |
| 252 | } |
| 253 | if (on) { |
| 254 | flags |= TIOCM_RTS; |
| 255 | } else { |
| 256 | flags &= ~TIOCM_RTS; |
| 257 | } |
| 258 | if (ioctl(fd, TIOCMSET, &flags) == -1) { |
| 259 | if (ctx->debug) { |
| 260 | fprintf(stderr, "ERROR Can't set RTS line state (%s)\n", strerror(errno)); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | static ssize_t _modbus_rtu_send(modbus_t *ctx, const 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_RTS |
| 276 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 277 | if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) { |
| 278 | ssize_t size; |
| 279 | |
| 280 | if (ctx->debug) { |
| 281 | fprintf(stderr, "Sending request using RTS signal\n"); |
| 282 | } |
| 283 | |
| 284 | uint64_t total_delay; |
| 285 | |
| 286 | ctx_rtu->set_rts(ctx, ctx_rtu->rts == MODBUS_RTU_RTS_UP); |
| 287 | usleep(ctx_rtu->rts_delay); |
| 288 | |
| 289 | size = write(ctx->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_t) ctx_rtu->onebyte_time * (uint64_t) req_length + |
| 295 | (uint64_t) ctx_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_t delay = |
| 303 | (total_delay > 999999ULL) ? 999999 : (useconds_t) total_delay; |
| 304 | usleep(delay); |
| 305 | total_delay -= delay; |
| 306 | } |
| 307 | ctx_rtu->set_rts(ctx, ctx_rtu->rts != MODBUS_RTU_RTS_UP); |
| 308 | |
| 309 | return size; |
| 310 | } else { |
| 311 | #endif |
| 312 | return write(ctx->s, req, req_length); |
| 313 | #if HAVE_DECL_TIOCM_RTS |
| 314 | } |
| 315 | #endif |
| 316 | #endif |
| 317 | } |
| 318 | |
| 319 | static int _modbus_rtu_receive(modbus_t *ctx, uint8_t *req) |
| 320 | { |
| 321 | int rc; |
| 322 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 323 | |
| 324 | if (ctx_rtu->confirmation_to_ignore) { |
| 325 | (void) _modbus_receive_msg(ctx, req, MSG_CONFIRMATION); |
| 326 | /* Ignore errors and reset the flag */ |
| 327 | ctx_rtu->confirmation_to_ignore = FALSE; |
| 328 | rc = 0; |
| 329 | if (ctx->debug) { |
| 330 | printf("Confirmation to ignore\n"); |
| 331 | } |
| 332 | } else { |
| 333 | rc = _modbus_receive_msg(ctx, req, MSG_INDICATION); |
| 334 | if (rc == 0) { |
| 335 | /* The next expected message is a confirmation to ignore */ |
| 336 | ctx_rtu->confirmation_to_ignore = TRUE; |
| 337 | } |
| 338 | } |
| 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | static ssize_t _modbus_rtu_recv(modbus_t *ctx, 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 read(ctx->s, rsp, rsp_length); |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | static int _modbus_rtu_flush(modbus_t *); |
| 352 | |
| 353 | static int _modbus_rtu_pre_check_confirmation(modbus_t *ctx, |
| 354 | const uint8_t *req, |
| 355 | const 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_ADDRESS) { |
| 361 | if (ctx->debug) { |
| 362 | fprintf(stderr, |
| 363 | "The responding slave %d isn't the requested slave %d\n", |
| 364 | rsp[0], |
| 365 | req[0]); |
| 366 | } |
| 367 | errno = EMBBADSLAVE; |
| 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_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length) |
| 378 | { |
| 379 | uint16_t crc_calculated; |
| 380 | uint16_t crc_received; |
| 381 | int slave = msg[0]; |
| 382 | |
| 383 | crc_calculated = crc16(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->debug) { |
| 389 | fprintf(stderr, |
| 390 | "ERROR CRC received 0x%0X != CRC calculated 0x%0X\n", |
| 391 | crc_received, |
| 392 | crc_calculated); |
| 393 | } |
| 394 | |
| 395 | if (ctx->error_recovery & MODBUS_ERROR_RECOVERY_PROTOCOL) { |
| 396 | _modbus_rtu_flush(ctx); |
| 397 | } |
| 398 | errno = EMBBADCRC; |
| 399 | return -1; |
| 400 | } |
| 401 | |
| 402 | /* Filter on the Modbus unit identifier (slave) in RTU mode */ |
| 403 | if (slave != ctx->slave && slave != MODBUS_BROADCAST_ADDRESS) { |
| 404 | if (ctx->debug) { |
| 405 | printf("Request for slave %d ignored (not %d)\n", slave, ctx->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_TERMIOS2 |
| 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_connect(modbus_t *ctx) |
| 651 | { |
| 652 | #ifdef HAVE_STRUCT_TERMIOS2 |
| 653 | struct termios2 tios; |
| 654 | #else |
| 655 | speed_t speed; |
| 656 | struct termios tios; |
| 657 | #endif |
| 658 | int flags; |
| 659 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 660 | |
| 661 | if (ctx->debug) { |
| 662 | printf("Opening %s at %d bauds (%c, %d, %d)\n", |
| 663 | ctx_rtu->device, |
| 664 | ctx_rtu->baud, |
| 665 | ctx_rtu->parity, |
| 666 | ctx_rtu->data_bit, |
| 667 | ctx_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_RDWR | O_NOCTTY | O_NONBLOCK; |
| 678 | #ifdef O_CLOEXEC |
| 679 | flags |= O_CLOEXEC; |
| 680 | #endif |
| 681 | |
| 682 | ctx->s = open(ctx_rtu->device, flags); |
| 683 | if (ctx->s < 0) { |
| 684 | if (ctx->debug) { |
| 685 | fprintf(stderr, |
| 686 | "ERROR Can't open the device %s (%s)\n", |
| 687 | ctx_rtu->device, |
| 688 | strerror(errno)); |
| 689 | } |
| 690 | return -1; |
| 691 | } |
| 692 | |
| 693 | /* Save */ |
| 694 | #ifdef HAVE_STRUCT_TERMIOS2 |
| 695 | if (ioctl(ctx->s, TCGETS2, &ctx_rtu->old_tios) < 0) { |
| 696 | #else |
| 697 | if (tcgetattr(ctx->s, &ctx_rtu->old_tios) < 0) { |
| 698 | #endif |
| 699 | if (ctx->debug) { |
| 700 | fprintf(stderr, |
| 701 | "ERROR Can't save the termios settings of %s (%s)\n", |
| 702 | ctx_rtu->device, |
| 703 | strerror(errno)); |
| 704 | } |
| 705 | close(ctx->s); |
| 706 | ctx->s = -1; |
| 707 | return -1; |
| 708 | } |
| 709 | |
| 710 | memset(&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_TERMIOS2 |
| 719 | tios.c_cflag |= BOTHER; /* Allow custom baud rate. */ |
| 720 | tios.c_ispeed = ctx_rtu->baud; /* Set input baud rate. */ |
| 721 | tios.c_ospeed = ctx_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_cflag |= (CREAD | CLOCAL); |
| 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_cflag &= ~CSIZE; |
| 753 | switch (ctx_rtu->data_bit) { |
| 754 | case 5: |
| 755 | tios.c_cflag |= CS5; |
| 756 | break; |
| 757 | case 6: |
| 758 | tios.c_cflag |= CS6; |
| 759 | break; |
| 760 | case 7: |
| 761 | tios.c_cflag |= CS7; |
| 762 | break; |
| 763 | case 8: |
| 764 | default: |
| 765 | tios.c_cflag |= CS8; |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | /* Stop bit (1 or 2) */ |
| 770 | if (ctx_rtu->stop_bit == 1) |
| 771 | tios.c_cflag &= ~CSTOPB; |
| 772 | else /* 2 */ |
| 773 | tios.c_cflag |= CSTOPB; |
| 774 | |
| 775 | /* PARENB Enable parity bit |
| 776 | PARODD Use odd parity instead of even */ |
| 777 | if (ctx_rtu->parity == 'N') { |
| 778 | /* None */ |
| 779 | tios.c_cflag &= ~PARENB; |
| 780 | } else if (ctx_rtu->parity == 'E') { |
| 781 | /* Even */ |
| 782 | tios.c_cflag |= PARENB; |
| 783 | tios.c_cflag &= ~PARODD; |
| 784 | } else { |
| 785 | /* Odd */ |
| 786 | tios.c_cflag |= PARENB; |
| 787 | tios.c_cflag |= PARODD; |
| 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_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 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->parity == 'N') { |
| 848 | /* None */ |
| 849 | tios.c_iflag &= ~INPCK; |
| 850 | } else { |
| 851 | tios.c_iflag |= INPCK; |
| 852 | } |
| 853 | |
| 854 | /* Software flow control is disabled */ |
| 855 | tios.c_iflag &= ~(IXON | IXOFF | IXANY); |
| 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_oflag &= ~OPOST; |
| 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_cc[VMIN] = 0; |
| 908 | tios.c_cc[VTIME] = 0; |
| 909 | |
| 910 | #ifdef HAVE_STRUCT_TERMIOS2 |
| 911 | if (ioctl(ctx->s, TCSETS2, &tios) < 0) { |
| 912 | close(ctx->s); |
| 913 | ctx->s = -1; |
| 914 | return -1; |
| 915 | } |
| 916 | if (ctx->debug) { |
| 917 | ioctl(ctx->s, TCGETS2, &tios); |
| 918 | if (tios.c_ispeed != (unsigned int)ctx_rtu->baud) { |
| 919 | fprintf(stderr, |
| 920 | "WARNING Failed to set baud rate %d (%d used)\n", |
| 921 | ctx_rtu->baud, |
| 922 | tios.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_connected(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->s >= 0; |
| 947 | #endif |
| 948 | } |
| 949 | |
| 950 | int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode) |
| 951 | { |
| 952 | if (ctx == NULL) { |
| 953 | errno = EINVAL; |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 958 | #if HAVE_DECL_TIOCSRS485 |
| 959 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 960 | struct serial_rs485 rs485conf; |
| 961 | |
| 962 | if (mode == MODBUS_RTU_RS485) { |
| 963 | // Get |
| 964 | if (ioctl(ctx->s, TIOCGRS485, &rs485conf) < 0) { |
| 965 | return -1; |
| 966 | } |
| 967 | // Set |
| 968 | rs485conf.flags |= SER_RS485_ENABLED; |
| 969 | if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) { |
| 970 | return -1; |
| 971 | } |
| 972 | |
| 973 | ctx_rtu->serial_mode = MODBUS_RTU_RS485; |
| 974 | return 0; |
| 975 | } else if (mode == MODBUS_RTU_RS232) { |
| 976 | /* Turn off RS485 mode only if required */ |
| 977 | if (ctx_rtu->serial_mode == MODBUS_RTU_RS485) { |
| 978 | /* The ioctl call is avoided because it can fail on some RS232 ports */ |
| 979 | if (ioctl(ctx->s, TIOCGRS485, &rs485conf) < 0) { |
| 980 | return -1; |
| 981 | } |
| 982 | rs485conf.flags &= ~SER_RS485_ENABLED; |
| 983 | if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) { |
| 984 | return -1; |
| 985 | } |
| 986 | } |
| 987 | ctx_rtu->serial_mode = MODBUS_RTU_RS232; |
| 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 | errno = EINVAL; |
| 1001 | return -1; |
| 1002 | } |
| 1003 | |
| 1004 | int modbus_rtu_get_serial_mode(modbus_t *ctx) |
| 1005 | { |
| 1006 | if (ctx == NULL) { |
| 1007 | errno = EINVAL; |
| 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1012 | #if HAVE_DECL_TIOCSRS485 |
| 1013 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 1014 | return ctx_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 | errno = EINVAL; |
| 1024 | return -1; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | int modbus_rtu_get_rts(modbus_t *ctx) |
| 1029 | { |
| 1030 | if (ctx == NULL) { |
| 1031 | errno = EINVAL; |
| 1032 | return -1; |
| 1033 | } |
| 1034 | |
| 1035 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1036 | #if HAVE_DECL_TIOCM_RTS |
| 1037 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 1038 | return ctx_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 | errno = EINVAL; |
| 1048 | return -1; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | int modbus_rtu_set_rts(modbus_t *ctx, int mode) |
| 1053 | { |
| 1054 | if (ctx == NULL) { |
| 1055 | errno = EINVAL; |
| 1056 | return -1; |
| 1057 | } |
| 1058 | |
| 1059 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1060 | #if HAVE_DECL_TIOCM_RTS |
| 1061 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 1062 | |
| 1063 | if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP || |
| 1064 | mode == MODBUS_RTU_RTS_DOWN) { |
| 1065 | ctx_rtu->rts = mode; |
| 1066 | |
| 1067 | /* Set the RTS bit in order to not reserve the RS485 bus */ |
| 1068 | ctx_rtu->set_rts(ctx, ctx_rtu->rts != MODBUS_RTU_RTS_UP); |
| 1069 | |
| 1070 | return 0; |
| 1071 | } else { |
| 1072 | errno = EINVAL; |
| 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 | errno = EINVAL; |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
| 1088 | int modbus_rtu_set_custom_rts(modbus_t *ctx, void (*set_rts)(modbus_t *ctx, int on)) |
| 1089 | { |
| 1090 | if (ctx == NULL || set_rts == NULL) { |
| 1091 | errno = EINVAL; |
| 1092 | return -1; |
| 1093 | } |
| 1094 | |
| 1095 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1096 | #if HAVE_DECL_TIOCM_RTS |
| 1097 | modbus_rtu_t *ctx_rtu = ctx->backend_data; |
| 1098 | ctx_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 | errno = EINVAL; |
| 1109 | return -1; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | int modbus_rtu_get_rts_delay(modbus_t *ctx) |
| 1114 | { |
| 1115 | if (ctx == NULL) { |
| 1116 | errno = EINVAL; |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1121 | #if HAVE_DECL_TIOCM_RTS |
| 1122 | modbus_rtu_t *ctx_rtu; |
| 1123 | ctx_rtu = (modbus_rtu_t *) ctx->backend_data; |
| 1124 | return ctx_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 | errno = EINVAL; |
| 1134 | return -1; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | int modbus_rtu_set_rts_delay(modbus_t *ctx, int us) |
| 1139 | { |
| 1140 | if (ctx == NULL || us < 0) { |
| 1141 | errno = EINVAL; |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
| 1145 | if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { |
| 1146 | #if HAVE_DECL_TIOCM_RTS |
| 1147 | modbus_rtu_t *ctx_rtu; |
| 1148 | ctx_rtu = (modbus_rtu_t *) ctx->backend_data; |
| 1149 | ctx_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 | errno = EINVAL; |
| 1160 | return -1; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | static void _modbus_rtu_close(modbus_t *ctx) |
| 1165 | { |
| 1166 | /* Restore line settings and close file descriptor in RTU mode */ |
| 1167 | modbus_rtu_t *ctx_rtu = ctx->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_TERMIOS2) |
| 1186 | if (ctx->s >= 0) { |
| 1187 | if (ioctl(ctx->s, TCSETS2, &ctx_rtu->old_tios) < 0 && ctx->debug) { |
| 1188 | fprintf(stderr, |
| 1189 | "ERROR Can't restore the termios settings (%s)\n", |
| 1190 | strerror(errno)); |
| 1191 | } |
| 1192 | close(ctx->s); |
| 1193 | ctx->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_flush(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 tcflush(ctx->s, TCIOFLUSH); |
| 1216 | #endif |
| 1217 | } |
| 1218 | |
| 1219 | static int |
| 1220 | _modbus_rtu_select(modbus_t *ctx, fd_set *rset, struct 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 = select(ctx->s + 1, rset, NULL, NULL, tv)) == -1) { |
| 1236 | if (errno == EINTR) { |
| 1237 | if (ctx->debug) { |
| 1238 | fprintf(stderr, "A non blocked signal was caught\n"); |
| 1239 | } |
| 1240 | /* Necessary after an error */ |
| 1241 | FD_ZERO(rset); |
| 1242 | if (ctx->s < 0 || ctx->s >= FD_SETSIZE) { |
| 1243 | errno = EINVAL; |
| 1244 | return -1; |
| 1245 | } |
| 1246 | FD_SET(ctx->s, rset); |
| 1247 | } else { |
| 1248 | return -1; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | if (s_rc == 0) { |
| 1253 | /* Timeout */ |
| 1254 | errno = ETIMEDOUT; |
| 1255 | return -1; |
| 1256 | } |
| 1257 | #endif |
| 1258 | |
| 1259 | return s_rc; |
| 1260 | } |
| 1261 | |
| 1262 | static void _modbus_rtu_free(modbus_t *ctx) |
| 1263 | { |
| 1264 | if (ctx->backend_data) { |
| 1265 | free(((modbus_rtu_t *) ctx->backend_data)->device); |
| 1266 | free(ctx->backend_data); |
| 1267 | } |
| 1268 | |
| 1269 | free(ctx); |
| 1270 | } |
| 1271 | |
| 1272 | // clang-format off |
| 1273 | const modbus_backend_t _modbus_rtu_backend = { |
| 1274 | _MODBUS_BACKEND_TYPE_RTU, |
| 1275 | _MODBUS_RTU_HEADER_LENGTH, |
| 1276 | _MODBUS_RTU_CHECKSUM_LENGTH, |
| 1277 | MODBUS_RTU_MAX_ADU_LENGTH, |
| 1278 | _modbus_set_slave, |
| 1279 | _modbus_rtu_build_request_basis, |
| 1280 | _modbus_rtu_build_response_basis, |
| 1281 | _modbus_rtu_get_response_tid, |
| 1282 | _modbus_rtu_send_msg_pre, |
| 1283 | _modbus_rtu_send, |
| 1284 | _modbus_rtu_receive, |
| 1285 | _modbus_rtu_recv, |
| 1286 | _modbus_rtu_check_integrity, |
| 1287 | _modbus_rtu_pre_check_confirmation, |
| 1288 | _modbus_rtu_connect, |
| 1289 | _modbus_rtu_is_connected, |
| 1290 | _modbus_rtu_close, |
| 1291 | _modbus_rtu_flush, |
| 1292 | _modbus_rtu_select, |
| 1293 | _modbus_rtu_free |
| 1294 | }; |
| 1295 | |
| 1296 | // clang-format on |
| 1297 | |
| 1298 | modbus_t * |
| 1299 | modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit) |
| 1300 | { |
| 1301 | modbus_t *ctx; |
| 1302 | modbus_rtu_t *ctx_rtu; |
| 1303 | |
| 1304 | /* Check device argument */ |
| 1305 | if (device == NULL || *device == 0) { |
| 1306 | fprintf(stderr, "The device string is empty\n"); |
| 1307 | errno = EINVAL; |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | /* Check baud argument */ |
| 1312 | if (baud <= 0) { |
| 1313 | fprintf(stderr, "The baud rate value must be strictly positive\n"); |
| 1314 | errno = EINVAL; |
| 1315 | return NULL; |
| 1316 | } |
| 1317 | |
| 1318 | /* Check data_bit argument */ |
| 1319 | if (data_bit < 5 || data_bit > 8) { |
| 1320 | fprintf(stderr, "The number of data bits must be between 5 and 8\n"); |
| 1321 | errno = EINVAL; |
| 1322 | return NULL; |
| 1323 | } |
| 1324 | |
| 1325 | /* Check stop_bit argument */ |
| 1326 | if (stop_bit != 1 && stop_bit != 2) { |
| 1327 | fprintf(stderr, "The number of stop bits must be 1 or 2\n"); |
| 1328 | errno = EINVAL; |
| 1329 | return NULL; |
| 1330 | } |
| 1331 | |
| 1332 | ctx = (modbus_t *) malloc(sizeof(modbus_t)); |
| 1333 | if (ctx == NULL) { |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
| 1337 | _modbus_init_common(ctx); |
| 1338 | ctx->backend = &_modbus_rtu_backend; |
| 1339 | ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); |
| 1340 | if (ctx->backend_data == NULL) { |
| 1341 | modbus_free(ctx); |
| 1342 | errno = ENOMEM; |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | ctx_rtu = (modbus_rtu_t *) ctx->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->device = (char *) malloc((strlen(device) + 1) * sizeof(char)); |
| 1353 | if (ctx_rtu->device == NULL) { |
| 1354 | modbus_free(ctx); |
| 1355 | errno = ENOMEM; |
| 1356 | return NULL; |
| 1357 | } |
| 1358 | |
| 1359 | #if defined(_WIN32) |
| 1360 | strcpy_s(ctx_rtu->device, strlen(device) + 1, device); |
| 1361 | #else |
| 1362 | strcpy(ctx_rtu->device, device); |
| 1363 | #endif |
| 1364 | |
| 1365 | ctx_rtu->baud = baud; |
| 1366 | if (parity == 'N' || parity == 'E' || parity == 'O') { |
| 1367 | ctx_rtu->parity = parity; |
| 1368 | } else { |
| 1369 | modbus_free(ctx); |
| 1370 | errno = EINVAL; |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | ctx_rtu->data_bit = data_bit; |
| 1374 | ctx_rtu->stop_bit = stop_bit; |
| 1375 | |
| 1376 | #if HAVE_DECL_TIOCSRS485 |
| 1377 | /* The RS232 mode has been set by default */ |
| 1378 | ctx_rtu->serial_mode = MODBUS_RTU_RS232; |
| 1379 | #endif |
| 1380 | |
| 1381 | #if HAVE_DECL_TIOCM_RTS |
| 1382 | /* The RTS use has been set by default */ |
| 1383 | ctx_rtu->rts = MODBUS_RTU_RTS_NONE; |
| 1384 | |
| 1385 | /* Calculate estimated time in micro second to send one byte */ |
| 1386 | ctx_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_rts = _modbus_rtu_ioctl_rts; |
| 1391 | |
| 1392 | /* The delay before and after transmission when toggling the RTS pin */ |
| 1393 | ctx_rtu->rts_delay = ctx_rtu->onebyte_time; |
| 1394 | #endif |
| 1395 | |
| 1396 | ctx_rtu->confirmation_to_ignore = FALSE; |
| 1397 | |
| 1398 | return ctx; |
| 1399 | } |