Bridging serial peripherals

There are several peripherals which relays on serial port communication for interaction. Bluetooth and GSM modules are two examples, just to name a few. If support is not yet available in Zerynth and you need experimenting, this little tutorial lets you get going.

Python Script
The following little script assumes board’s SERIAL0 is connected to your PC and SERIAL1 to your device:

import streams
import threading

std = streams.serial(SERIAL0, 115200)
dev = streams.serial(SERIAL1, 115200)

def dev2std ():
    while True:

def std2dev ():
    while True:


Another piece of the puzzle is socat, a relay for bidirectional data transfer between two independent data channels. We use it to forward data from board’s standard port (e.g. /dev/ttyACM0) to GNU readline as to allow editing and reusing input lines:

socat -v -x /dev/ttyACM0,sane,rawer,b115200,crlf READLINE,prompt='$ ',history='hist' 2> log

The important configuration parameter here is rawer which disables several flags used when dealing with terminals (see TERMIOS option group in socat’s documentation). Above all, it disables echo which would cause a “digital Larsen effect”: a command sent to the peripheral generates a response which is echoed back to the peripheral which generates another response which is echoed back …

On Unix-like systems, crlf converts PC’s line ending\n’ to peripheral’s ‘\r\n’ and viceversa.

Pro tip: if you want to know PC’s port settings, run the following while port is open:

stty -F /dev/ttyACM0

Output Example
On a command line terminal:

$ AT


The log file looks like this:

> 2020/08/09 09:54:35.624991  length=4 from=0 to=3
 41 54 0d 0a                                      AT..
< 2020/08/09 09:54:35.654308  length=3 from=0 to=2
 41 54 0a                                         AT.
< 2020/08/09 09:54:35.655278  length=3 from=3 to=5
 4f 4b 0a                                         OK.
> 2020/08/09 09:54:40.721057  length=9 from=4 to=12
 41 54 2b 43 47 4d 49 0d 0a                       AT+CGMI..
< 2020/08/09 09:54:40.754280  length=8 from=6 to=13
 41 54 2b 43 47 4d 49 0a                          AT+CGMI.
< 2020/08/09 09:54:40.755867  length=7 from=14 to=20
 75 2d 62 6c 6f 78 0a                             u-blox.
< 2020/08/09 09:54:40.765398  length=1 from=21 to=21
 0a                                               .
< 2020/08/09 09:54:40.766244  length=3 from=22 to=24
 4f 4b 0a                                         OK.
1 Like

Really informative and neat.
Thanks @lorcap for this awesome tutorial.