stephane/libmodbus — Issue #36: modbus_rtu_set_serial_mode ERROR
Author: mariof155
Created: 2011-11-03 16:31:56+00:00
State: open

First of all, sorry for my English.

I always get an error when try to use that function. What can I do? Every call I do (modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485 (or RS232)) returns -1. Bad file descriptor.

I've followed the examples but I cannot go throught that issue.

Anyone could help me?

int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
{
if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
#if HAVE_DECL_TIOCSRS485
modbus_rtu_t *ctx_rtu = ctx->backend_data;
struct serial_rs485 rs485conf;
memset(&rs485conf, 0x0, sizeof(struct serial_rs485));

if (mode == MODBUS_RTU_RS485) {
rs485conf.flags = SER_RS485_ENABLED;
if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {

// ALWAYS FAILS HERE <<<-----------------------------------------------------------------------------------------------------------------

return -1;
}

ctx_rtu->serial_mode |= MODBUS_RTU_RS485;
return 0;
} else if (mode == MODBUS_RTU_RS232) {
if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
return -1;
}

ctx_rtu->serial_mode = MODBUS_RTU_RS232;
return 0;
}
#else
if (ctx->debug) {
fprintf(stderr, "This function isn't supported on your platform\n");
}
errno = ENOTSUP;
return -1;
#endif
}

/* Wrong backend and invalid mode specified */
errno = EINVAL;
return -1;
}