NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Reading USB port in Linux environment

January 28, 2010
by Mujda
Mujda's Avatar

I am trying to read the USB data sent from the temperature sensor project on a PC running Ubuntu. The temperature sensor project works fine (LCD displays temp as it should), but it should also be sending temp data back through the UART/USB cable.

According to other threads, I should be able to see the output with:

tail -f /dev/ttyUSB0 (or whichever the correct USB port is at the time)

The 'character special file' exists (when the USB/UART cable is plugged in), and the ATmega programs fine (which both writes and reads to the port) so there doesn't seem to be any hardware problems.

I understand there are other routes to read the USB port (specifically Python), but I've had problems installing the 'Serial' plug-in, and I really like the simplicity of the above method (assuming it can actually work). I think I'll actually understand the inner workings better.

Can anyone give my any guidance on how to trace the problem through?

Thanks

January 28, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Mujda,

Welcome to the forums!

The approach you describe can work, but it relies on you first setting up the serial port correctly with the "stty" command. For example, see wayward's post here where he recommends issuing this command:

stty -F /dev/ttyUSB0 115200 -parity -cstopb

before doing any cat / tail / etc. commands This sets the serial port to 115200 baud, 8N1 mode. Without this command, the serial port character device may not be set up correctly, so you might see nothing (or gibberish). Let us know if that works!

Also, if you're not 100% sure that your running program is actually sending data over the serial / USB port, then you may want to try something like Minicom first. We have instructions on our Servo Squirter tutorial page if you scroll down to Serial Port Communications.

Mike

January 28, 2010
by pbfy0
pbfy0's Avatar

there's also my perl script for reading serial. I'm pretty sur it works on linux, but it's only been tested on mac.

January 29, 2010
by treymd
treymd's Avatar

install PuTTY, it's a terminal emulator that also will read/write to your USB/Serial converter cable. I use it because, well, it's easy. It should be in Ubuntu's repositories, I know Fedora had it. Just change the connection type to serial.. enter the port (ttyUSB0) << USUALLY BUT NOT ALWAYS! and the speed at which the nerdkit is set to communicate (115200). Save your configuration for next time, as only one program can occupy the device at a time, which means you will need to close putty in order to upload code to the chip.

Another benefit I've seen with putty is that most of the defaults are perfect for our needs. Many of the packaged terminal emulators in Linux have some setting here or there that just make things difficult.

There will certainly be a time when you need programmatic access to the port and you will outgrow putty, but it will still be useful as a debugging tool. I highly recommend it!

BTW for you folks tethered to Bill Gates, Putty is also available for Windows. Google putty, it's the first hit.

January 31, 2010
by Mujda
Mujda's Avatar

Thanks, those responses look very helpful, and will try them out in the next couple of days.

I have a question about buffering. The temp controller project dumps all this data through the UART to the USB connection. If you don't read the USB port, eventually either the data gets lost, or an error condition is flagged. How does the MCU know the data hasn't been read at the other end, so shouldn't send any more data? Is it the UART data overrun flag?

January 31, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Mujda,

The way we've set it up, there is no mechanism to know from the microcontroller end whether the USB port is being drained. The UART data overrun flag can only tell whether you are writing to the UART data register too fast -- but once it goes over the wire to the PC, it's considered "sent" by the microcontroller.

In practice, the USB-Serial buffer is plenty big compared to the baud rate, so this may not be an issue.

But if you want to make sure that some program is running on the PC-side to make sure it's being read, then I think you basically need to either send back explicit acknowledgements to the microcontroller every so often, or explore some flow control mechanisms. (User mikedoug had explored XON/XOFF flow control -- he might be able to help you out.)

Mike

February 02, 2010
by Mujda
Mujda's Avatar

Success! Thanks for the input.

I had no luck on Linux using 'tail' after I'd set the port using 'stty' as described above, not sure why. I simply get nothing out, not even garbage.

I did have success with Putty (in Vista). I'm getting a nice set of temp readings. Finally, just 2 questions relating to this: 1) Under 'Device Manager' and LPT&COM Ports, there are some automatically configured port settings. Does Putty ignore these, and are they correct anyway? Do the correct port settings refer to the USB port, or the UART settings in the MCU? 2) My next step would be to use Python, but Python Serial totally refuses to install on Vista. Does anyone have any experience of this?

Thanks,

Mujda

February 02, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Mujda,

I'm not positive what the settings you see on the device manager are. If I had to guess those are probably the default setting of the port, Putty just ignores them if you choose to use something different. The settings for serial communication have to be the same on both ends for the communication to work. Basically the computers serial port and the MCU have to agree to use the same baud rate and other settings.

I have never installed pySerial on Vista, but I can't think of a reason it should not work. Did you try running the installer as administrator?

Humberto

Post a Reply

Please log in to post a reply.

Did you know that you can use printf and scanf functions to talk to your computer from your USB NerdKit? Learn more...