static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) { #if defined(_WIN32) modbus_rtu_t *ctx_rtu = ctx->backend_data; DWORD n_bytes = 0; return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1; #else #if HAVE_DECL_TIOCM_RTS modbus_rtu_t *ctx_rtu = ctx->backend_data; if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) { ssize_t size; if (ctx->debug) { fprintf(stderr, "Sending request using RTS signal\n"); } _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts == MODBUS_RTU_RTS_UP); usleep(_MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); size = write(ctx->s, req, req_length); usleep(ctx_rtu->onebyte_time * req_length + _MODBUS_RTU_TIME_BETWEEN_RTS_SWITCH); _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP); return size; } else { #endif return write(ctx->s, req, req_length); #if HAVE_DECL_TIOCM_RTS } #endif #endif }
static ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length) { #if defined(_WIN32) modbus_rtu_t *ctx_rtu = ctx->backend_data; DWORD n_bytes = 0; return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? (ssize_t)n_bytes : -1; #else #if HAVE_DECL_TIOCM_RTS modbus_rtu_t *ctx_rtu = ctx->backend_data; if (ctx_rtu->rts != MODBUS_RTU_RTS_NONE) { ssize_t size; if (ctx->debug) { fprintf(stderr, "Sending request using RTS signal\n"); } GPIO_SET = 1 << 24; /* Turn on yellow LED to signal traffic */ _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts == MODBUS_RTU_RTS_UP); usleep(rts_delay_before); size = write(ctx->s, req, req_length); usleep(ctx_rtu->onebyte_time * req_length + rts_delay_after); _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP); GPIO_CLR = 1 << 24; /* Turn off yellow LED */ return size; } else { #endif ssize_t size; GPIO_SET = 1 << 17; /* Set p1-11 high to set DE high while sending */ GPIO_SET = 1 << 24; /* Turn on yellow LED to signal traffic */ usleep(1000); /* Temp: sleep 1ms */ size = write(ctx->s, req, req_length); modbus_flush(ctx); usleep(1000); /* Temp: sleep 1 ms */ GPIO_CLR = 1 << 17; /* Set p1-11 low to allow response */ GPIO_CLR = 1 << 24; /* Message sent, turn off yellow LED */ return size; #if HAVE_DECL_TIOCM_RTS } #endif #endif }
int modbus_rtu_set_rts(modbus_t *ctx, int mode) { if (ctx == NULL) { errno = EINVAL; return -1; } if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) { #if HAVE_DECL_TIOCM_RTS modbus_rtu_t *ctx_rtu = ctx->backend_data; if (mode == MODBUS_RTU_RTS_NONE || mode == MODBUS_RTU_RTS_UP || mode == MODBUS_RTU_RTS_DOWN) { ctx_rtu->rts = mode; /* Set the RTS bit in order to not reserve the RS485 bus */ _modbus_rtu_ioctl_rts(ctx->s, ctx_rtu->rts != MODBUS_RTU_RTS_UP); return 0; } else { errno = EINVAL; return -1; } #else if (ctx->debug) { fprintf(stderr, "This function isn't supported on your platform\n"); } errno = ENOTSUP; return -1; #endif } /* Wrong backend or invalid mode specified */ errno = EINVAL; return -1; }