Stty Serial Port Settings for Parity Not Persistent

stty serial port settings for parity not persistent

The stty command is simply a method from the shell to utilize the termios API.

Application programs are expected to use the termios API to configure the serial terminal to the exact requirements of the situation (rather than rely on an expected configuration on startup).

If the app environment that you're using does not permit access to the termios API, then you may be using an inappropriate method.

Do you know any linux app that could explicitly react somehow to a parity error?

The following C program reads lines (i.e. canonical mode) from a serial terminal, and is configured to detect a Mark (or 1) as the parity bit with an 8-bit character frame.

#define SERIALTERMINAL      "/dev/ttyS0"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

int set_interface_attribs(int fd, int speed)
{
struct termios tty;

if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}

cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);

tty.c_cflag |= CLOCAL | CREAD;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag |= PARENB; /* enable parity */
tty.c_cflag &= ~PARODD; /* Even parity */
tty.c_cflag |= CMSPAR; /* force Even parity to SPACE */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */

tty.c_lflag |= ICANON | ISIG; /* canonical input */
tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);

tty.c_iflag &= ~IGNCR; /* preserve carriage return */
tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
tty.c_iflag &= ~(IXON | IXOFF | IXANY); /* no SW flowcontrol */
tty.c_iflag |= IGNBRK; /* ignore breaks */
tty.c_iflag &= ~ISTRIP;
tty.c_iflag &= ~IGNPAR; /* report error */
tty.c_iflag |= INPCK; /* test parity */
tty.c_iflag |= PARMRK; /* verbose parity err */

tty.c_oflag &= ~OPOST;

tty.c_cc[VEOL] = 0;
tty.c_cc[VEOL2] = 0;
tty.c_cc[VEOF] = 0x04;

if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}


int main(void)
{
char *portname = SERIALTERMINAL;
int fd;
int wlen;

fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
/*baudrate 115200, 8 bits, Space for parity, 1 stop bit */
set_interface_attribs(fd, B115200);

/* simple output */
wlen = write(fd, "Hello!\n", 7);
if (wlen != 7) {
printf("Error from write: %d, %d\n", wlen, errno);
}
tcdrain(fd); /* delay for output */


/* simple canonical input, read lines */
do {
unsigned char buf[81];
unsigned char *p;
int rdlen;

rdlen = read(fd, buf, sizeof(buf) - 1);
if (rdlen > 0) {
buf[rdlen] = 0;
printf("Read %d:", rdlen);
/* first display as hex numbers then ASCII */
for (p = buf; rdlen-- > 0; p++) {
printf(" 0x%x", *p);
if (*p < ' ')
*p = '.'; /* replace any control chars */
}
printf("\n \"%s\"\n\n", buf);
} else if (rdlen < 0) {
printf("Error from read: %d: %s\n", rdlen, strerror(errno));
} else { /* rdlen == 0 */
printf("Nothing read. EOF?\n");
}
/* repeat read */
} while (1);
}

The program was executed on an old Linux (Ubuntu 14.04.2 LTS) PC that has a built-in 16550A serial port.

[    2.656593] 00:08: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A

This serial port does not seem capable of transmitting 8-bits with parity (an 11-bit frame), but does seem capable of reading 8-bits with parity.

The serial data is generated from a SBC that has a 9-bit capable UART. An oscilloscope was used to capture frames to confirm that the 8S1 and 8E1 frames were 11 bits long.

(An FTDI USB-to-RS232 converter was not reliable in generating all parity configurations with 8-bit characters.)


When the sender is configured for 8-bits and Space for parity (which matches the program), the PC program reads "ABCDEFG\n" as:

Read 8: 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0xa
"ABCDEFG."

The data is read correctly.


When the sender is configured for 8-bits and Even parity, the PC program reads "ABCDEFG\n" as:

Read 14: 0x41 0x42 0xff 0x0 0x43 0x44 0xff 0x0 0x45 0xff 0x0 0x46 0x47 0xa
"AB�.CD�.E�.FG."

The read (correctly) identifies three characters that have Mark instead of Space as the parity bit.

Each character with "parity error" is preceded by bytes of 0xFF 0x00 (i.e. a total of three bytes).

Note that when the actual received datum is 0xFF (with no parity error), termios will expand that datum to two bytes of 0xFF 0xFF. So beware that when the next datum is 0x00, then this is not the error indication. IOW reading 0xFF 0xFF 0x00 converts to actual data 0xFF 0x00.

But when the actual received datum is 0xFF with a parity error, then read returns 0xFF 0x00 0xFF (i.e. there is no expansion combined with the error indication).

Persistent stty Settings?

Credits go to PHS. Symlinking .bashrc to .profile actually worked. Wow, I thought bash would read .profile by default.

Thanks Phs.

Set stty parameters

try

stty -F /dev/ttyS0 cs7 cstopb -ixon raw speed 1200

screen /dev/ttyUSB0' with different options such as databit, parity, etc

I don't think Screen has support for all these different serial port settings. Only the most basic parameters are supported.
You're already in the correct direction by looking at the stty manual, but you have to use stty as a separate tool from Screen:
First you configure your serial port, and then you connect to it using Screen.

To configure your serial port for computer 1:

# stty - change and print terminal line settings
#
# -F /dev/ttyUSB0 Change the settings of /dev/ttyUSB0
# cs8 Use 8 character bits
# -parenb Don't use a parity bit (the '-' means 'disable')
# crtscts Enable RTS/CTS handshaking (hardware flow control)
stty -F /dev/ttyUSB0 cs8 -parenb cstopb crtscts

After you've configured your port, you can start using it trough screen:

# screen - screen manager with VT100/ANSI terminal emulation
#
# /dev/ttyUSB0 Use /dev/ttyUSB0 as terminal
# 9600 Open the serial port using 9600 baud
screen /dev/ttyUSB0 9600

The same applies for your second computer:

# stty - change and print terminal line settings
#
# -F /dev/ttyUSB0 Change the settings of /dev/ttyUSB0
# cs7 Use 7 character bits
# parenb Enable the a parity bit
# -parodd Don't use ODD, but use EVEN parity
# -cstopb Don't use 2 stopbits, but just the regular 1
# crtscts Enable RTS/CTS handshaking (hardware flow control)
stty -F /dev/ttyUSB0 cs7 parenb -parodd -cstopb crtscts

Then you can launch Screen at 9600 baud:

# screen - screen manager with VT100/ANSI terminal emulation
#
# /dev/ttyUSB0 Use /dev/ttyUSB0 as terminal
# 9600 Open the serial port using 9600 baud
screen /dev/ttyUSB0 9600

This should do the trick. You can find much more configuration options in the help of stty:

stty --help


Related Topics



Leave a reply



Submit