annotate rsib.py @ 75:576f112e0aba

Fix read timeout handling so it works over imperfect links.
author Daniel O'Connor <doconnor@gsoft.com.au>
date Fri, 27 Sep 2024 09:27:33 +0930
parents b6ebe05f250f
children 84f96c5fe791
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
85 import time
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 class RSIBDevice(object):
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 hello = '\x00\x00\x00\x40'
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 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
90
26
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
91 def __init__(self, host, port = None):
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
92 if port == None:
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
93 port = 2525
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 self.host = host
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 self.port = port
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 s1 = socket.socket()
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 s1.settimeout(5)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 s1.connect((self.host, self.port))
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
100 s1.send(b'\x00\x00\x00\x40')
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
101 rx = b''
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 while len(rx) < 8:
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 rx = rx + s1.recv(8)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
104 if rx == b'':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
105 raise IOError("EOF from device")
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 s2 = socket.socket()
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 s2.settimeout(5)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 s2.connect((self.host, self.port))
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 s2.send(rx[4:])
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 self.s1 = s1
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 self.s2 = s2
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 self.tag = 0
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
115
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 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
117 """Send data (string) to the instrument"""
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 self.s1.settimeout(timeout)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 if len(cmd) > 0x99:
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
121 raise ValueError("Command too long")
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
122
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 # 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
124 self.tag = (self.tag + 1) & 0xff
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
125 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
126 self.s1.send(msg)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
127
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 def read(self, timeout = 0.5):
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 """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
130
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
131 if timeout is not None:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
132 timeout = time.clock_gettime(time.CLOCK_MONOTONIC) + timeout
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 self.s1.settimeout(timeout)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
135 reply = b''
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
136 last = False
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
137 while not last:
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
138 # Fetch the header
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
139 hdr = b''
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
140 remain = 7
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
141 while len(hdr) < 7:
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
142 rx = self.s1.recv(remain)
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
143 if len(rx) == 0:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
144 if timeout is not None and time.clock_gettime(time.CLOCK_MONOTONIC) >= timeout:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
145 raise IOError("EOF from device")
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
146 else:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
147 time.sleep(0.1)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
148 remain -= len(rx)
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
149 hdr += rx
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
150 rxlen = rx[0] << 24 | rx[1] << 16 | rx[2] << 8 | rx[3]
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
151 #print(rx, rxlen)
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
152 if self.tag != rx[6]:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
153 raise IOError("Reply out of order, got 0x%02x expected 0x%02x" % (rx[6], self.tag))
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
154 if rx[4] == 0x80:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
155 #print('EOM')
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
156 last = True
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
157 if rx[5] != 0:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
158 print("Mystery byte %d != 0" % (rx[5]))
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
159
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
160 # Fetch the actual data block now we know the length
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
161 remain = rxlen
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
162 while remain > 0:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
163 rx = self.s1.recv(remain)
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
164 if len(rx) == 0:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
165 if timeout is not None and time.clock_gettime(time.CLOCK_MONOTONIC) >= timeout:
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
166 raise IOError("EOF from device")
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
167 else:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
168 time.sleep(0.1)
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
169
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
170 reply += rx
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
171 remain -= len(rx)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
172
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 return(reply)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
175 def ask(self, s, timeout = None):
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
176 '''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
177 self.write(s)
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
178 return self.read(timeout = timeout)
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
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 def queryrsb(self):
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
181 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
182 self.s2.send(msg)
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 reply = b''
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
185 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
186 rx = self.s2.recv(7)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
187 if rx == b'':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
188 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
189 reply += rx
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
191 # '\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
192 if rx[4] != b'\x80':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
193 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
194
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
195 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
196
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
197 def waitsrq(self):
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
198 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
199
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
200 def testsrq(self):
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
201 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
202 self.s2.send(msg)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
203 reply = b''
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
204 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
205 rx = self.s2.recv(7)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
206 if rx == b'':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
207 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
208 reply += rx
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
209
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
210 # 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
211 # 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
212 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
213 return False
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
214 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
215 return True
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
216 else:
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
217 raise IOError("Unknown SRQ byte sequence - " + str(rx))