Linux serial read blocks minicom

2019-09-05 16:39发布

I'm attempting to read from a serial port (/dev/ttyS4) on a BeagleBone Black, but I think(?) this should apply to all Linux devices in general.

Currently, I can set up minicom with a baud rate of 9600 and 8N1 data to read from the serial port correctly. However, if I attempt to directly cat /dev/ttyS4, nothing shows up in my terminal. My code also does this, and returns a Resource temporarily unavailable error, which I suspect is what is happening with the cat command.

If I run stty -F /dev/ttyS4, I get the following output (which, as far as I can tell, is consistent with my minicom settings):

speed 9600 baud; line = 0;
intr = <undef>; quit = <undef>; erase = <undef>; kill = <undef>; eof = <undef>; start = <undef>; stop = <undef>; susp = <undef>; rprnt = <undef>; werase = <undef>; lnext = <undef>; flush = <undef>;
-brkint -imaxbel
-opost -onclr
-isig -iexten -echo -echoe -echok -echoctl -echoke

An interesting note is that when I have minicom open, if I start my program, minicom will stop printing anything, and stay that way even if I stop my program. I need to open the serial settings again (Ctrl-A, P) and close it for minicom to resume working (it appears that nothing was changed).

My code is as follows:

int main() {
    std::cout << "Starting..." << std::endl;

    std::cout << "Connecting..." << std::endl;
    int tty4 = open("/dev/ttyS4", O_RDWR | O_NOCTTY | O_NDELAY);
    if (tty4 < 0) {
        std::cout << "Error opening serial terminal." << std::endl;
    }

    std::cout << "Configuring..." << std::endl;
    struct termios oldtio, newtio;
    tcgetattr(tty4, &oldtio);   // save current serial port settings
    bzero(&newtio, sizeof(newtio)); // clear struct for new settings

    newtio.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
    newtio.c_iflag = IGNPAR | ICRNL;
    newtio.c_oflag = 0;
    newtio.c_lflag = ICANON;

    tcflush(tty4, TCIFLUSH);
    tcsetattr(tty4, TCSANOW, &newtio);

    std::cout << "Reading..." << std::endl;
    while (true) {
        uint8_t byte;
        int status = read(tty4, &byte, 1);
        if (status > 0) {
            std::cout << (char)byte;
        } else if (status == -1) {
            std::cout << "\tERROR: " << strerror(errno) << std::endl;
        }
    }

    tcsetattr(tty4, TCSANOW, &oldtio);
    close(tty4);
}

Edit: I've gotten the serial port to work correctly (in python) by following Adafruit's tutorial for using python with the BeagleBone. At this point I'm certain that I'm doing something wrong; the question is what. I would much prefer using C++ over python, so it'd be great to get that working.

1条回答
Deceive 欺骗
2楼-- · 2019-09-05 17:00

Your program opens the serial terminal in nonblocking mode.

   int tty4 = open("/dev/ttyS4", O_RDWR | O_NOCTTY | O_NDELAY);

Nonblocking I/O, especially read operations, requires additional, special handling in a program. Since you neglect to mention this mode, and your program has no capability to properly process this mode, this could be considered a bug.

Either remove O_NDELAY option from the open() call, or insert a fcntl(tty4, F_SETFL, 0) statement to revert back to blocking mode.


My code also does this, and returns a Resource temporarily unavailable error,

That's an EAGAIN error, which is consistent with a nonblocking read().
The man page describes this error will occur when "the file descriptor ... has been marked nonblocking (O_NONBLOCK), and the read would block".
The read() syscall "would block" because there is no data to satisfy the read request.

If you insist on using nonblocking mode, then your program must be able to cope with this condition, which is not an error but a temporary/transient status.
But blocking mode is the simpler and preferred mode of operation for typical programs in a multitasking system.
Your program should be modified as previously mentioned.


There are numerous issues with the initialization of the serial terminal.


   tcgetattr(tty4, &oldtio);   // save current serial port settings

The return values from the tcgetattr() and tcsetattr() syscalls are never checked for errors.


   bzero(&newtio, sizeof(newtio)); // clear struct for new settings

Starting with an empty termios structure is almost always a bad idea. It may appear to work on some systems, but it is not portable code.
The proper method for initializing a termios structure is to use values from tcgetattr().
See Setting Terminal Modes Properly.
Since it is already called, all you need is newtio = oldtio to copy the structure.


    newtio.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
    newtio.c_iflag = IGNPAR | ICRNL;
    newtio.c_oflag = 0;
    newtio.c_lflag = ICANON;

Rather than assignment of constants, the proper method of changing these flags is to enable or disable the individual attributes.
The following should suffice for canonical mode:

    newtio.c_cflag |= CLOCAL | CREAD;
    newtio.c_cflag &= ~CSIZE;
    newtio.c_cflag |= CS8;         /* 8-bit characters */
    newtio.c_cflag &= ~PARENB;     /* no parity bit */
    newtio.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    newtio.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

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

    newtio.c_iflag &= ~INPCK;
    newtio.c_iflag |= ICRNL;
    newtio.c_iflag &= ~(INLCR | IGNCR | IUCLC | IMAXBEL);
    newtio.c_iflag &= ~(IXON | IXOFF | IXANY);   /* no SW flowcontrol */

    newtio.c_oflag &= ~OPOST;

The following is the preferred method for setting the baudrate:

    cfsetospeed(&newtio, B9600);
    cfsetispeed(&newtio, B9600);

If any salient attributes are left unspecified, the existing settings are used.
This can lead to erratic program behavior, e.g. sometimes it works, sometimes it doesn't.


An interesting note is that when I have minicom open, if I start my program, minicom will stop printing anything, and stay that way even if I stop my program. I need to open the serial settings again (Ctrl-A, P) and close it for minicom to resume working (it appears that nothing was changed).

The serial terminal is not intended for sharing among more than one process.
Some of the termios attributes have to be implemented in the serial device driver, which has no concept of sharing the port. The most recent termios attributes are in effect for the device.
When you execute your program after minicom has started, you are clobbering the termios attributes that minicom expects to execute with.
You are restoring the termios attributes to minicom's requirements by using its menu.

查看更多
登录 后发表回答