writing to serial port from linux command line

erin c picture erin c · Jan 16, 2012 · Viewed 132.8k times · Source

From windows I can communicate with a serial port device using following commands:

mode com1: baud=9600 data=8 parity=n stop=1
copy con com1
alt+18alt+2ctrl+z

Device starts the requested operation.

When I try to accomplish the same operation from a stand alone debian box or from a debian virtualbox instance of the same windows machine, I had no luck so far.

Here's equivalent linux commands(at least I think so)

stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb
echo '\x12\x02' > /dev/ttyS0

Nothing happens.

Can somebody please direct me to the right direction?

Answer

praetorian droid picture praetorian droid · Jan 16, 2012

If you want to use hex codes, you should add -e option to enable interpretation of backslash escapes by echo (but the result is the same as with echoCtrlRCtrlB). And as wallyk said, you probably want to add -n to prevent the output of a newline:

echo -en '\x12\x02' > /dev/ttyS0

Also make sure that /dev/ttyS0 is the port you want.