Protocols / UART
Examples
Linux userspace — configuring a tty with termios
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
struct termios tty;
tcgetattr(fd, &tty);
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8 data bits
tty.c_cflag &= ~PARENB; // no parity
tty.c_cflag &= ~CSTOPB; // 1 stop bit
tty.c_cflag &= ~CRTSCTS; // no hw flow control
tty.c_cflag |= (CLOCAL | CREAD);
cfmakeraw(&tty); // disable line-discipline processing — raw bytes in/out
tcsetattr(fd, TCSANOW, &tty);
write(fd, "ping\n", 5);
What I learned
Skipping cfmakeraw() is a classic trap: without it, the
line discipline may translate newlines, strip control characters, or
buffer by line — fine for a human terminal, silently corrupting for a
binary protocol.