annotate rsib.py @ 84:4b4ae555067b

Use RMS detector and fix the sweep time for more accuracy. Add links to app notes discussing theory. Run forever without pause and print a summary on ctrl-c
author Daniel O'Connor <doconnor@gsoft.com.au>
date Thu, 03 Oct 2024 08:57:10 +0930
parents 84f96c5fe791
children
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
79
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
56 # 04 94 MsgID
6
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 #
79
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
68 # MsgID is 0x94 but 0x90 works but then we get "Query UNTERMINATED"
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
69 # from SYS:ERR? Could be EoM bit.
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
70 #
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 # Reply to command:
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 # <- 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
73 #
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 # Offs Value Meaning
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 # 00 00 Length 31..24
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
76 # 01 00 of 23..16
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
77 # 02 00 block 15..8
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 # 03 23 7..0
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
79 # 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
80 # 05 00 ?
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 # 06 01 Seq number
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 MSG_HELLO = 0x40 # We send this on connect
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
84 MSG_SRQ = 0x91 # Query SRQ
26
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
85
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 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
87 import time
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 class RSIBDevice(object):
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 hello = '\x00\x00\x00\x40'
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 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
92
26
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
93 def __init__(self, host, port = None):
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
94 if port == None:
226ea06bc050 Use "new" style exceptions.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
95 port = 2525
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 self.host = host
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 self.port = port
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 s1 = socket.socket()
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 s1.settimeout(5)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 s1.connect((self.host, self.port))
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
102 s1.send(b'\x00\x00\x00\x40')
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
103 rx = b''
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 while len(rx) < 8:
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 rx = rx + s1.recv(8)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
106 if rx == b'':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
107 raise IOError("EOF from device")
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 s2 = socket.socket()
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 s2.settimeout(5)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 s2.connect((self.host, self.port))
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 s2.send(rx[4:])
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 self.s1 = s1
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 self.s2 = s2
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 self.tag = 0
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
117
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 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
119 """Send data (string) to the instrument"""
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 self.s1.settimeout(timeout)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 if len(cmd) > 0x99:
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
123 raise ValueError("Command too long")
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
124
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 # 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
126 self.tag = (self.tag + 1) & 0xff
79
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
127 msg = b'\x00\x00\x00' + bytes([len(cmd)]) + b'\x94\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
128 self.s1.send(msg)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
129
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 def read(self, timeout = 0.5):
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 """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
132
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
133 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
134 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
135 self.s1.settimeout(timeout)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
137 reply = b''
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
138 last = False
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
139 while not last:
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
140 # 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
141 hdr = b''
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
142 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
143 while len(hdr) < 7:
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
144 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
145 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
146 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
147 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
148 else:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
149 time.sleep(0.1)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
150 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
151 hdr += rx
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
152 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
153 #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
154 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
155 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
156 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
157 #print('EOM')
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
158 last = True
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
159 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
160 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
161
75
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
162 # 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
163 remain = rxlen
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
164 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
165 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
166 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
167 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
168 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
169 else:
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
170 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
171
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
172 reply += rx
576f112e0aba Fix read timeout handling so it works over imperfect links.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 74
diff changeset
173 remain -= len(rx)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
174
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 return(reply)
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
177 def ask(self, s, timeout = None):
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
178 '''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
179 self.write(s)
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
180 return self.read(timeout = timeout)
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
181
79
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
182 def querystb(self):
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
183 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
184 self.s2.send(msg)
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
185
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
186 reply = b''
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
187 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
188 rx = self.s2.recv(7)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
189 if rx == b'':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
190 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
191 reply += rx
6
85dfc0babc36 Add basic RSIB parser and example program.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
193 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4
79
84f96c5fe791 Use different message ID that does not result in
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 75
diff changeset
194 if rx[4] != 0x80:
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
195 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
196
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
197 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
198
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
199 def waitsrq(self):
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
200 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
201
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
202 def testsrq(self):
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
203 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
204 self.s2.send(msg)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
205 reply = b''
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
206 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
207 rx = self.s2.recv(7)
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
208 if rx == b'':
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
209 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
210 reply += rx
72
da3558dec4e3 Fetch large data blocks correctly.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 71
diff changeset
211
14
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
212 # 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
213 # 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
214 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
215 return False
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
216 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
217 return True
55c8dae6d1db Add test code and some (not enabled) sequence checking.
Daniel O'Connor <darius@dons.net.au>
parents: 6
diff changeset
218 else:
71
00800345fbae Python3ise RSIB code
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
219 raise IOError("Unknown SRQ byte sequence - " + str(rx))