comparison rsib.py @ 71:00800345fbae

Python3ise RSIB code
author Daniel O'Connor <doconnor@gsoft.com.au>
date Thu, 24 Aug 2023 16:52:10 +0930
parents 91b476ebc0f2
children da3558dec4e3
comparison
equal deleted inserted replaced
70:6ffa6fcf278e 71:00800345fbae
75 MSG_HELLO = 0x40 # We send this on connect 75 MSG_HELLO = 0x40 # We send this on connect
76 MSG_CMDREP = 0x80 # Reply from the instrument to us 76 MSG_CMDREP = 0x80 # Reply from the instrument to us
77 MSG_CMD = 0x90 # Command to the instrument 77 MSG_CMD = 0x90 # Command to the instrument
78 MSG_SRQ = 0x91 # Query SRQ 78 MSG_SRQ = 0x91 # Query SRQ
79 79
80 import exceptions
81 import socket 80 import socket
82 81
83 class RSIBDevice(object): 82 class RSIBDevice(object):
84 hello = '\x00\x00\x00\x40' 83 hello = '\x00\x00\x00\x40'
85 docmd = '\x00\x00\x00\x05\x90\x00\x01' 84 docmd = '\x00\x00\x00\x05\x90\x00\x01'
91 self.port = port 90 self.port = port
92 91
93 s1 = socket.socket() 92 s1 = socket.socket()
94 s1.settimeout(5) 93 s1.settimeout(5)
95 s1.connect((self.host, self.port)) 94 s1.connect((self.host, self.port))
96 s1.send('\x00\x00\x00\x40') 95 s1.send(b'\x00\x00\x00\x40')
97 rx = '' 96 rx = b''
98 while len(rx) < 8: 97 while len(rx) < 8:
99 rx = rx + s1.recv(8) 98 rx = rx + s1.recv(8)
100 if rx == '': 99 if rx == b'':
101 raise exceptions.IOError("EOF from device") 100 raise IOError("EOF from device")
102 101
103 s2 = socket.socket() 102 s2 = socket.socket()
104 s2.settimeout(5) 103 s2.settimeout(5)
105 s2.connect((self.host, self.port)) 104 s2.connect((self.host, self.port))
106 s2.send(rx[4:]) 105 s2.send(rx[4:])
112 def write(self, cmd, timeout = 0.5): 111 def write(self, cmd, timeout = 0.5):
113 """Send data (string) to the instrument""" 112 """Send data (string) to the instrument"""
114 self.s1.settimeout(timeout) 113 self.s1.settimeout(timeout)
115 114
116 if len(cmd) > 0x99: 115 if len(cmd) > 0x99:
117 raise exceptions.ValueError("Command too long") 116 raise ValueError("Command too long")
118 117
119 # Pre-increment for easy comparison in read 118 # Pre-increment for easy comparison in read
120 self.tag = (self.tag + 1) & 0xff 119 self.tag = (self.tag + 1) & 0xff
121 msg = '\x00\x00\x00' + chr(len(cmd)) + '\x90\x00' + chr(self.tag) + cmd 120 msg = b'\x00\x00\x00' + bytes([len(cmd)]) + b'\x90\x00' + bytes([self.tag]) + cmd.encode('ascii')
122 self.s1.send(msg) 121 self.s1.send(msg)
123 122
124 def read(self, timeout = 0.5): 123 def read(self, timeout = 0.5):
125 """Read data from the device, waits for up to timeout seconds for each TCP read""" 124 """Read data from the device, waits for up to timeout seconds for each TCP read"""
126 125
127 self.s1.settimeout(timeout) 126 self.s1.settimeout(timeout)
128 127
129 # Fetch the header 128 # Fetch the header
130 rx = '' 129 rx = b''
131 while len(rx) < 7: 130 while len(rx) < 7:
132 rx = self.s1.recv(7) 131 rx = self.s1.recv(7)
133 if rx == '': 132 if rx == b'':
134 raise exceptions.IOError("EOF from device") 133 raise IOError("EOF from device")
135 134
136 if self.tag != ord(rx[6]): 135 if self.tag != rx[6]:
137 raise exceptions.IOError("Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag)) 136 raise IOError("Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag))
138 137
139 rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3]) 138 rxlen = rx[0] << 24 | rx[1] << 16 | rx[2] << 8 | rx[3]
140 #print "Msg ID 0x%02x" % (ord(rx[4])) 139 #print "Msg ID 0x%02x" % (rx[4])
141 if False and ord(rx[4]) != MSG_CMDREP: 140 if False and rx[4] != MSG_CMDREP:
142 raise exceptions.IOError("Unexpected Msg ID 0x%02x" % (ord(rx[4]))) 141 raise IOError("Unexpected Msg ID 0x%02x" % (rx[4]))
143 142
144 if ord(rx[5]) != 0: 143 if rx[5] != 0:
145 print("Mystery byte %d != 0" % (ord(rx[5]))) 144 print("Mystery byte %d != 0" % (rx[5]))
146 # Fetch the actual reply now we know the length 145 # Fetch the actual reply now we know the length
147 reply = '' 146 reply = b''
148 while len(reply) < rxlen: 147 while len(reply) < rxlen:
149 rx = self.s1.recv(rxlen) 148 rx = self.s1.recv(rxlen)
150 if rx == '': 149 if rx == b'':
151 raise exceptions.IOError("EOF from device") 150 raise IOError("EOF from device")
152 reply += rx 151 reply += rx
153 152
154 return(reply) 153 return(reply)
155 154
156 def queryrsb(self): 155 def queryrsb(self):
157 msg = '\x00\x00\x00\x00\xd1\x18\x00' 156 msg = b'\x00\x00\x00\x00\xd1\x18\x00'
158 self.s2.send(msg) 157 self.s2.send(msg)
159 158
160 reply = '' 159 reply = b''
161 while len(reply) < 7: 160 while len(reply) < 7:
162 rx = self.s2.recv(7) 161 rx = self.s2.recv(7)
163 if rx == '': 162 if rx == b'':
164 raise exceptions.IOError("EOF from device") 163 raise IOError("EOF from device")
165 reply += rx 164 reply += rx
166 165
167 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4 166 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4
168 if rx[4] != '\x80': 167 if rx[4] != b'\x80':
169 raise exceptions.IOError("Incorrect Msg ID in response to STB query") 168 raise IOError("Incorrect Msg ID in response to STB query")
170 169
171 return ord(rx[5]) 170 return rx[5]
172 171
173 def waitsrq(self): 172 def waitsrq(self):
174 msg = '\x00\x00\x00\x00\xb1\x00\x00' 173 msg = b'\x00\x00\x00\x00\xb1\x00\x00'
175 174
176 def testsrq(self): 175 def testsrq(self):
177 msg = '\x00\x00\x00\x00\x91\x00\x00' 176 msg = b'\x00\x00\x00\x00\x91\x00\x00'
178 self.s2.send(msg) 177 self.s2.send(msg)
179 reply = '' 178 reply = b''
180 while len(reply) < 7: 179 while len(reply) < 7:
181 rx = self.s2.recv(7) 180 rx = self.s2.recv(7)
182 if rx == '': 181 if rx == b'':
183 raise exceptions.IOError("EOF from device") 182 raise IOError("EOF from device")
184 reply += rx 183 reply += rx
185 184
186 # 00 00 00 00 80 14 08 - SRQ = 0 185 # 00 00 00 00 80 14 08 - SRQ = 0
187 # 00 00 00 00 a0 64 07 - SRQ = 1 186 # 00 00 00 00 a0 64 07 - SRQ = 1
188 if rx == '\x00\x00\x00\x00\x80\x14\x08': 187 if rx == b'\x00\x00\x00\x00\x80\x14\x08':
189 return False 188 return False
190 elif rx == '\x00\x00\x00\x00\xa0\x64\x07': 189 elif rx == b'\x00\x00\x00\x00\xa0\x64\x07':
191 return True 190 return True
192 else: 191 else:
193 raise exceptions.IOError("Unknown SRQ byte sequence - " + str(list(map(ord, rx)))) 192 raise IOError("Unknown SRQ byte sequence - " + str(rx))