You are not logged in.

#1 2009-10-29 04:36:49

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] How To Read Data Over /dev/ttyUSB0

So...I am using a serial device connected to a Keyspan Serial-to-USB adapter and plugged in to my computer's USB port. The device is constantly sending 115200 baud data over UART, with no parity, 8 data bits, 1 stop bit, no flow control, and no handshaking. I can read the data just fine on my Windows machine (once I install the Keyspan driver). On Arch64, I notice that when I plug it in, /dev/ttyUSB0 shows up so I believe that means the kernel is recognising the device and already has drivers to handle it. I would like to simply see the output of the device through my terminal emulator so I have written the following script....

stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb
while true; do
        read LINE < /dev/ttyUSB0
        echo $LINE
done

When I run this script as root, however, I get no output. I have also tried appending "-icanon min 1 time 1" to the stty arguments list but also to no avail. Can anyone help? Thanks!!

Last edited by tony5429 (2009-10-30 20:59:30)

Offline

#2 2009-10-29 12:25:28

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

hmmm - 'read' might be abit choosy for the end-of-line character ...
Why not try:
cat </dev/ttyUSB0
... though you _may_ screw up your terminal if there are escape-sequences coming through ...

Offline

#3 2009-10-29 21:53:59

R00KIE
Forum Fellow
From: Between a computer and a chair
Registered: 2008-09-14
Posts: 4,734

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

I have tried to make that work too before ... but I did put it on hold (wasn't successful) but it would be great to know how to do it ... it would be very handy because it would allow me to get some printscreens from oscilloscopes with a serial port.

At the time I did try to fiddle with stty, setserial, and either minicom or gtkterm (can't remember which one) but no luck there.
I remember stty and setserial had way too many options when compared with the simple setup windows and the oscilloscopes have ...


R00KIE
Tm90aGluZyB0byBzZWUgaGVyZSwgbW92ZSBhbG9uZy4K

Offline

#4 2009-10-29 22:45:52

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Thanks, all for the responses!

perbh: I wasn't sure exactly how to implement your suggestion. If the cat line doesn't use the variable, "$LINE", how can "echo $LINE" work? I am probably missing something simple...

Well, I have had a little success. The following script creates blank lines whenever data comes in through the usb device. Basically the device sends FFT data whenever it hears a sound which is greater than a certain threshold, so when I snap my fingers near it I see the FFT data show up in Terminal on Windows. Now, with the script below, if I snap my fingers my Linux machine outputs a bunch of blank lines so basically I just see my linux terminal emulator scrolling whenever I snap my fingers tongue Does anyone have any ideas how to get the actual data on Linux? Thanks again!

stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb
while true; do
        read -n 8 LINE < /dev/ttyUSB0
        echo $LINE
done

Offline

#5 2009-10-30 02:52:42

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

There must be a description somewhere of the data ... it could be plain binary (in which case read/cat might not show up much, unless you use 'cat -v' (ie 'print' non-printing characters). It the data arrives as a string - it most probably is terminated by CR and LF. However - untill we know what the data is like, its difficult to say which is the most correct way.
I have had quite a lot of success with 'minicom' in the past ...

So - give us a brief description of the data and we'll see what we can do ...

Offline

#6 2009-10-30 03:09:21

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Awesome; so I should try... "cat -v < /dev/ttyUSB0" and remove the "echo $LINE" line? Well, I'll try that out and get back to you.

As for the data, it is just straight ASCII. The device itself is an Intel 8051 microcontroller development kit and I'm just converting the RS232 serial port output on the board to USB so I can read the data on my laptop.

Offline

#7 2009-10-30 03:44:15

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

You might want to set raw mode so you can read one byte at a time - otherwise the string may not be 'released' until it encounters a LF ...

Offline

#8 2009-10-30 05:07:03

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Hrm... how do I set it to raw mode?

Offline

#9 2009-10-30 13:49:41

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

man setserial or man stty

Offline

#10 2009-10-30 14:05:07

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Ah; is the "-icanon min 1 time 1" argument what I want?

Offline

#11 2009-10-30 14:10:08

wuischke
Member
From: Suisse Romande
Registered: 2007-01-06
Posts: 630

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Why not use minicom?

Offline

#12 2009-10-30 14:10:19

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Hrm.... I tried the following but nothing showed up...

stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -icanon min 1 time 1
while true; do
        cat -v < /dev/ttyUSB0
done

With the following, I'm getting the same blank lines popping up whenever I snap.

stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -icanon min 1 time 1
while true; do
        read -n 8 LINE < /dev/ttyUSB0
        echo $LINE
done

Any other ideas? Thanks again for your help!

Offline

#13 2009-10-30 14:12:19

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

I tried minicom and have to admit I felt the interface and configuration was somewhat difficult to work with (I know...I should appreciate the lightweight feel of it...)

Offline

#14 2009-10-30 14:23:41

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

When in 'cooked' mode (which is the default), you wont see anything until it finds a LF in the stream
Instead of "-icanon" above, use "-cooked"

But - as you have been told 2 or 3 times by now - try out 'minicom' first!
If that works - ie at least you can 'see' some activity, then you can go on to something more adventurous

[edit]
Was busy checking 'man stty' and didnt see your last post ...
Basically, in 'minicom' what you need is "ctrl/a - o"
You just have to forget about all the modem-specific detail

Other than that - doing a little bit of c-programming on a serial port is a no-brainer ...
But personally - I alway use minicom/stty/cat first to make sure something is actually happening,
then I go and tailormake it for _my_ personal use.

Last edited by perbh (2009-10-30 14:28:23)

Offline

#15 2009-10-30 14:43:13

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

"-cooked" didn't change anything for the cat-based script nor for the read-based script. I'll give minicom a shot using your suggestion; thanks!

Offline

#16 2009-10-30 14:54:58

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Sweet! It works! Thank you very much!

Offline

#17 2009-10-30 14:58:13

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

If solved can you mark the thread as solved?


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#18 2009-10-30 20:59:42

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How To Read Data Over /dev/ttyUSB0

Done.

Offline

Board footer

Powered by FluxBB