Hi
I am just trying to analyze (parse) data from the serial port (I have
connected GPS receiver to the ttyS0, so I can read ASCII characters in
the CSV form on the serial port 1).
I am doing this just to understand how Python works (yes, you can call
me Python/Linux newbie :)
My environment is Fedora Core 4, Python 2.4.1
CSV alone (to read CSV data from the file) and py-serial alone (to
read data from the serial port) are working flawlessly.
Even I was trying to google through this group and through the
Internet, I am not able to read (and parse) CSV data directly from the
serial port.
data from my serial port (using py-serial) I am getting this way: import serial s = serial.Serial(port=0,baudrate=4800, timeout=20) s.readline()
'$GPRMC,101236.331,A,5026.1018,N,01521.6653,E,0.0, 328.1,230805,,*09\r\n'
my next intention was to do something like this:
import csv
r = csv.reader(s.readline())
for currentline in r:
if currentline[0] == '$GPRMC':
print currentline[2]
print currentline[4]
but it does not work
Thanks for your comments
Petr Jakes 14 4017
McBooCzech wrote: r = csv.reader(s.readline())
csv.reader expects an iterable, not a str.
--
Michael Hoffman
So do I have to save to the file first and analyze later on?
Or there is an other way how to process it (read from the serial and
analyze data) on the fly?
Petr Jakes
McBooCzech wrote: So do I have to save to the file first and analyze later on?
Certainly not.
Or there is an other way how to process it (read from the serial and analyze data) on the fly?
I've never used py-serial, so I was hoping someone else would jump in
with the best way to do this (or perhaps you can find it in the
documentation). One way would be to use the iter() built-in to make it
into an iterable, like this:
iterable = iter(s.readline, "")
Note that you do not call s.readline() here; you are only supplying the
bound method itself as an argument. iter() will call it later. I'm
assuming that s.readline returns an empty string when there is no more
input. Please replace that with whatever end sentinel it actually uses.
(None?)
Another way would be to use s.readlines(), which returns a list, which
is iterable, but you would have to wait until the entire list were
collected before beginning processing.
--
Michael Hoffman
On 2005-08-23, McBooCzech <pe**@tpc.cz> wrote: import serial s = serial.Serial(port=0,baudrate=4800, timeout=20) s.readline()
'$GPRMC,101236.331,A,5026.1018,N,01521.6653,E,0.0, 328.1,230805,,*09\r\n'
my next intention was to do something like this:
import csv r = csv.reader(s.readline()) for currentline in r: if currentline[0] == '$GPRMC': print currentline[2] print currentline[4]
but it does not work
For something that simple (the data itself doesn't contain any
commas), it's probably easier to use the string's split method
rahter than CSV. Try something like this:
line = s.readline()
words = line.split(',')
--
Grant Edwards grante Yow! Now KEN and BARBIE
at are PERMANENTLY ADDICTED to
visi.com MIND-ALTERING DRUGS...
On Tue, 23 Aug 2005 14:11:59 +0100, Michael Hoffman
<ca*******@mh391.invalid> declaimed the following in comp.lang.python: Note that you do not call s.readline() here; you are only supplying the bound method itself as an argument. iter() will call it later. I'm assuming that s.readline returns an empty string when there is no more input. Please replace that with whatever end sentinel it actually uses. (None?)
Another way would be to use s.readlines(), which returns a list, which is iterable, but you would have to wait until the entire list were collected before beginning processing.
The latter wouldn't work in this situation -- as the only way to end
the list is to disconnect the serial port <G>
Most GPS receivers, when set to send the messages, will do so at a
rate of one message every 1 or 2 seconds, as I recall.
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> <
Sorry, I did not mentioned the data flow from the serial port is
permanent/continuous (as long as the GPS receiver is connected to the
serial port). The input data are commning every second, they are comma
separated and they are looking like:
$GPGGA,174525.617,5026.1080,N,01521.6724,E,1,05,1. 8,306.5,M,,,,0000*0B
$GPGSA,A,3,02,09,05,06,14,,,,,,,,3.6,1.8,3.1*31
$GPGSV,3,1,09,30,74,294,,05,65,093,49,06,40,223,32 ,02,39,089,49*78
$GPRMC,174525.617,A,5026.1080,N,01521.6724,E,0.0,0 05.8,230805,,*0A
etc.... From the rows they are begining with $GPRMC I want to get following
data only (positions 2,4,6)
174525.617
5026.1080
01521.672
This (according to your suggestions) is my code which works for me
import serial
s = serial.Serial(port=0,baudrate=4800, timeout=20)
while 1:
line = s.readline()
words = line.split(',')
if words[0]=="$GPRMC":
print words[1], words[3], words[5]
I just wonder if there is some beter (or as you are saying "more
pythonic":) aproach how to write such a piece of code.
--
Petr Jakes
McBooCzech wrote: Sorry, I did not mentioned the data flow from the serial port is permanent/continuous (as long as the GPS receiver is connected to the serial port). The input data are commning every second, they are comma separated and they are looking like:
$GPGGA,174525.617,5026.1080,N,01521.6724,E,1,05,1. 8,306.5,M,,,,0000*0B $GPGSA,A,3,02,09,05,06,14,,,,,,,,3.6,1.8,3.1*31 $GPGSV,3,1,09,30,74,294,,05,65,093,49,06,40,223,32 ,02,39,089,49*78 $GPRMC,174525.617,A,5026.1080,N,01521.6724,E,0.0,0 05.8,230805,,*0A etc....
From the rows they are begining with $GPRMC I want to get following data only (positions 2,4,6) 174525.617 5026.1080 01521.672
This (according to your suggestions) is my code which works for me
import serial s = serial.Serial(port=0,baudrate=4800, timeout=20) while 1: line = s.readline() words = line.split(',') if words[0]=="$GPRMC": print words[1], words[3], words[5]
I just wonder if there is some beter (or as you are saying "more pythonic":) aproach how to write such a piece of code.
That code is quite tidy. You could save yourself the split on lines that
weren't of interest, though frankly this isn't essential - this task
won't use 1% of CPU on almost any computer built in the last five years.
But, if you are interested in seeing other solutions you might consider
it, and it does avoid the split when it's not necessary.
while 1:
line = s.readline()
if line.startswith("$GPRMC"):
words = line.split(",")
print words[1], words[3], words[5]
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Steve Holden <st***@holdenweb.com> writes: McBooCzech wrote:
[...] $GPRMC,174525.617,A,5026.1080,N,01521.6724,E,0.0,0 05.8,230805,,*0A etc....
[...] s = serial.Serial(port=0,baudrate=4800, timeout=20) while 1: line = s.readline() words = line.split(',') if words[0]=="$GPRMC": print words[1], words[3], words[5] I just wonder if there is some beter (or as you are saying "more
pythonic":) aproach how to write such a piece of code.
That code is quite tidy. You could save yourself the split on lines that weren't of interest, though frankly this isn't essential - this task won't use 1% of CPU on almost any computer built in the last five years. But, if you are interested in seeing other solutions you might consider it, and it does avoid the split when it's not necessary.
while 1: line = s.readline() if line.startswith("$GPRMC"):
"$GPRMC," would be better, -- not to match something like "$GPRMC174...".
--
Sergei.
Sergei, I do not realy understand your comment??? Am I missing
something?
BTW, is there some possibility to address lists like:
print words [1; 3; 5]
instead of
print words[1], words[3], words[5]
Petr Jakes
On 23 Aug 2005 12:59:03 -0700, "McBooCzech" <pe**@tpc.cz> declaimed the
following in comp.lang.python: Sergei, I do not realy understand your comment??? Am I missing something?
BTW, is there some possibility to address lists like:
print words [1; 3; 5] instead of print words[1], words[3], words[5]
Mix & Matching from two messages <G>
2>$GPRMC,174525.617,A,5026.1080,N,01521.6724,E,0.0 ,005.8,230805,,*0A
2>etc....
2>
2>>From the rows they are begining with $GPRMC I want to get following
2>data only (positions 2,4,6)
2>174525.617
2>5026.1080
2>01521.672
2>
Interesting that you don't want to handle conditions crossing
Greenwich/dateline, or equator (since you ignore the N/S, E/W tags.
2>This (according to your suggestions) is my code which works for me
2>
2>import serial
2>s = serial.Serial(port=0,baudrate=4800, timeout=20)
2>while 1:
2> line = s.readline()
2> words = line.split(',')
2> if words[0]=="$GPRMC":
2> print words[1], words[3], words[5]
Well... Let's see
#open port, etc...
while True:
line = s.readline()
if line.startswith("$GPRMC"):
(msg, timestamp, junk,
raw_latitude, NS_indicator,
raw_longitude) = line.split(",")[:6]
print timestamp, raw_latitude, raw_longitude line = "$GPRMC,174525.617,A,5026.1080,N,01521.6724,E,0.0, 005.8,230805,,*0A" (msg, timestamp, junk,
.... raw_latitude, NS_indicator,
.... raw_longitude) = line.split(",")[:6] print timestamp, raw_latitude, raw_longitude
174525.617 5026.1080 01521.6724
-- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.netcom.com/> <
McBooCzech enlightened us with: This (according to your suggestions) is my code which works for me
import serial s = serial.Serial(port=0,baudrate=4800, timeout=20) while 1: line = s.readline() words = line.split(',') if words[0]=="$GPRMC": print words[1], words[3], words[5]
I just wonder if there is some beter (or as you are saying "more pythonic":) aproach how to write such a piece of code.
You could use regular expressions instead. And to make it even more
pythonic, replace the "while" and the "line = s.readline()" with
"for line in s:"
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
On 2005-08-24, Sybren Stuvel <sy*******@YOURthirdtower.com.imagination> wrote: import serial s = serial.Serial(port=0,baudrate=4800, timeout=20) while 1: line = s.readline() words = line.split(',') if words[0]=="$GPRMC": print words[1], words[3], words[5]
I just wonder if there is some beter (or as you are saying "more pythonic":) aproach how to write such a piece of code. You could use regular expressions instead.
"There is a programmer who has a problem to solve. He decides
to use regular expressions. Now he has two problems."
And to make it even more pythonic, replace the "while" and the "line = s.readline()" with "for line in s:"
Serial port objects aren't iterable.
--
Grant Edwards grante Yow! I was making donuts
at and now I'm on a bus!
visi.com
McBooCzech wrote: This (according to your suggestions) is my code which works for me
import serial s = serial.Serial(port=0,baudrate=4800, timeout=20) while 1: line = s.readline() words = line.split(',') if words[0]=="$GPRMC": print words[1], words[3], words[5]
I just wonder if there is some beter (or as you are saying "more pythonic":) aproach how to write such a piece of code.
import csv
from serial import Serial
port = Serial(port=0, baudrate=4800, timeout=20)
for row in csv.reader(iter(port.readline, None)):
if row[0] == "$GPMRC":
print row[1], row[3], row[5]
--
Michael Hoffman
Thanks you all, guys, for your suggestions and help. Everything now
works great :)
Regards
Petr Jakes This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Justin Johnson |
last post by:
Hello,
I was wondering if anyone has seen this behavior before for the
process.py
module available at http://starship.python.net/crew/tmick/. I've been
using it quite well on windows 2000...
|
by: John J. Lee |
last post by:
I'm tearing my hair out at what seems like weird import behaviour I'm
getting from Python's stdlib test script, regrtest.py (not for the
first time: seem to have forgotten the resolution from last...
|
by: zelzel.zsu |
last post by:
I am a new learner of Python Programming Language.
Now. I am reading a book.
In the section relating to module, I see an example.
the directory tree looks like below:
root\
system1\...
|
by: svenn.are |
last post by:
Hi,
has anybody thought of / already used graphviz to convert the output of
trace.py into a graph? I looked at PyUMLGraph, but 1. PyUMLGraph does
not successfully create a dot file, and 2. I...
|
by: kristian.hermansen |
last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
|
by: Sandro Dentella |
last post by:
Hi everybody,
I'm trying to fix the packaging of a very simple package, but some problem
show me that I have not well understood the whole mechanism
The structure of my package (some debug...
|
by: jmalone |
last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has
AIX and does not want to install Python). The python script is pretty
simple (the only things it imports are sys and socket)....
|
by: TiNo |
last post by:
Hi,
I have installed python two days ago on a USB memory stick (I am on
the move and have no laptop.) I am on windows computers, mostly XP,
all the time.
Now, after pluging it in to a...
|
by: korean_dave |
last post by:
From command Prompt, i type in a script, "tryme.py".
This, instead, brings up PythonWin editor and Interactive Window.
Path variable is "C:\Python24". (I need Python 2.4 installed, not 2.5)
...
|
by: rajasankar |
last post by:
Hi,
I am using Jython based application and trying to use inspect.py in the python files.
Here is my code
import inspect,os,sys,pprint,imp
def handle_stackframe_without_leak(getframe):
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |