Testing

It is possible to use socat to create a pair of virtual serial ports:

$ socat -d -d pty,raw,echo=0 pty,raw,echo=0
2019/05/02 17:26:21 socat[24050] N PTY is /dev/ttys002
2019/05/02 17:26:21 socat[24050] N PTY is /dev/ttys007

In this example, two serial ports ttys002 and ttys007 were created.

Now we can start the command tool listening on one serial port:

$ serial -v /dev/ttys007
Listening on port /dev/ttys007, press Ctrl+c to exit.

And send messages to the other port:

$ echo "H|Hello|Serial|Port|\nL|1" > /dev/ttys002

Send messages from a file

If you have a file with the data to be transmitted, you can make use of sed to read and send the contents of the file line by line. Create a bash script file “send” first, like follows:

#!/bin/bash
echo -n -e `sed -n $3p $1` > $2

Give execution permissions:

$ chmod +x send

Having a file “input.txt”, you can send the contents of this file sequentially, by executing the following command:

$ send input.txt /dev/ttys002 1

Where the last number represents the line number from the input file to send.

Escape characters