I need to send a small file over serial port (actually /dev/ttyUSB0, there is a usb-to-serual converter attached to the port). The operation needs to be scriptable i.e. non-interactive.

Here is what I have tried:

  • minicom - works correctly, but is interactive. --script=SCRIPT option looks promising, but I have no idea how to write the script.
  • sx - non-interactive, but freezes, probably because I did not specify baud rate and byte format (stop bits, parity, data bits).

What are my options?

EDIT1: stty + sx

Still no luck, while minicom with 38400 8N1 sends the file correctly. I am doing

sudo stty -F /dev/ttyUSB0 38400 cs8 -parenb -cstopb -ixoff
sx ~/test_signals/ones.bin -vvvvvvvvvvvv

sx just freezes. Here is the output:

vorac@laptop:~/projects/megaboot$ sx ~/test_signals/ones.bin -vvvvvvvvvvvv
sx 0.12.21rc

Sending /home/vorac/test_signals/ones.bin, 7 blocks: Give your local XMODEM receive command now.
wctx:file length=1000
                     Calling read: alarm=60  Readnum=128 

And just stays there. The receiver is using the short 1-byte checksum method, indicated by sending a NACK at the beginning of the file, instead of the 2-byte crc, which would be indicated by sending a 'C'. Maybe this is unsupported by cx?

EDIT2: cu + stty + sx

To avoid reading the exhaustive documentation on cu, I copied the command from this article. Still no luck.

vorac@laptop:~$ sudo cu -l /dev/ttyUSB0 -s 38400
cu: open (/dev/ttyUSB0): Permission denied
cu: /dev/ttyUSB0: Line in use
vorac@laptop:~$ groups vorac
vorac : vorac adm dialout cdrom sudo dip plugdev lpadmin sambashare wireshark

sx doesn't talk to serial ports by itself; it communicates over stdout. You need to redirect the output for it to do anything useful.

Personally, I find it easiest to use cu to set up the serial connection, run whatever is needed on the remote end to start receiving data with *MODEM, and then use cu's escape codes to run sx locally. You can do this with ~$sx (followed by whatever options you want to pass to sx) as the escape sequence.

Do note, though, that cu recognizes escape sequences only at the start of a line.

EDIT: The 'line in use' message means something else is using the serial port. Try running fuser /dev/ttyUSB0 to figure out what it is, and kill it. Alternatively, you can pull the USB serial convertor from the USB port and plug it in again; that will certainly kill whatever it is that is hogging your serial port. After that, your cu line should just work.

If even that doesn't work, you may have a stale lock file in /var/lock. That's rather unlikely, though, unless you used kill -9 on some previous tool.

  • I didn't expect this to be so difficult. Provided you have the time to spare, pelease see my second edit. Btw running screen results in [screen is terminating]. – Vorac Oct 29 '15 at 9:30

You don't set the baud rate etc with sx.

You do that with stty. You probably also want to disable xon/xoff for xmodem just in case there are any XON or XOFF bytes in the file you are sending, so use the -ixon option.

e.g. for 115200,n,8,1 you would use:

stty -F /dev/ttyUSB0 115200 cs8 -parenb -cstopb -ixon

See man stty for more details.

  • This looks great, but I still can't send the file. I edited the details into my question. Any idea how can I understand what is going on? – Vorac Oct 29 '15 at 7:28
  • I assumed you'd already established the serial connection to your device. See unix.stackexchange.com/questions/56614/… – cas Oct 29 '15 at 7:37
  • if you don't want to use screen, you could use cu(very simple command-line serial utility) from the uucp package. – cas Oct 29 '15 at 7:39

An xmodem transition secceed here. After input/output redirection (example refs) as Wouter Verhelst suggested is added, and based on your experience. Luckyly, I send an bin firmware on linux with 2 commands.

$ sudo stty -F /dev/ttyUSB1 9600 cs8 -parenb -cstopb -ixoff
$ sudo stty -F /dev/ttyUSB1
speed 9600 baud; line = 0;
-brkint -imaxbel

$ sx fw.bin < /dev/ttyUSB1 > /dev/ttyUSB1

can't you just do something like

$ picocom -b 115000 /dev/ttyUSB0 
// 2nd term
$ cat file > /dev/ttyUSB0
  • Welcome to the site. Would you mind explaining to which of the two possiblities your statement about your personal expertise applies? If possible, please only contribute answers where you don't need that strong of a disclaimer ... ;) – AdminBee Feb 3 at 9:22
  • No problem. Thank you. – donkeh Feb 3 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.