About my blog

I am multithreaded, and sometimes the threads get tangled.

tagline


a Studiolab production.

FIY

food design course at TUDelft
I blog in Italian
I have a LinkedIn profile
my lab is Studiolab!
I love Jasper van Kuijk's product usability weblog

the wind I am enjoying


Disclaimer

De meningen ge-uit door medewerkers en studenten van de TU Delft en de commentaren die zijn gegeven reflecteren niet perse de mening(en) van de TU Delft. De TU Delft is dan ook niet verantwoordelijk voor de inhoud van hetgeen op de TU Delft weblogs zichtbaar is. Wel vindt de TU Delft het belangrijk - en ook waarde toevoegend - dat medewerkers en studenten op deze, door de TU Delft gefaciliteerde, omgeving hun mening kunnen geven.

Posted in July 2009

making the FoxBoard talk to Arduino

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;
-brkint -imaxbel

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:

cat </dev/ttyUSB0

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
-opost
-isig -icanon

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 :)

© 2011 TU Delft