[SOLVED] trying to read and display data from ttyUSB0
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I´m trying to read and display a data string from ttyUSB0 with a Raspberry Pi. The data is a decimal number. There are two conditions 1) the number is static but repeated over and over or 2) continuously changing. The command stty raw -echo < /dev/ttyUSB0; cat -vte /dev/ttyUSB0 produces this typicaly on the terminal line ^@^X^XM-^XM-f^XM-^F~M-^XM-xM-^X^^^@^X^XM-^XM-f^XM-^F~M-^XM-xM-^X^^^@^X^XM-^XM-f^... Ultimately I would like to display this number on the terminal screen in a large high contrast font. There may be easier better ways to access the data string... tried using Python Serial module but couldn't get it to work... newbie retread here - simple is good!
Thanks for your help
I would use a terminal program like minicom to make sure the serial port settings are correct and that you can actually see the number. I assume that the number is ASCII and not binary.
I think using python or other programming language and not bash would be a better idea.
I´m trying to read and display a data string from ttyUSB0 with a Raspberry Pi. The data is a decimal number. There are two conditions 1) the number is static but repeated over and over or 2) continuously changing. The command stty raw -echo < /dev/ttyUSB0; cat -vte /dev/ttyUSB0 produces this typicaly on the terminal line ^@^X^XM-^XM-f^XM-^F~M-^XM-xM-^X^^^@^X^XM-^XM-f^XM-^F~M-^XM-xM-^X^^^@^X^XM-^XM-f^... Ultimately I would like to display this number on the terminal screen in a large high contrast font. There may be easier better ways to access the data string...
You didn't post a number, but what looks like a string of garbage. I'd go with michaelk's suggestion of playing with minicom first, to make sure you have your baud rate and such things set correctly first. However, if you pipe that through "od -x", you do get a series of numbers in octal (see man page on OD for what other options you have).
Quote:
tried using Python Serial module but couldn't get it to work
Saying "couldn't get it to work" tells us nothing; you don't say what you did/tried, post the code, or give us an example of what you actually want to see when the string is decoded and formatted. We can't guess.
Quote:
newbie retread here - simple is good!
Lots of examples on how to do this; read the "Question Guidelines" link in my posting signature, for code samples in many languages.
Perl:
Code:
#!/bin/perl
use Device::SerialPort;
my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->baudrate(9600); # Configure this to match your device
$port->databits(8);
$port->parity("none");
$port->stopbits(1);
while (1) {
my $char = $port->lookfor();
if ($char) {
print "Received $char \n";
}
$port->lookclear;
}
Python:
Code:
import serial
port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=3.0)
while True:
port.write("\r\nSay something:")
rcv = port.read(10)
port.write("\r\nYou sent:" + repr(rcv))
I think you'll want to follow TB0ne's python example. I could only contribute C program form, which I do not believe would serve you well here.
The question then is what is the nature of these characters? Are they printable ASCII or non-printable?
When you say "simple is good!", then I say, avoid all the dressing, do not worry yet about a large high contrast font. There are ways to do that, but first tackle reading and parsing the value from the serial USB, printing it out in simple form, and then fixing up the visual parts of it.
Thanks for the post giving what you've tried. Are you presently stuck, or has the recommendations to this point given you enough to work with to take some next possible steps?
Thanks for the post giving what you've tried. Are you presently stuck, or has the recommendations to this point given you enough to work with to take some next possible steps?
Presently still stuck. When I try to run my python script I get nothing and must abort script
You have indeed given me much food for thought - will research minicom
Yes of course I should have given better details of my attempts at python
Code:
#!/usr/bin/env python
import time
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
while 1:
x=ser.readline()
print (x),
Please use CODE tags when posting code, to preserve formatting and make things readable. And see the Python example posted, because you're using single-quotes instead of double for the device, and can probably try to omit the parity, stop-bits, and bit-count, since 8/N/1 is pretty much the 'standard' for serial since the dawn of time.
Quote:
Originally Posted by RonHof
I don´t know. Hopefully I will be able to figure that out once I see what is there.
You posted a string; if you can't see what's there, how did you post it? And re-read my previous post, where I mention the "od" command, and it's variations. Decoding what you get isn't hard, but again, you need to VERIFY your serial settings FIRST, by using minicom. If you can see legible data from the screen, you then have a starting point. Trying to write code and guessing as to what you're going to get/see makes things 1000% harder.
You don't say what hardware is generating this serial data...could very well be documented in whatever user-manual they have, along with correct serial settings. And you don't say what you're connecting to on the Linux side of things (presumably a USB-to-serial adapter), nor what version/distro of Linux you're using, or what kind of display you want the data displayed on. Lacking a good number of details; we can't guess.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.