Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate rsib.py @ 74:b6ebe05f250f
Add some commentry about what it works with
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Wed, 25 Sep 2024 21:10:01 +0930 |
parents | da3558dec4e3 |
children | 576f112e0aba |
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 |
74
b6ebe05f250f
Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
72
diff
changeset
|
3 # Copyright (c) 2024 |
6
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 |
74
b6ebe05f250f
Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
72
diff
changeset
|
28 # Helper functions to talk to Rohde & Schwarz test equipment over the |
b6ebe05f250f
Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
72
diff
changeset
|
29 # RSIB protocol. |
b6ebe05f250f
Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
72
diff
changeset
|
30 # |
b6ebe05f250f
Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
72
diff
changeset
|
31 # This is for older test equipment (tested on an FSP7 Windows NT 3.51..) |
b6ebe05f250f
Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
72
diff
changeset
|
32 # |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
33 # 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
|
34 # 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
|
35 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
36 # 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
|
37 # 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
|
38 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 # Packets to the instrument |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 # On connection: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 # -> 00 00 00 40 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 # <- 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
|
43 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
44 # 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
|
45 # 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
|
46 # though. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 # Send a command: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 # -> 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
|
50 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 # Offs Value Meaning |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 # 00 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 # 01 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 # 02 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
55 # 03 05 Length |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 # 04 90 MsgID |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
57 # 05 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
58 # 06 03 Seq number |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 # 07 2a (*) Cmd byte 0 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 # 08 49 (I) Cmd byte 1 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 # 09 44 (D) Cmd byte 2 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
62 # 10 4a (N) Cmd byte 3 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 # 10 3f (?) Cmd byte 4 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 # 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
|
66 # first 4 bytes are length. |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
68 # Reply to command: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 # <- 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
|
70 # |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
71 # Offs Value Meaning |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 # 00 00 Length 31..24 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
73 # 01 00 of 23..16 |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
74 # 02 00 block 15..8 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 # 03 23 7..0 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
76 # 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
|
77 # 05 00 ? |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 # 06 01 Seq number |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 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
|
81 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
|
82 MSG_SRQ = 0x91 # Query SRQ |
26
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
83 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 import socket |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 class RSIBDevice(object): |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
87 hello = '\x00\x00\x00\x40' |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 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
|
89 |
26
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
90 def __init__(self, host, port = None): |
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
91 if port == None: |
226ea06bc050
Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
92 port = 2525 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 self.host = host |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 self.port = port |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
95 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 s1 = socket.socket() |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 s1.settimeout(5) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 s1.connect((self.host, self.port)) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
99 s1.send(b'\x00\x00\x00\x40') |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
100 rx = b'' |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 while len(rx) < 8: |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 rx = rx + s1.recv(8) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
103 if rx == b'': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
104 raise IOError("EOF from device") |
6
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 s2 = socket.socket() |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 s2.settimeout(5) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
108 s2.connect((self.host, self.port)) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
109 s2.send(rx[4:]) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
110 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 self.s1 = s1 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
112 self.s2 = s2 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
113 self.tag = 0 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
114 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
115 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
|
116 """Send data (string) to the instrument""" |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
117 self.s1.settimeout(timeout) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
118 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
119 if len(cmd) > 0x99: |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
120 raise ValueError("Command too long") |
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 # 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
|
123 self.tag = (self.tag + 1) & 0xff |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
124 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
|
125 self.s1.send(msg) |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
126 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
127 def read(self, timeout = 0.5): |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
128 """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
|
129 |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
130 self.s1.settimeout(timeout) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
132 reply = b'' |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
133 last = False |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
134 while not last: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
135 # Fetch the header |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
136 rx = b'' |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
137 remain = 7 |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
138 while len(rx) < 7: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
139 rx = self.s1.recv(remain) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
140 if rx == b'': |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
141 raise IOError("EOF from device") |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
142 remain -= len(rx) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
143 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
|
144 #print(rx, rxlen) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
145 if self.tag != rx[6]: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
146 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
|
147 if rx[4] == 0x80: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
148 #print('EOM') |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
149 last = True |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
150 if rx[5] != 0: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
151 print("Mystery byte %d != 0" % (rx[5])) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
152 |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
153 # 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
|
154 remain = rxlen |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
155 while remain > 0: |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
156 rx = self.s1.recv(remain) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
157 if rx == b'': |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
158 raise IOError("EOF from device") |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
159 reply += rx |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
160 remain -= len(rx) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
161 |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 return(reply) |
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
163 |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
164 def ask(self, s, timeout = None): |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
165 '''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
|
166 self.write(s) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
167 return self.read(timeout = timeout) |
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
168 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
169 def queryrsb(self): |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
170 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
|
171 self.s2.send(msg) |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
172 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
173 reply = b'' |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
174 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
|
175 rx = self.s2.recv(7) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
176 if rx == b'': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
177 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
|
178 reply += rx |
6
85dfc0babc36
Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
179 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
180 # '\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
|
181 if rx[4] != b'\x80': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
182 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
|
183 |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
184 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
|
185 |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
186 def waitsrq(self): |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
187 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
|
188 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
189 def testsrq(self): |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
190 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
|
191 self.s2.send(msg) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
192 reply = b'' |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
193 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
|
194 rx = self.s2.recv(7) |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
195 if rx == b'': |
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
196 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
|
197 reply += rx |
72
da3558dec4e3
Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
71
diff
changeset
|
198 |
14
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
199 # 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
|
200 # 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
|
201 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
|
202 return False |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
203 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
|
204 return True |
55c8dae6d1db
Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
205 else: |
71
00800345fbae
Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
206 raise IOError("Unknown SRQ byte sequence - " + str(rx)) |