Serial Programming

From miki
Jump to navigation Jump to search

References

How-To

Flush Serial Buffers

Use constant TCSAFLUSH for function tcsetattr (see [1] and [2]):

Constants for tcsetattr
Constant Description
TCSANOW Make changes now without waiting for data to complete
TCSADRAIN Wait until everything has been transmitted
TCSAFLUSH Flush input and output buffers and make the change

This can be done even on a serial port that is already configured:

  struct termios  options;
  tcgetattr(fd, &options);                   // Get current configuration of this port
  cfsetispeed(&options, B115200);            // Configure some port options (e.g. baud rate...)
  ...
  tcsetattr(fd, TCSANOW, &options);          // Apply the new settings immediately 
  ...                                        // Read / Write to the port...                       
  tcsetattr(fd, TCSAFLUSH, &options);        // Flush IO buffer

Tools

Minicom

Minicom (wikipedia) is a text-based modem control and terminal emulation program for Linux.

It can be used to debug / test serial connections, but unfortunately it is only ascii based and does not allow to send / receive easily data in hexadecimal format (except via sending / receiving files).

Minicom is in the repositories:

sudo apt-get install minicom

Cutecom

Cutecom is a handy and easy graphic tool to test/debug embedded devices controlled via serial link. This tool allows sending / receiving data in hexadecimal format.

Cutecom is in the repositories:

sudo apt-get install cutecom

interceptty

interceptty is a program that sits between a serial port (or other tty-style device) and an application, and logs everything that passes through it.

SerLook

SerLook is a KDE application for inspecting data going over serial lines.

Troubleshooting

Shunt RXD/TXD pin

An easy way to test whether the serial line is working properly is simply to shunt the RXD and TXD line of the serial connector (2nd and 3rd pin on a typical DE9 RS232 connector). This way anything sent to the serial port is echoed back.

  1. Open a terminal connected to the serial port,
  2. Shunt RXD and TXD (2nd and 3rd pin),
  3. Type character and verify that the same character are echoed back.
  _______________________
 /   1               5   \
 \   o   o   o   o   o   /
  \    6           9    / 
   \   o   o   o   o   /
    \_________________/

On a D-sub DE9 RS232 connector, the top left pin is the 1st pin, the bottom right is the 9th pin. The pinout is as follows:

Pin Name Dir. Description Pin Name Dir. Description
1 CD Carrier Detect 6 DSR Data Set Ready
2 RXD Receive Data 7 RTS Request to Send
3 TXD Transmit Data 8 CTS Clear to Send
4 DTR Data Terminal Ready 9 RI Ring Indicator
5 GND System Ground