Monday, October 28, 2013

Serial Port Tricks

Bidirectional

The Netcat program can shovel data bidirectionally to/from a serial port and over a network, which is very handy indeed.

Set the serial port in raw mode and configure it:
# stty -F /dev/ttyUSB0 raw
# stty -F /dev/ttyUSB1 raw
# stty -F /dev/ttyUSB0 19200
# stty -F /dev/ttyUSB1 19200


Set up a netcat listener that will send data to/from the one serial device:
# nc -l 1234 < /dev/ttyUSB1 >/dev/ttyUSB1

Set up a netcat client that will send data to/from the other serial device:
# nc listeneripaddress 1234 < /dev/ttyUSB0 >/dev/ttyUSB0

Unidirectional

For debugging and scripting, you can also use ordinary cat, echo, head or even data definition to access the serial ports: 

Send data one way only using the common kitty:
# cat /dev/ttyUSB0 > /dev/ttyUSB1

Send a message out a port using echo:
# echo Hello > /dev/ttyUSB0

Send data denoted as hexadecimal values and suppress the LF at the end of the line:
# echo -en "\x12\x23\x45" > /dev/ttyUSB0

Read one character from a serial port using head:
# $CHAR = head -c 1 /dev/ttyUSB0
# echo $CHAR

La voila!

No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.