Using standard utilities
Since serial ports are represented by files on Linux, standard utilities such as echo and cat can be used to interact with them. stty can be used to change the port’s settings. Let’s see how each of these applications is used.
With stty, you can set the port’s baud rate, amount of data bits, parity, handshake type (also known as flow control), and other parameters. This may be necessary to interact with a device or an application, since most of the time, the parameters have to match for the connection to work.
Use the following command to list your port’s current settings:
sudo stty -a -F <your port>
And this is an example command that sets the ttyS0 port’s baud rate to 9600, data bits to 8, and enables RTS/CTS handshaking:
sudo stty -F /dev/ttyS0 9600 cs8 crtscts
With the proper settings selected, we can send data to the device with echo. It’s possible to send plain text to a serial port with echo, like this:
echo ‘TEXT’ > /dev/ttyS0
However, to do this, you need to log in as a superuser. This can be done with the “su -” command.
In our case, special ASCII symbols, such as ESC and Carriage Return, are needed to make full use of the connected printer. One way to do this is using echo’s -e argument and escape sequences. An escape sequence for ASCII characters is started with \x, followed by a hexadecimal number that corresponds to the character.
For example, this is how the word “TEXT” can be printed with escape sequences:
echo -e '\x54\x45\x58\x54'‘
We will send the following messages:
1B 40 |
(ESC @, Initialize) |
1B 6D |
(ESC m, Full Cut) |
4C 49 4E 45 20 31 |
("LINE 1") |
1B 21 80 |
(Enable Underline) |
4C 49 4E 45 20 32 |
("LINE 2") |
0D 0A |
(CR FF, New Line) |
1B 40 |
(Full Cut) |
The -e argument is needed to allow the “\x” hexadecimal escape sequences. This is what it looks like in the terminal, and on the resulting print:
While echo can be used to send data over a serial connection, cat can be used to receive it. In our case, the printer will respond to standard ESC/POS requests for status and ID information. This means that we'll need to catch the responses. First, we will launch cat, listening to the ttyS0 device and redirecting the output to a text file:
cat /dev/ttyS0 > examplefile
The application must keep running to receive data, so a new Terminal window or tab needs to be opened to send requests to our device. We will use echo to send the following:
1D 49 31 |
(Request model ID) |
1D 49 32 |
(Request type ID) |
Since the responses will be provided in hexadecimal format, direct cat output, which was recorded to “examplefile”, will not be human-readable. Instead, we will run the file through hexdump:
Using gtkterm
Linux also provides serial terminals like minicom and gtkterm that can comprehensively interact with serial ports, handling both input and output, providing more formatting options, and making things easier overall.
As an example, we’ll try gtkterm. Similarly to stty, gtkterm needs to be launched with command line arguments to specify the connection settings, if the defaults are not suitable.
Some useful options include local echo (to duplicate the data you send on your side), logging, hexadecimal view, and sending hex data. Also, note the indicators in the bottom right - they show the states of DTR, RTS, CD, DSR, and RI signal lines - it's another way to see that the device is connected and prepared to receive data.