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

mode:1
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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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