ctx->s = open(ctx_rtu->device, flags);
if (ctx->s == -1) {
if (ctx->debug) {
fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
ctx_rtu->device, strerror(errno));
}
return -1;
}
/* Save */
tcgetattr(ctx->s, &ctx_rtu->old_tios);
memset(&tios, 0, sizeof(struct termios));
/* C_ISPEED Input baud (new interface)
C_OSPEED Output baud (new interface)
*/
switch (ctx_rtu->baud) {
case 110:
speed = B110;
break;
case 300:
speed = B300;
break;
case 600:
speed = B600;
break;
case 1200:
speed = B1200;
break;
case 2400:
speed = B2400;
break;
case 4800:
speed = B4800;
break;
case 9600:
speed = B9600;
break;
case 19200:
speed = B19200;
break;
default:
speed = B9600;
if (ctx->debug) {
fprintf(stderr,
"WARNING Unknown baud rate %d for %s (B9600 used)\n",
ctx_rtu->baud, ctx_rtu->device);
}
}
/* Set the baud rate */
if ((cfsetispeed(&tios, speed) < 0) ||
(cfsetospeed(&tios, speed) < 0)) {
close(ctx->s);
ctx->s = -1;
fprintf(stderr, "ERROR: Set baud rate failed (cfsetospeed)");
return -1;
}
tios.c_cflag |= (CREAD | CLOCAL);
tios.c_cflag &= ~CSIZE;
switch (ctx_rtu->data_bit) {
case 5:
tios.c_cflag |= CS5;
break;
case 6:
tios.c_cflag |= CS6;
break;
case 7:
tios.c_cflag |= CS7;
break;
case 8:
default:
tios.c_cflag |= CS8;
break;
}
/* Stop bit (1 or 2) */
if (ctx_rtu->stop_bit == 1)
tios.c_cflag &=~ CSTOPB;
else /* 2 */
tios.c_cflag |= CSTOPB;
/* PARENB Enable parity bit
PARODD Use odd parity instead of even */
if (ctx_rtu->parity == 'N') {
/* None */
tios.c_cflag &=~ PARENB;
} else if (ctx_rtu->parity == 'E') {
/* Even */
tios.c_cflag |= PARENB; // FAILS HERE !!!
tios.c_cflag &=~ PARODD;
} else {
/* Odd */
tios.c_cflag |= PARENB;
tios.c_cflag |= PARODD;
}
/* Raw input */
tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
if (ctx_rtu->parity == 'N') {
/* None */
tios.c_iflag &= ~INPCK;
} else {
tios.c_iflag |= INPCK;
}
/* Software flow control is disabled */
tios.c_iflag &= ~(IXON | IXOFF | IXANY);
/* Raw output */
tios.c_oflag &=~ OPOST;
tios.c_cc[VMIN] = 0;
tios.c_cc[VTIME] = 0;
if (tcsetattr(ctx->s, TCSANOW, &tios) < 0) {
close(ctx->s);
ctx->s = -1;
fprintf(stderr, "ERROR: tcsetattr failed: %s\n", strerror(errno));
return -1;
}