// Set data bits, stop bits, and parity
int set_parity(int fd, int databits, int parity, int stopbits)
{
struct termios opt;
if (tcgetattr(fd, &opt) != 0)
{
perror("tcgetattr\n");
return 0;
}
opt.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
opt.c_cflag |= ~CS7;
break;
case 8:
opt.c_cflag |= ~CS8;
break;
default:
fprintf(stderr, "Unsupported data bits\n");
return 0;
}
switch (parity)
{
case 'n':
case 'N':
opt.c_cflag &= ~PARENB;
opt.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
opt.c_cflag |= (PARODD | PARENB);
opt.c_iflag |= INPCK;
break;
case 'e':
case 'E':
opt.c_cflag |= PARENB;
opt.c_cflag &= ~PARODD;
opt.c_iflag |= INPCK;
break;
case 'S':
case 's':
opt.c_cflag &= ~PARENB;
opt.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "Unsupported parity.\n");
return 0;
}