Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 18th, 2006, 01:15 AM
Petr Jakes
Guest
 
Posts: n/a
Default writing serial port data to the gzip file

I am trying to save data it is comming from the serial port continually
for some period.
(expect reading from serial port is 100% not a problem)
Following is an example of the code I am trying to write. It works, but
it produce an empty gz file (0kB size) even I am sure I am getting data
from the serial port. It looks like g.close() does not close the gz
file.
I was reading in the doc:

Calling a GzipFile object's close() method does not close fileobj,
since you might wish to append more material after the compressed
data...

so I am completely lost now...

thanks for your comments.
Petr Jakes
==== snippet of the code ====
def dataOnSerialPort():
data=s.readLine()
if data:
return data
else:
return 0

while 1:
g=gzip.GzipFile("/root/foofile.gz","w")
while dataOnSerialPort():
g.write(data)
else: g.close()

  #2  
Old December 18th, 2006, 02:15 AM
Leo Kislov
Guest
 
Posts: n/a
Default Re: writing serial port data to the gzip file


Petr Jakes wrote:
Quote:
I am trying to save data it is comming from the serial port continually
for some period.
(expect reading from serial port is 100% not a problem)
Following is an example of the code I am trying to write. It works, but
it produce an empty gz file (0kB size) even I am sure I am getting data
from the serial port. It looks like g.close() does not close the gz
file.
I was reading in the doc:
>
Calling a GzipFile object's close() method does not close fileobj,
since you might wish to append more material after the compressed
data...
>
so I am completely lost now...
>
thanks for your comments.
Petr Jakes
==== snippet of the code ====
def dataOnSerialPort():
data=s.readLine()
if data:
return data
else:
return 0
>
while 1:
g=gzip.GzipFile("/root/foofile.gz","w")
while dataOnSerialPort():
g.write(data)
else: g.close()
Your while loop is discarding result of dataOnSerialPort, so you're
probably writing empty string to the file many times. Typically this
kind of loop are implemented using iterators. Check if your s object
(is it from external library?) already implements iterator. If it does
then

for data in s:
g.write(data)

is all you need. If it doesn't, you can use iter to create iterator for
you:

for data in iter(s.readLine, ''):
g.write(data)

-- Leo

  #3  
Old December 18th, 2006, 08:05 PM
jim-on-linux
Guest
 
Posts: n/a
Default Re: writing serial port data to the gzip file




If someone hasn't already commented,

Aside from any other problems, the file you are
trying to write to is (opened)?? in the "w" mode.
Every time a file is opened in the 'w' mode,
everything in the file is deleted.

If you open a file in the 'a' mode, then
everything in the file is left untouched and the
new data is appended to the end of the file.

Your while loop is deleting everything in the file
on each loop with the 'w' mode.

try,
vfile = open('vfile', 'a')
rather than
vfile = open('vfile', 'w')

jim-on-linux
http:\\www.inqvista.com

Quote:
while 1:
g=gzip.GzipFile("/root/foofile.gz","w")
while dataOnSerialPort():
g.write(data)
else: g.close()


On Sunday 17 December 2006 20:06, Petr Jakes
wrote:
Quote:
I am trying to save data it is comming from the
serial port continually for some period.
(expect reading from serial port is 100% not a
problem) Following is an example of the code I
am trying to write. It works, but it produce an
empty gz file (0kB size) even I am sure I am
getting data from the serial port. It looks
like g.close() does not close the gz file.
I was reading in the doc:
>
Calling a GzipFile object's close() method does
not close fileobj, since you might wish to
append more material after the compressed
data...
>
so I am completely lost now...
>
thanks for your comments.
Petr Jakes
==== snippet of the code ====
def dataOnSerialPort():
data=s.readLine()
if data:
return data
else:
return 0
>
while 1:
g=gzip.GzipFile("/root/foofile.gz","w")
while dataOnSerialPort():
g.write(data)
else: g.close()
  #4  
Old December 18th, 2006, 10:25 PM
Petr Jakes
Guest
 
Posts: n/a
Default Re: writing serial port data to the gzip file

Maybe I am missing something. Expect data is comming continually to the
serial port for the period say 10min. (say form the GPS), than it stops
for 1 minute and so on over and over. I would like to log such a data
to the different gzip files.
My example was written just for the simplicity (I was trying to
demonstrate the problem, it was not the real code and I was really
tired trying to solve it by myself, sorry for the bugy example)

the better way how to write such a infinite loop can be probably:
===== 8< =====
g=0
x=0
while 1:
if not g:
x+=1
g=gzip.GzipFile("/root/foofile%s.gz" % x,"w")
data=dataOnSerialPort()
while data:
myFlag=1
g.write(data)
data=dataOnSerialPort():
else:
if myFlag:
g.close()
pring g
myFlag=0

But it looks like g.close() method does not close the file (while
trying to print the g object, it still exists)

Quote:
Your while loop is discarding result of dataOnSerialPort, so you're
probably writing empty string to the file many times. Typically this
kind of loop are implemented using iterators. Check if your s object
(is it from external library?) already implements iterator. If it does
then
>
for data in s:
g.write(data)
>
is all you need. If it doesn't, you can use iter to create iterator for
you:
>
for data in iter(s.readLine, ''):
g.write(data)
>
-- Leo
  #5  
Old December 18th, 2006, 11:35 PM
Petr Jakes
Guest
 
Posts: n/a
Default Re: writing serial port data to the gzip file

Hi Dennis,
thanks for your reply.
Dennis Lee Bieber napsal:
Quote:
Quote:
def dataOnSerialPort():
data=s.readLine()
>
Unless you are using a custom serial port module, that should be
s.readline()
sorry for the typo
Quote:
>
Quote:
if data:
return data
else:
return 0
>
This if statement is meaningless -- if "data" evaluates to false,
return a numeric value that evaluates to false.
I see, it is OK just to return data (or an empty string "")
Quote:
>
Quote:

while 1:
g=gzip.GzipFile("/root/foofile.gz","w")
while dataOnSerialPort():
g.write(data)
>
"data" is an uninitialized value here
Quote:
else: g.close()
>
And what is the purpose of closing the file if you immediately turn
around and create it again (assuming gzip.GzipFile() behaves as open()
does, a mode of "w" means delete the old file and create a new one.
There is NO exit from the above.
>
Since I can't read your mind with regards to some of your looping...
>
s = ... #somewhere you had to open the serial port
>
g = gzip.GzipFile("/root/foofile.gz", "w")
while True:
data = s.readline()
if not data: break
g.write(data)
g.close()
what I am trying to say is g.close() does not close the g file (try to
add the line "print g" after g.close())
Petr

 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles