473,545 Members | 2,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 dataOnSerialPor t():
data=s.readLine ()
if data:
return data
else:
return 0

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

Dec 18 '06 #1
4 2631

Petr Jakes wrote:
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 dataOnSerialPor t():
data=s.readLine ()
if data:
return data
else:
return 0

while 1:
g=gzip.GzipFile ("/root/foofile.gz","w" )
while dataOnSerialPor t():
g.write(data)
else: g.close()
Your while loop is discarding result of dataOnSerialPor t, 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

Dec 18 '06 #2

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

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


On Sunday 17 December 2006 20:06, Petr Jakes
wrote:
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 dataOnSerialPor t():
data=s.readLine ()
if data:
return data
else:
return 0

while 1:
g=gzip.GzipFile ("/root/foofile.gz","w" )
while dataOnSerialPor t():
g.write(data)
else: g.close()
Dec 18 '06 #3
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=dataOnSeri alPort()
while data:
myFlag=1
g.write(data)
data=dataOnSeri alPort():
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)

Your while loop is discarding result of dataOnSerialPor t, 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
Dec 18 '06 #4
Hi Dennis,
thanks for your reply.
Dennis Lee Bieber napsal:
def dataOnSerialPor t():
data=s.readLine ()

Unless you are using a custom serial port module, that should be
s.readline()
sorry for the typo
>
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 "")
>

while 1:
g=gzip.GzipFile ("/root/foofile.gz","w" )
while dataOnSerialPor t():
g.write(data)

"data" is an uninitialized value here
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

Dec 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1220
by: sandeepa | last post by:
Hello all I am using the serial port to receive data(7 bytes per second) from the microcontroller,receiving the data as a string and then splitting the string in two,to display 2 different values.Am using the Oncomm event to detect reception of data.All was working fine till recently. I then changed my controller program to send 7...
1
9152
by: ssc | last post by:
I'm new to C#, but have been doing embedded programming for years. I have an application that talks to an embedded radio on the serial port of my PC. I have most of the application running pretty well, but if I click a button before the radio sends its response from the previous command, things get ugly. I need the application to "lock"...
15
8175
by: xkenneth | last post by:
Hi, I'm writing a couple python applications that use the serial port (RS-232) quite extensively. Is there any way I can monitor all activity on the serial port and have it printed as the transactions occur? I'm trying to reverse engineer a microcontroller serial routine and I'd like to see any response the chip sends back. Regards, Ken
1
1498
by: Narjis | last post by:
Hi everybody? hope y'r all fine I need a small help.. can anyone give me a c++ program that reads the serial port data? (i.e. that data are burned in a microcontroller chip and i want to connect the chip to the PC and let the c++ program read this data) Regards to all..
2
7819
by: Nasif | last post by:
Currently I am writing a program which sends and receives messages through serial port to a device. I am using C# and Microsoft Visual studio 2005 for windows program. But my problem is when i try to write in serial port from my windows a Timeoutexception is thrown. I use SerialPort class in System.IO.Ports and for writing port i used write()...
2
3073
by: crampio | last post by:
Hello everyone, I generally look at Google and other websites before I post a question, but trust me I still cannot find and answer to this problem. I'm using VB.net. My problem being is that I don't know how to redirect the serial port output to a selected file. I looked in the forum but no luck. Here is the sequence of events. User...
3
8900
by: Ajinkya | last post by:
How can I poll for serial port data using javascript ?
6
11699
by: cnixuser | last post by:
Hello, I have a basic application written which is designed to data over a serial cable and then receive a response back. I am not getting any triggers to my data received event. I have tried connecting the pc I am running this application on to another PC which is also sending hex characters ;however, when I run the application I am getting no...
6
4049
by: james457 | last post by:
Hi all, I am sending data from a linux application through serial port to an embedded device. In the current implementation a byte circular buffer is used in the firmware. (Nothing but an array with a read and write pointer) As the bytes come in, it is written to the circular bufffer. Now the PC application appears to be sending the data...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7941
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.