LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-19-2019, 11:33 AM   #1
RonHof
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Rep: Reputation: 1
trying to read and display data from ttyUSB0


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
 
Old 09-19-2019, 11:53 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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.
 
Old 09-19-2019, 12:15 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,716

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by RonHof View Post
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))
 
1 members found this post helpful.
Old 09-19-2019, 12:32 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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.
 
Old 09-19-2019, 12:33 PM   #5
RonHof
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Original Poster
Rep: Reputation: 1
Yes of course I should have given better details of my attempts at python

#!/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),
 
Old 09-19-2019, 12:38 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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?
 
Old 09-19-2019, 12:39 PM   #7
RonHof
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Original Poster
Rep: Reputation: 1
[QUOTE=rtmistler;6038338]

The question then is what is the nature of these characters? Are they printable ASCII or non-printable?

I don´t know. Hopefully I will be able to figure that out once I see what is there.
 
Old 09-19-2019, 12:45 PM   #8
RonHof
LQ Newbie
 
Registered: Aug 2006
Posts: 23

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by rtmistler View Post
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

Thanks
 
Old 09-19-2019, 01:16 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,716

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by RonHof View Post
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.
 
  


Reply

Tags
code, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ connecting to /dev/ttyUSB0 and r/w binary or hexadecimal data Germanunkol Programming 5 08-24-2011 05:28 PM
Using Piklab and trying to burn thru /dev/ttyUSB0 ritsz Linux - Software 4 02-05-2011 04:20 AM
How do I read and write to /dev/ttyUSB0 in C? LinuxTexan Linux - Newbie 12 11-24-2010 03:03 AM
Importing data from /dev/ttyUSB0 RVDowning Mandriva 1 06-29-2006 08:06 PM
serial to USB, trying to read from /dev/ttyUSB0 in C code newguy21 Programming 1 09-29-2004 01:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration