Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate rsib.py @ 72:da3558dec4e3
Fetch large data blocks correctly.
Add ask function.
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Wed, 25 Sep 2024 20:56:39 +0930 |
parents | 00800345fbae |
children | b6ebe05f250f |
rev | line source |
---|---|
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 # Copyright (c) 2009 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
6 # Redistribution and use in source and binary forms, with or without |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 # modification, are permitted provided that the following conditions |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 # are met: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 # 1. Redistributions of source code must retain the above copyright |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 # notice, this list of conditions and the following disclaimer. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 # 2. Redistributions in binary form must reproduce the above copyright |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 # notice, this list of conditions and the following disclaimer in the |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 # documentation and/or other materials provided with the distribution. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
25 # SUCH DAMAGE. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 # Reverse engineered from the Linux library & example program at |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 # http://epsrv.astro.umk.pl/~ep/irbene/rsib/library/RSIB-Linux.zip |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 # There are 2 socket connections for some reason. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 # The first seems to do [nearly] all the work |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
33 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
34 # Packets to the instrument |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
35 # On connection: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
36 # -> 00 00 00 40 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 # <- 00 00 00 40 a0 04 00 00 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 # We need to echo back the last 4 bytes of the reply to a second |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 # socket opened to the same port. I don't know what that socket is for |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 # though. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
43 # Send a command: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
44 # -> 00 00 00 05 90 00 01 2a 49 44 4e 3f .......* IDN? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
46 # Offs Value Meaning |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 # 00 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 # 01 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 # 02 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 # 03 05 Length |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 # 04 90 MsgID |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 # 05 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 # 06 03 Seq number |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 # 07 2a (*) Cmd byte 0 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
55 # 08 49 (I) Cmd byte 1 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 # 09 44 (D) Cmd byte 2 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
57 # 10 4a (N) Cmd byte 3 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
58 # 10 3f (?) Cmd byte 4 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 # Interactive program seems to cap length at 0x99 but perhaps the |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 # first 4 bytes are length. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
62 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 # Reply to command: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 # <- 00 00 00 23 80 00 01 52 6f 68 ... |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 # Offs Value Meaning |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 # 00 00 Length 31..24 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
68 # 01 00 of 23..16 |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
69 # 02 00 block 15..8 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 # 03 23 7..0 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
71 # 04 00/80 Is 0x80 if this is the last block in a sequence |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 # 05 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
73 # 06 01 Seq number |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 MSG_HELLO = 0x40 # We send this on connect |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 MSG_CMD = 0x90 # Command to the instrument |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
77 MSG_SRQ = 0x91 # Query SRQ |
26
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
78 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 import socket |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 class RSIBDevice(object): |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
82 hello = '\x00\x00\x00\x40' |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
83 docmd = '\x00\x00\x00\x05\x90\x00\x01' |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 |
26
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
85 def __init__(self, host, port = None): |
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
86 if port == None: |
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
87 port = 2525 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 self.host = host |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
89 self.port = port |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 s1 = socket.socket() |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
92 s1.settimeout(5) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 s1.connect((self.host, self.port)) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
94 s1.send(b'\x00\x00\x00\x40') |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
95 rx = b'' |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 while len(rx) < 8: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 rx = rx + s1.recv(8) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
98 if rx == b'': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
99 raise IOError("EOF from device") |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 s2 = socket.socket() |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 s2.settimeout(5) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 s2.connect((self.host, self.port)) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
104 s2.send(rx[4:]) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
105 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
106 self.s1 = s1 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 self.s2 = s2 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
108 self.tag = 0 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
109 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
110 def write(self, cmd, timeout = 0.5): |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 """Send data (string) to the instrument""" |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
112 self.s1.settimeout(timeout) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
113 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
114 if len(cmd) > 0x99: |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
115 raise ValueError("Command too long") |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
116 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
117 # Pre-increment for easy comparison in read |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
118 self.tag = (self.tag + 1) & 0xff |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
119 msg = b'\x00\x00\x00' + bytes([len(cmd)]) + b'\x90\x00' + bytes([self.tag]) + cmd.encode('ascii') |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
120 self.s1.send(msg) |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
121 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
122 def read(self, timeout = 0.5): |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
123 """Read data from the device, waits for up to timeout seconds for each TCP read""" |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
124 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
125 self.s1.settimeout(timeout) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
126 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
127 reply = b'' |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
128 last = False |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
129 while not last: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
130 # Fetch the header |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
131 rx = b'' |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
132 remain = 7 |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
133 while len(rx) < 7: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
134 rx = self.s1.recv(remain) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
135 if rx == b'': |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
136 raise IOError("EOF from device") |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
137 remain -= len(rx) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
138 rxlen = rx[0] << 24 | rx[1] << 16 | rx[2] << 8 | rx[3] |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
139 #print(rx, rxlen) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
140 if self.tag != rx[6]: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
141 raise IOError("Reply out of order, got 0x%02x expected 0x%02x" % (rx[6], self.tag)) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
142 if rx[4] == 0x80: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
143 #print('EOM') |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
144 last = True |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
145 if rx[5] != 0: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
146 print("Mystery byte %d != 0" % (rx[5])) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
147 |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
148 # Fetch the actual data block now we know the length |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
149 remain = rxlen |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
150 while remain > 0: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
151 rx = self.s1.recv(remain) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
152 if rx == b'': |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
153 raise IOError("EOF from device") |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
154 reply += rx |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
155 remain -= len(rx) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
156 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
157 return(reply) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
158 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
159 def ask(self, s, timeout = None): |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
160 '''Wrapper to send a command and wait for a reply''' |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
161 self.write(s) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
162 return self.read(timeout = timeout) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
163 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
164 def queryrsb(self): |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
165 msg = b'\x00\x00\x00\x00\xd1\x18\x00' |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
166 self.s2.send(msg) |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
167 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
168 reply = b'' |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
169 while len(reply) < 7: |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
170 rx = self.s2.recv(7) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
171 if rx == b'': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
172 raise IOError("EOF from device") |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
173 reply += rx |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
175 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
176 if rx[4] != b'\x80': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
177 raise IOError("Incorrect Msg ID in response to STB query") |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
178 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
179 return rx[5] |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
180 |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
181 def waitsrq(self): |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
182 msg = b'\x00\x00\x00\x00\xb1\x00\x00' |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
183 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
184 def testsrq(self): |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
185 msg = b'\x00\x00\x00\x00\x91\x00\x00' |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
186 self.s2.send(msg) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
187 reply = b'' |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
188 while len(reply) < 7: |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
189 rx = self.s2.recv(7) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
190 if rx == b'': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
191 raise IOError("EOF from device") |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
192 reply += rx |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
193 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
194 # 00 00 00 00 80 14 08 - SRQ = 0 |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
195 # 00 00 00 00 a0 64 07 - SRQ = 1 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
196 if rx == b'\x00\x00\x00\x00\x80\x14\x08': |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
197 return False |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
198 elif rx == b'\x00\x00\x00\x00\xa0\x64\x07': |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
199 return True |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
200 else: |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
201 raise IOError("Unknown SRQ byte sequence - " + str(rx)) |