Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19943 > unrolled thread
| Started by | Jean Dupont <jeandupont115@gmail.com> |
|---|---|
| First post | 2012-02-06 13:40 -0800 |
| Last post | 2012-02-07 12:44 -0500 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.python
how to read serial stream of data [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-06 13:40 -0800
Re: how to read serial stream of data [newbie] Roy Smith <roy@panix.com> - 2012-02-07 00:07 -0500
Re: how to read serial stream of data [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-07 04:13 -0800
Re: how to read serial stream of data [newbie] Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-07 15:48 +0200
Re: how to read serial stream of data [newbie] Peter Otten <__peter__@web.de> - 2012-02-07 15:02 +0100
Re: how to read serial stream of data [newbie] Antti J Ylikoski <antti.ylikoski@tkk.fi> - 2012-02-07 19:44 +0200
Re: how to read serial stream of data [newbie] Heiko Wundram <modelnine@modelnine.org> - 2012-02-07 15:04 +0100
Re: how to read serial stream of data [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-07 06:46 -0800
Re: how to read serial stream of data [newbie] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-02-07 12:44 -0500
| From | Jean Dupont <jeandupont115@gmail.com> |
|---|---|
| Date | 2012-02-06 13:40 -0800 |
| Subject | how to read serial stream of data [newbie] |
| Message-ID | <e84f3af4-da6d-4ae9-8974-54354ec16307@b18g2000vbz.googlegroups.com> |
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?
thanks
Jean
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-02-07 00:07 -0500 |
| Message-ID | <roy-5854D8.00073507022012@news.panix.com> |
| In reply to | #19943 |
In article <e84f3af4-da6d-4ae9-8974-54354ec16307@b18g2000vbz.googlegroups.com>, Jean Dupont <jeandupont115@gmail.com> 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 (http://docs.python.org/library/functions.html#open), 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 (http://docs.python.org/library/struct.html) to extract the data in a more useful form.
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont115@gmail.com> |
|---|---|
| Date | 2012-02-07 04:13 -0800 |
| Message-ID | <cd2a40a2-5f6f-461c-9d02-f2e9e8732e6f@l14g2000vbe.googlegroups.com> |
| In reply to | #19948 |
On 7 feb, 06:07, Roy Smith <r...@panix.com> wrote:
> In article
> <e84f3af4-da6d-4ae9-8974-54354ec16...@b18g2000vbz.googlegroups.com>,
> Jean Dupont <jeandupont...@gmail.com> 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
> (http://docs.python.org/library/functions.html#open), 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 (http://docs.python.org/library/struct.html) 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:
#!/usr/bin/python
import serial, time, os
voltport='/dev/ttyUSB2'
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)
ser2.setDTR(level=True)
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:
0:0:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0")
print "Opening " + ser2.portstr
s =ser2.read(11) #read up to 11bytes
voltdata.write(s)
ser2.close()
voltdata.close()
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
Jean
[toc] | [prev] | [next] | [standalone]
| From | Antti J Ylikoski <antti.ylikoski@tkk.fi> |
|---|---|
| Date | 2012-02-07 15:48 +0200 |
| Message-ID | <sY9Yq.12835$I33.11565@uutiset.elisa.fi> |
| In reply to | #19954 |
On 7.2.2012 14:13, Jean Dupont wrote: > ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, > rtscts=0, dsrdtr=0, timeout=15) In Python, if you want to continue the source line into the next text line, you must end the line to be continued with a backslash '\'. So you should write: ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, \ rtscts=0, dsrdtr=0, timeout=15) and analogously. Hope that this will help. Andy.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-02-07 15:02 +0100 |
| Message-ID | <mailman.5500.1328623332.27778.python-list@python.org> |
| In reply to | #19957 |
Antti J Ylikoski wrote:
> On 7.2.2012 14:13, Jean Dupont wrote:
>> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
>> rtscts=0, dsrdtr=0, timeout=15)
>
> In Python, if you want to continue the source line into the next text
> line, you must end the line to be continued with a backslash '\'.
>
> So you should write:
>
> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, \
> rtscts=0, dsrdtr=0, timeout=15)
>
> and analogously.
>
> Hope that this will help. Andy.
This is wrong. A line with an open parenthesis is continued automatically:
>>> zip("abc",
... "def")
[('a', 'd'), ('b', 'e'), ('c', 'f')]
>>> ("abc"
... "def")
'abcdef'
>>> 1 + 2 + (
... 3)
6
[toc] | [prev] | [next] | [standalone]
| From | Antti J Ylikoski <antti.ylikoski@tkk.fi> |
|---|---|
| Date | 2012-02-07 19:44 +0200 |
| Message-ID | <vqdYq.12887$I33.9352@uutiset.elisa.fi> |
| In reply to | #19958 |
On 7.2.2012 16:02, Peter Otten wrote:
> Antti J Ylikoski wrote:
>
>> On 7.2.2012 14:13, Jean Dupont wrote:
>>> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
>>> rtscts=0, dsrdtr=0, timeout=15)
>>
>> In Python, if you want to continue the source line into the next text
>> line, you must end the line to be continued with a backslash '\'.
>>
>> So you should write:
>>
>> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, \
>> rtscts=0, dsrdtr=0, timeout=15)
>>
>> and analogously.
>>
>> Hope that this will help. Andy.
>
> This is wrong. A line with an open parenthesis is continued automatically:
>
>>>> zip("abc",
> ... "def")
> [('a', 'd'), ('b', 'e'), ('c', 'f')]
>>>> ("abc"
> ... "def")
> 'abcdef'
>>>> 1 + 2 + (
> ... 3)
> 6
>
>
Thank you for correcting me. Andy.
[toc] | [prev] | [next] | [standalone]
| From | Heiko Wundram <modelnine@modelnine.org> |
|---|---|
| Date | 2012-02-07 15:04 +0100 |
| Message-ID | <mailman.5501.1328623489.27778.python-list@python.org> |
| In reply to | #19957 |
Am 07.02.2012 14:48, schrieb Antti J Ylikoski:
> On 7.2.2012 14:13, Jean Dupont wrote:
>> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
>> rtscts=0, dsrdtr=0, timeout=15)
>
> In Python, if you want to continue the source line into the next text
> line, you must end the line to be continued with a backslash '\'.
Absolutely not true, and this is bad advice (stylistically).
When (any form of) brackets are open at the end of a line, Python does
not start a new command on the next line but rather continues the
backeted content.
So:
ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
rtscts=0, dsrdtr=0, timeout=15)
is perfectly fine and certainly the recommended way of putting this.
Adding the backslash-continuation is always _possible_, but only
_required_ when there are no open brackets.
So:
x = "hello" \
" test"
is equivalent to:
x = ("hello"
" test")
in assigning:
x = "hello test"
--
--- Heiko.
[toc] | [prev] | [next] | [standalone]
| From | Jean Dupont <jeandupont115@gmail.com> |
|---|---|
| Date | 2012-02-07 06:46 -0800 |
| Message-ID | <97e03762-e7b7-4202-a0b8-51ff579c5066@l14g2000vbe.googlegroups.com> |
| In reply to | #19959 |
On 7 feb, 15:04, Heiko Wundram <modeln...@modelnine.org> wrote:
> Am 07.02.2012 14:48, schrieb Antti J Ylikoski:
>
> > On 7.2.2012 14:13, Jean Dupont wrote:
> >> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
> >> rtscts=0, dsrdtr=0, timeout=15)
>
> > In Python, if you want to continue the source line into the next text
> > line, you must end the line to be continued with a backslash '\'.
>
> Absolutely not true, and this is bad advice (stylistically).
>
> When (any form of) brackets are open at the end of a line, Python does
> not start a new command on the next line but rather continues the
> backeted content.
>
> So:
>
> ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
> rtscts=0, dsrdtr=0, timeout=15)
>
> is perfectly fine and certainly the recommended way of putting this.
>
> Adding the backslash-continuation is always _possible_, but only
> _required_ when there are no open brackets.
>
> So:
>
> x = "hello" \
> " test"
>
> is equivalent to:
>
> x = ("hello"
> " test")
>
> in assigning:
>
> x = "hello test"
>
> --
> --- Heiko.
Hello to all who gave advice concerning the line continuation, in fact
this was not a real problem but happened by accident
copying and pasting my program lines. Advice concerning the empty file
would of course also be very much appreciated.
thanks,
Jean
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2012-02-07 12:44 -0500 |
| Message-ID | <mailman.5505.1328636706.27778.python-list@python.org> |
| In reply to | #19954 |
On Tue, 7 Feb 2012 04:13:39 -0800 (PST), Jean Dupont
<jeandupont115@gmail.com> wrote:
>filename = raw_input()
Note: this may include the terminating new-line character; you
should strip leading/ending white-space characters.
filename = raw_input().strip()
>ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
>rtscts=0, dsrdtr=0, timeout=15)
Note: this specifying NO FLOW CONTROL (your PERL example is
activating DTR, which could be both enabling DSRDTR control, AND setting
the DTR line high). Finally, you are specifying a 15 SECOND timeout.
>ser2.setDTR(level=True)
Note: here you are setting the DTR level high -- but you previously
opened the port without enabling the use of DSRDTR. Use
..., dsrdtr=True, ...
in the serial port creation.
>print "Opening " + ser2.portstr
ser2.name is the preferred usage; and you opened it with the
serial.Serial() call. And for such debug output, no need for a string
concatenation
print "Reading", ser2.name
>s =ser2.read(11) #read up to 11bytes
print len(s), repr(s)
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web