Zmodem over uart

Sometimes you need to get a file to your embedded linux system and all you have is a serial port. zmodem to the rescue. zmodem is nice because it manages the file creation and much faster and safer to use than xmodem.

First install rz and sz tools. also included in busybox.

sudo apt install -y lrzsz

Then using whatever terminal you like log into your embedded system. You should be able to keep the terminal open as long as you don't send any data during the transfer.

Below is commands to get the file over zmodem. change /dev/ttyACM0 to whatever you serial port device is at.

#!/bin/bash
stty -F /dev/ttyACM0 115200  #configure to the baud rate of the embedded system 
echo "rz" > /dev/ttyACM0  #run the rz server on the embedded system
sleep 0.5  #wait for the embedded system to start the server
sz --zmodem YourFile >/dev/ttyACM0 < /dev/ttyACM0    #push your file file to the embedded system. 

And below is a more generic script allowing a filename to be passed in. scriptname sourceFile [opt: destination folder serialname baudrate]

example running on the host system: this will copy a file called fimware.bin to the embedded system to /tmp/firmware.bin

./xmodem_cp.sh fimware.bin /tmp/ /dev/ttyUSB0 921600
#!/bin/bash
DEVICEPIPE="/dev/ttyACM0"
BAUDRATE="115200"
if [ $4 ]
then
BAUDRATE=$4
fi
if [ $3 ]
then
DEVICEPIPE=$3
fi
if [ $2 ]
then
echo "cd $2"  > $DEVICEPIPE
fi
stty -F $DEVICEPIPE $BAUDRATE
echo "rz" > $DEVICEPIPE
sleep 0.5
sz --zmodem $1 > $DEVICEPIPE < $DEVICEPIPE