About my blog
I am multithreaded, and sometimes the threads get tangled.
- December 2012 (1)
- March 2012 (1)
- December 2011 (1)
- May 2011 (1)
- April 2011 (1)
- March 2011 (1)
- January 2011 (1)
- December 2010 (1)
- October 2010 (2)
- September 2010 (1)
- June 2010 (1)
- May 2010 (1)
- April 2010 (3)
- February 2010 (2)
- November 2009 (2)
- October 2009 (1)
- September 2009 (1)
- August 2009 (2)
- July 2009 (1)
- June 2009 (2)
- May 2009 (1)
- April 2009 (2)
- March 2009 (6)
- February 2009 (5)
- January 2009 (3)
a Studiolab production.
the wind I am enjoying
Posted in July 2009
Suppose you have a sketch loaded on your Arduino, and suppose that you need some serial communication. You try it out on your Windows box, and all is happiness. Then you attach the cable to the Foxboard (a single board computer running Linux) and nothing works anymore. Why?
Because apparently the default settings of the serial port are wrong for your purposes.
To set the serial port your way, you have to use the wonderful stty command, something that our hairy ancestors used to do when setting up terminals in the Lascaux caves. First of all, run the dmesg command. You will see lots of stuff, and at the end you should see something like this
crisv10_irq dbg: ctr_status_irq, controller status: host_mode started
crisv10_irq dbg: ctr_status_irq, controller status: host_mode started running
usb 1-1: new full speed USB device using hc-crisv10 and address 4
ftdi_sio 1-1:1.0: FTDI USB Serial Device converter detected
drivers/usb/serial/ftdi_sio.c: Detected FT232BM
usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0
This means that the USB serial converter in the Arduino is recognized, the driver has been loaded, and the Arduino is mapped by ttyUSB0. I have marked in bold the most important part. It mans that the Arduino’s serial port is now available as the /dev/ttyUSB0 file, for reading and writing.
To see the current configuration of the port type stty -F /dev/ttyUSB0
[root@vdhelm1 /dev]118# stty -F /dev/ttyUSB0
speed 9600 baud;
This is a quite typical configuration, good for controlling your UNIX minicomputer from a VT100 terminal, and if we were on a time machine to Dr. Who land it would be probably the perfect configuration. For our purposes, though, we need to dumb it down a bit.
I have noticed that to make things work "like on Windows" I have to kill the local echo. This is done with the command
stty -F /dev/ttyUSB0 -echo
and then things work pretty nicely. Send stuff to the Arduino like this:
echo "whatever" >/dev/ttyUSB0
and see on screen what the Arduino is sending back like this:
extra info: if things work strangely, or don’t work at all, one reason can be that your application on the Foxboard side is sending out the right stuff, but the terminal is not sending it to the Arduino because it is in "canonical" mode. Canonical mode means, among other things, that information is sent one line at a time – which really made a lot of sense for controlling printers. But it may be that you need your data to reach the Arduino as soon as you send it. Additionally, binary protocol data cannot really be divided in "lines". What you want is a device that will just shoot stuff out on the wire as soon as you send it. To get just that, you need to put your device in "raw" mode.
[root@vdhelm1 /mnt/flash/root]116# stty raw -F /dev/ttyUSB0
[root@vdhelm1 /mnt/flash/root]116# stty -F /dev/ttyUSB0
speed 9600 baud;
min = 1; time = 0;
-brkint -icrnl -imaxbel
the parts in bold are the ones you type. Without delving too much in the output of stty, notice that "raw" is actually a macro for a bunch of configuration options. The "-" means NOT, so these are all character processing options having been switched off. If you want to find out why this whole wacky tty mess exists, and why it makes a sort of sense (historical, at any rate), there is a great page about The TTY demistified.
caveat: if, by mistake, you set your current tty to raw mode, things will become very funny indeed. Time to get another tty 🙂