comparison rsib.py @ 14:55c8dae6d1db

Add test code and some (not enabled) sequence checking.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 10 Aug 2011 12:49:15 +0930
parents 85dfc0babc36
children 6e7619f2dffd
comparison
equal deleted inserted replaced
13:c27c2df0241c 14:55c8dae6d1db
73 # 06 01 Seq number 73 # 06 01 Seq number
74 74
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 78 MSG_SRQ = 0x91 # Query SRQ
79 import socket 79 import socket
80 80
81 class RSIBDevice(object): 81 class RSIBDevice(object):
82 hello = '\x00\x00\x00\x40' 82 hello = '\x00\x00\x00\x40'
83 docmd = '\x00\x00\x00\x05\x90\x00\x01' 83 docmd = '\x00\x00\x00\x05\x90\x00\x01'
131 131
132 if self.tag != ord(rx[6]): 132 if self.tag != ord(rx[6]):
133 raise "Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag) 133 raise "Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag)
134 134
135 rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3]) 135 rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3])
136 print "Msg ID 0x%02x" % (ord(rx[4]))
137 if False and ord(rx[4]) != MSG_CMDREP:
138 raise "Unexpected Msg ID 0x%02x" % (ord(rx[4]))
139
136 if ord(rx[5]) != 0: 140 if ord(rx[5]) != 0:
137 print "Mystery byte %d != 0" % (ord(rx[5])) 141 print "Mystery byte %d != 0" % (ord(rx[5]))
138
139 # Fetch the actual reply now we know the length 142 # Fetch the actual reply now we know the length
140 reply = '' 143 reply = ''
141 while len(reply) < rxlen: 144 while len(reply) < rxlen:
142 rx = self.s1.recv(rxlen) 145 rx = self.s1.recv(rxlen)
143 if rx == '': 146 if rx == '':
144 raise "EOF from device" 147 raise "EOF from device"
145 reply += rx 148 reply += rx
146 149
147 return(reply) 150 return(reply)
148 151
149 152 def queryrsb(self):
153 msg = '\x00\x00\x00\x00\xd1\x18\x00'
154 self.s2.send(msg)
155
156 reply = ''
157 while len(reply) < 7:
158 rx = self.s2.recv(7)
159 if rx == '':
160 raise "EOF from device"
161 reply += rx
162
163 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4
164 if rx[4] != '\x80':
165 raise "Incorrect Msg ID in response to STB query"
166
167 return ord(rx[5])
168
169 def waitsrq(self):
170 msg = '\x00\x00\x00\x00\xb1\x00\x00'
171
172 def testsrq(self):
173 msg = '\x00\x00\x00\x00\x91\x00\x00'
174 self.s2.send(msg)
175 reply = ''
176 while len(reply) < 7:
177 rx = self.s2.recv(7)
178 if rx == '':
179 raise "EOF from device"
180 reply += rx
181
182 # 00 00 00 00 80 14 08 - SRQ = 0
183 # 00 00 00 00 a0 64 07 - SRQ = 1
184 if rx == '\x00\x00\x00\x00\x80\x14\x08':
185 return False
186 elif rx == '\x00\x00\x00\x00\xa0\x64\x07':
187 return True
188 else:
189 raise "Unknown SRQ byte sequence - " + str(map(ord, rx))
190
150 def test(r): 191 def test(r):
151 import numpy 192 import numpy
152 from matplotlib import pylab 193 from matplotlib import pylab
153 194
154 r.write('*IDN?') 195 r.write('*IDN?')