15 January 2014

Talking to your modem under embedded linux

With the right drivers, modems chipsets show up as USB devices under linux, usually /dev/ttyACM*. When everything is working right, pppd just takes care of all of this an you have a nice easy ip layer. But when you are trying to figure out what your device is doing from the datasheet, you just want to type commands to the modem and get back its responses.

Googling for this resulted in approximately one billion ways to read and write from USB modems, including cat, cu, socat, getty, minicom, putty and, god help us, perl. These are distinguished by either a) not being bi-direction or b) not installed on a generic busybox-based embedded system. What you really want to use is microcom. It is built in to busybox, it works, and it's actually easier to use that most of the other options.

microcom [-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY
Copy bytes for stdin to TTY and from TTY to stdout
        -d      Wait up to DELAY ms for TTY output before sending every
                next byte to it
        -t      Exit if both stdin and TTY are silent for TIMEOUT ms
        -s      Set serial line to SPEED
        -X      Disable special meaning of NUL and Ctrl-X from stdin

The -t option is actually quite clever; it's very easy to disable control sequences and then get stuck. This bombs you out if nobody talks for x milliseconds. Example:

$ microcom -t 3000 /dev/ttyACM2

+CFUN: (0,1,4-6),(0,1)


If you really want to use socat I guess you can. In the below I seem to have forgotten whatever magic option is required to make socat send CRLF instead of just LF, so it doesn't actually work.

D4-11-D6-00-00-16:~ $ socat - /dev/ttyACM2



Notes on using the shell

Since I mostly use the shell under embedded linux, when I say /bin/sh I really mean /bin/ash, the Almquist shell, which is compiled into BusyBox and therefore pretty cheap resource-wise.

Maybe there are some people out there who like shell syntax. Suffice it to say I am not one of them. 


Some helpful links for myself:
The secret to successful shell Googling is knowing that "[" is pronounced "test".

Code Chunks

I can't believe I have to actually write down how to make a for loop.

POSIX-compatible For Loop

while [ $i -lt $max ]; do
  echo "$i"
  true $(( i++ ))

Assuming I can edit blogger posts, I'll edit this later.

Hilbert Transforms

This talk on Signal Processing Problems in Genomics by P. P. Vaidyanathan is an easy-to-read introduction to how signal processing techniques such as Fourier analysis are used in Genomics.

Complex to Real has a series of tutorials on signal processing topics. The tutorial on the Hilbert transform and its application to computing the envelope of a function is the best I've found.