Tuesday, October 21, 2014

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


Monday, October 20, 2014

Printers

A few notes for those new to Linux printing on how to waste perfectly good trees.

FTP

First walk over to the printer and write down the IP address from the little LCD display.

Many network printers have an unsecured FTP server that your company IT is blissfully unaware of (or more likely, they know only True Card Carrying Geeks will use it in a moment of sheer desperation, so they leave it be, even though it can potentially be abused).  If you upload a postscript file to a printer, it will print it immediately - no questions asked.  This works without having anything special installed on your Linux machine.

The process works something like this:

In your application, select Print to File, then save the file as file.ps.  Open a terminal and connect with FTP, then put the file: 

$ ftp 172.22.8.12
Login: [enter]
Password: [enter]
ftp> put file.ps
ftp> bye

La voila!

CUPS with IPP

The CUPS service is maintained by Apple and it works very well indeed.   Usually, it will discover network printers automatically, but unless your company IT named the printers properly, it won't tell you where the printer is and in a large company there may be hundreds of printers all over the world.  If you cannot figure it out, then you can manually configure a printer.

Open a browser and type localhost:631 to open the CUPS management program.

Go to the Administration screen and select Add Printer.  Select the IPP protocol.  For the URL, type for example ipp://172.22.8.12/ipp/

Enter a printer name and location so you can remember which one it is.  Find the printer manufacturer and device driver, for example HP and HP Laserjet 9040 with CUPS and Gutenprint (en).  If you can't find the exact printer model number, try something similar, it usually works - sometimes you just lose a special feature such as duplex printing that nobody ever use anyway, since it always causes paper jams.

Finally, select Maintenance and print a Test Page.

When you experience printer trouble, restart CUPS from the command line with service cups restart  and then go to the management screen again and clear all the stuck print jobs.

Easy as pie.

LPR

When you have CUPS installed, you can also use the lpr program from the command line.  CUPS can print most types of files automatically.  You don't necessarily need to run a specific application to print something - just send the file to CUPS and it will usually figure it out and convert the file to postscript all by itself.

For example to print to the default printer on your machine:

$ lpr -P localhost file.pdf

It also works with text files and even pictures.  You don't need to fire up a PDF reader or Gimp to print.  You can also pipe multiple files and then go and feed the trees and toner into the printer.

You can likewise use lpr to send a file from a new machine with no configuration, to another machine that already has CUPS installed and print via a proxy.  There is always a simple way to make it work.

Saturday, October 18, 2014

Diabetic Foods and Cooking

A few notes on sugar free cooking.

As my doctor put it, I am not diabetic, but I should eat as if I am.

It appears that the most important dietary modification to diabetes and weight control, is to avoid any food that digests rapidly.

At first the list looks daunting: Wheat, potato, rice, alcohol, glucose, sucrose... essentially all common carbohydrates must be avoided - which makes one wonder what is left, since that is about 90% of all the junk on the supermarket shelves!

Oats and Rye

I don't mind going on a caveman diet of meat, salad and nuts, since I'm a committed carnivore already, but wheat bread is curiously addictive and getting over it is not easy.  There are many different kinds of wheat: Spelt, Durham... so be careful that you don't buy an alternative that isn't an alternative at all.

Rye bread is commonly available at bakeries, but it is a little chewy - an automatic consumption limiter.

My wife is an engineer of economics, loves cooking and did a few experiments with rye, oats and barley kernels.  She quickly figured out that it is very easy to make rye and oats flour: Simply chuck the grains into a coffee grinder and press the button - brrrrrrrrrrrrzzzzzzt - done, perfect flour!

The other surprising thing is that when oats flour is used in baking, it doesn't taste like oatmeal porridge as one would expect.  Baked oats confectionery works and tastes the same as wheat bread and cake - probably because of all the other ingredients.  So between oats and rye, we got wheat eliminated easily and she bakes bread, cake, pancakes, strudel - anything - with oats and rye flour.

Barley

Barley makes a perfect alternative to rice.  It is a little different, but sufficiently the same that we don't miss rice at all.

Sucralose, Aspartame, Xilatol and Stevea

For baking, it is important to use a sweetener that doesn't denature at high temperature.  Sucralose is available in boxes as a fluffed up powder that can be used almost 1:1 (more like 1:2, since it is extremely sweet) to replace sugar.

Be careful with diet sodas.  Many people are sensitive to phenylalanine (produced when Aspartame is broken down) and it increases muscle tension.  Too many diet sodas and you will feel creaky as if you are a 100 years old.  If you overdosed on sodas, bear in mind that phenylalanine takes about 14 days to get out of your system again, so be patient, you will eventually be able to turn your head again.  I can handle one diet soda every other day - no more.  Alcohol free beer is much better and I can chug as many of those as I want with no side effects.

Xilatol is used in 'dental' chewing gum.  It is derived from wood.  The only reason I mention this here, is because Xilatol gum will kill a dog - it makes a dog stop breathing.  Don't leave gum lying around where a puppy can get it, or you could become rather unpopular very quickly.

I don't like the taste of Stevea root much, but I cannot imagine life without chocolate...

Life in the Slow Lane

Once you made the above modifications, you will enjoy life in the slow metabolic lane.  Various nuts and little round cheeses is my way of snacking and when we go out, I drink either nealco Zlatý Bažant (Golden Pheasant) or Bavaria (yum), or Diet Cola (yech).

Swimming

The other important thing is exercise - the more the merrier.  I prefer swimming, since gym machines always hurt my hands and I need my fingers for typing.  So I swim about 3km per week - it sure helps living in a hot country, although it is rather chilly today - only 34 Celsius!