Linux Serial Programming

There is termios which is the linux library of accessing the serial port. It is super fucking complicated.

There are some concepts to keep in mind.

1. Canonical mode: This is most useful when dealing with real terminals, or devices that provide line-by-line communication. The terminal driver returns data line-by-line.

2. Non-canonical mode: In this mode, no special processing is done, and the terminal driver returns individual characters.

3. “Blocking”: sets whether a read() on the port waits for the specified number of characters to arrive. Setting no blocking means that a read() returns however many characters are available without waiting for more, up to the buffer limit.

The man page (link) for termios is actually pretty useful. This is not that complicated. I, and you, can friggin do this.

There is an interesting function called cfmakeraw that sets the terminal to what I want I think I want.

If you use printf to print to the terminal be aware that stdout is buffered! That means that it doesn't just instantaneously print to the terminal. It either waits for a new line, has a timeout (i think) or a buffer fills up.

To get printf to print immediately either call fflush(stdout) or do the following to make the buffer size smaller:

// printf which uses stdout is buffered. The larger the buffer, the slower it
// will post to the terminal. I can either set the buffer to smaller size like
// the following, or call fflush(stdout) to force a write to the terminal
char buffer[10];
setvbuf(stdout, buffer, _IOFBF, sizeof(buffer));

To list the USB devices by dev path use this script (link).

I have added this script to paul_scripts and added an alias called listusb to my .zshrc file.

  • linux_serial_programming.txt
  • Last modified: 2019/03/31 14:49
  • (external edit)