how to read serial stream of data [newbie]

Jean Dupont jeandupont115 at
Tue Feb 7 13:13:39 CET 2012

On 7 feb, 06:07, Roy Smith <r... at> wrote:
> In article
> <e84f3af4-da6d-4ae9-8974-54354ec16... at>,
>  Jean Dupont <jeandupont... at> wrote:
> > I'd like to read in a stream of data which looks like this:
> > the device sends out a byte-string of 11 bytes roughly every second:
> >     B0B0B0B0B03131B0B50D8A
> >     B0B0B0B0B03131B0B50D8A
> >     B0B0B031B63131B0310D8A
> >     B0B034B3323432B3310D8A
> >     B0B03237B53432B3310D8A
> > .
> > .
> > .
> > As you see every string is ended by 0D8A
> > How can this be accomplished in Python?
> The basic idea would be to open your datastream in binary mode
> (, then use read(11)
> to read exactly 11 bytes into a string.
> Depending on what the 11 bytes are, you might want to use the struct
> module ( to extract the data
> in a more useful form.

Thank you very much for taking the time to reply. I'm really
completely new to python and all help is really very welcome.
In the documentation I read that to open the datastream binary I need
to add the option b
this is how far I got until now:
import serial, time, os
print "Enter a filename:",
filename = raw_input()
voltdata = open(filename,'wb')
ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
rtscts=0, dsrdtr=0, timeout=15)
print "State of DSR-line: ", ser2.getDSR()
#the following line was added because I want to be sure that all
parameters are set the same as under a working application for the
same device
os.system("stty -F31:0:bbb:
print "Opening " + ser2.portstr
s #read up to 11bytes

However the above code doesn't fill my file with data, I guess the
data should also be flushed somewhere in the code but I'm unsure where
to do that.
A futher consideration: because the device sends its data continuously
I guess I'd have to use the byte sequence 0D8A of the previously sent
data string as an indicator that the next 9 bytes are those I really
want and put those in a string which than coudl be written to the file

all help welcome

More information about the Python-list mailing list