Saturday, January 4, 2014

CRCs and Serial Ports


Scope

This mini guide explains how to append messages with CRCs and send them to a serial port.  Operating a serial port from the Linux Bash shell is very obscure and precious few people know how to do it.

Once you know the tricks, it is extremely powerful and a big time saver over the more complicated and error prone alternatives.

Also see this: http://www.aeronetworks.ca/2014/07/fletcher-checksum-calculator-in-bash.html

Get a set of CRC utilities from here:

http://www.hampa.ch/misc-utils/index.html
http://www.hampa.ch/misc-utils/misc-utils-0.2.2.tar.gz

Install the tools:
$ cd
$ mkdir misc
$ cd misc
$ wget http://www.hampa.ch/misc-utils/misc-utils-0.2.2.tar.gz
$ tar -zxvf misc-utils-0.2.2.tar.gz
$ ./configure
$ make
$ su
password
# make install

CRC Message Preparation:

Prepare a message step by step:
$ echo -en "\xa5\x10\x01\xca" > msg
$ hex msg
A5 10 01 CA # 00000000  ....
$ crc -g crc16 msg
41cf

Or modify the message file on the fly:
$ crc -b -g crc16 msg >> msg
$ hex msg
A5 10 01 CA 41 CF # 00000000  ....A.

Or do it all in one line:
$  echo -en "\xa5\x10\x01\xca" | crc -b -g crc16 - >> msg

Serial Port Tricks

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

Prepare a message with a CRC16 and send it straight to the serial port:
$  echo -en "\xa5\x10\x01\xca" | crc -b -g crc16 - >> /dev/ttyUSB0

No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.