Skip to main content

Serial Ports Revisited

We were trying to test a pair of radios with a data loop-back and were again making the same old mistakes and then wondering what the heck is going on. It is just amazing how many times I have sat down and scratched my head with these things - it feels like I never learn or remember!

The mistake we make over and over again is to expect the wrong read/write behaviour from a serial port utility:
  • A serial port is a bidirectional device.
  • A program will grab the device file handle and open the device file either as 'Read', 'Write' or 'Read/Write'.
  • Simple programs like 'cat', 'echo', 'bash' (read, write), 'head', 'tail' and 'of', all open the device file as either 'Read' or 'Write', never 'Read/Write'.
  • Complex programs like 'cutecom', 'minicom', 'screen' and 'netcat', open the device file as 'Read/Write'.
So, one cannot run 'cat' twice on the same port in order to send and receive data, you have to use 'netcat' or 'screen' to do that in one convoluted operation, or you have to make a T cable and run the Rx and Tx wires to two separate serial ports and then you can run 'cat' twice (on the different ports).

Configuration

Ensure that you are a member of the dialout group:
# usermod -a -G dialout username

To configure a serial port you can use either 'screen', 'minicom' or 'stty':
$ screen /dev/ttyUSB0 115200
$ minicom -b 115200 -o -D /dev/ttyUSB0
or
$ stty -F /dev/ttyUSB0 raw
$ stty -F /dev/ttyUSB0 115200


To exit screen, type Control-A k and exit minicom with Control-A x.

Tx and Rx

Then, to send and receive data through the port:

This will not work concurrently.  You cannot open two terminals and run these commands together to see what is transceived in real time:
$ cat txfile > /dev/ttyUSB0
$ cat /dev/ttyUSB0 > rxfile


After the first instance of 'cat' grabbed the file handle to send the file, the second instance cannot open it again to receive the file at the same time - oops...

T Cable Solution

If you make a T cable then you can do this in two terminals:
$ cat txfile > /dev/ttyUSB0
$ cat /dev/ttyUSB1 > rxfile


That will work because each instance of 'cat' uses a separate serial port, but then you got to find three 9 pin connectors and two serial port adaptors, which may not all be available, but the commands are easy to understand.

Echo and Read

The easier way, is to use screen with exec, or minicom with expect, or do character I/O one at a time in bash with echo and read.

If you use read, be sure to set a timeout or it may hang forever.  It is also usually needed to flush any junk from the receive buffer before you try to look for a specific response, and use a sleep statement to allow the response to come back after a command:

echo "Set serial port USB0 to 9600N81"
stty -F /dev/ttyUSB0 raw
stty -F /dev/ttyUSB0 9600
echo

echo Wait for the dust to settle
sleep 1

# Flush
JUNK=""
for i in 1 2 3 4 5; do
  read -r -t1 JUNK < /dev/ttyUSB0

  sleep 0.1
done

# Send LFCR and Read a line
LINE=""
echo -en "/n/r" > /dev/ttyUSB0

sleep 0.1
read -r -t1 LINE < /dev/ttyUSB0

# Flush
JUNK=""
for i in 1 2 3 4 5; do
  read -r -t1 JUNK < /dev/ttyUSB0

  sleep 0.1
done

if [ "$LINE" != "NO CARRIER" ]; then
  echo Communication failure
  exit 1
fi

echo Communication OK

exit 0

Hex and Octal Dump

While I am at it, sometimes it is better to output data in hexadecimal. This can be done with the 'od' (octal dump) program instead of the venerable 'cat':
$ cat txfile | nc < /dev/ttyUSB0 > /dev/ttyUSB0 | od -x

or
$ od -x < /dev/ttyUSB1

or
$ $ od -x < /dev/ttyUSB1 > rxfile

and so on.

A Real Computer

Obviously, you need a real computer to do all this:
http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/20000/1000/000/21021/21021.strip.zoom.gif

The joke comes from here:
http://www.cryptonomicon.com/beginning.html

Serial Port Tips

www.aeronetworks.ca/2015/01/serial-port-io.html
www.aeronetworks.ca/2014/10/serial-ports-revisited.html
www.aeronetworks.ca/2014/01/crcs-and-serial-ports.html
www.aeronetworks.ca/2013/10/serial-port-tricks.html
www.aeronetworks.ca/2013/05/usb-serial-device-with-unknown-ids.html
www.aeronetworks.ca/2015/10/reading-and-parsing-data-from-serial.html
www.aeronetworks.ca/2013/05/compile-moxa-serial-widget-device.html


La voila!

Herman


Comments

Popular posts from this blog

Parasitic Quadrifilar Helical Antenna

This article was reprinted in OSCAR News, March 2018:  http://www.amsat-uk.org If you want to receive Satellite Weather Pictures , then you need a decent antenna, otherwise you will receive more noise than picture. For polar orbit satellites, one needs an antenna with a mushroom shaped radiation pattern .  It needs to have strong gain towards the horizon where the satellites are distant, less gain upwards where they are close and as little as possible downwards, which would be wasted and a source of noise.  Most satellites are spin stabilized and therefore the antenna also needs circular polarization, otherwise the received signal will flutter as the antennas rotate through nulls. The helical antenna, first proposed by Kraus in 1948, is the natural solution to circular polarized satellite communications.  It is a simple twisted wire - there seems to be nothing to it.  Various papers have been published on helix antennas, so the operation is pretty well understood. Therefore,

Weather Satellite Turnstile Antennas for the 2 meter Band

NEC2, 2 m band, 146 MHz, Yagi Turnstile Simulation and Build This article describes a Turnstile Antenna for the 2 meter band, 146 MHz amateur satcom, 137 MHz NOAA and Russian Meteor weather satellites.  Weather satellite reception is described here .  A quadrifilar helical antenna is described here .   Engineering, is the art of making what you need,  from what you can get. Radiation Pattern of the Three Element Yagi-Uda Antenna Once one combine and cross two Yagis, the pattern becomes distinctly twisted. The right hand polarization actually becomes visible in the radiation pattern plot, which I found really cool. Radiation Pattern of Six Element Turnstile Antenna Only a true RF Geek can appreciate the twisted invisible inner beauty of a herring bone antenna... Six Element Turnstile Antenna Essentially, it is three crosses on a stick.  The driven elements are broken in the middle at the drive points.  The other elements can go straight throug

Patch Antenna Design with NEC2

The older free Numerical Electromagnetic Code version 2 (NEC2) from Lawrence Livermore Lab assumes an air dielectric.  This makes it hard (but not impossible) for a radio amateur to experiment with Printed Circuit Board Patch antennas and micro strip lines. Air Spaced Patch Antenna Radiation Pattern You could use the free ASAP simulation program , which handles thin dielectrics, you could shell out a few hundred Dollars for a copy of NEC4 , You could buy GEMACS if you live in the USA, or you could add distributed capacitors to a NEC2 model with LD cards (hook up one capacitor in the middle of each element.), but that is far too much money/trouble for most. More information on driving an array antenna can be found here: https://www.aeronetworks.ca/2019/03/driving-quad-patch-array-antenna.htm l Air Dielectric Patch   The obvious lazy solution is to accept the limitation and make an air dielectric patch antenna. An advantage of using air dielectric, is that the antenn