comparison rsib.py @ 26:226ea06bc050

Use "new" style exceptions.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 21 Sep 2011 14:54:34 +0930
parents 6e7619f2dffd
children 91b476ebc0f2
comparison
equal deleted inserted replaced
25:438a5f1ddcd7 26:226ea06bc050
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 MSG_SRQ = 0x91 # Query SRQ 78 MSG_SRQ = 0x91 # Query SRQ
79
80 import exceptions
79 import socket 81 import socket
80 82
81 class RSIBDevice(object): 83 class RSIBDevice(object):
82 hello = '\x00\x00\x00\x40' 84 hello = '\x00\x00\x00\x40'
83 docmd = '\x00\x00\x00\x05\x90\x00\x01' 85 docmd = '\x00\x00\x00\x05\x90\x00\x01'
84 86
85 def __init__(self, host, port = 2525): 87 def __init__(self, host, port = None):
88 if port == None:
89 port = 2525
86 self.host = host 90 self.host = host
87 self.port = port 91 self.port = port
88 92
89 s1 = socket.socket() 93 s1 = socket.socket()
90 s1.settimeout(5) 94 s1.settimeout(5)
92 s1.send('\x00\x00\x00\x40') 96 s1.send('\x00\x00\x00\x40')
93 rx = '' 97 rx = ''
94 while len(rx) < 8: 98 while len(rx) < 8:
95 rx = rx + s1.recv(8) 99 rx = rx + s1.recv(8)
96 if rx == '': 100 if rx == '':
97 raise "EOF from device" 101 raise exceptions.IOError("EOF from device")
98 102
99 s2 = socket.socket() 103 s2 = socket.socket()
100 s2.settimeout(5) 104 s2.settimeout(5)
101 s2.connect((self.host, self.port)) 105 s2.connect((self.host, self.port))
102 s2.send(rx[4:]) 106 s2.send(rx[4:])
108 def write(self, cmd, timeout = 0.5): 112 def write(self, cmd, timeout = 0.5):
109 """Send data (string) to the instrument""" 113 """Send data (string) to the instrument"""
110 self.s1.settimeout(timeout) 114 self.s1.settimeout(timeout)
111 115
112 if len(cmd) > 0x99: 116 if len(cmd) > 0x99:
113 raise "Command too long" 117 raise exceptions.ValueError("Command too long")
114 118
115 # Pre-increment for easy comparison in read 119 # Pre-increment for easy comparison in read
116 self.tag = (self.tag + 1) & 0xff 120 self.tag = (self.tag + 1) & 0xff
117 msg = '\x00\x00\x00' + chr(len(cmd)) + '\x90\x00' + chr(self.tag) + cmd 121 msg = '\x00\x00\x00' + chr(len(cmd)) + '\x90\x00' + chr(self.tag) + cmd
118 self.s1.send(msg) 122 self.s1.send(msg)
125 # Fetch the header 129 # Fetch the header
126 rx = '' 130 rx = ''
127 while len(rx) < 7: 131 while len(rx) < 7:
128 rx = self.s1.recv(7) 132 rx = self.s1.recv(7)
129 if rx == '': 133 if rx == '':
130 raise "EOF from device" 134 raise exceptions.IOError("EOF from device")
131 135
132 if self.tag != ord(rx[6]): 136 if self.tag != ord(rx[6]):
133 raise "Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag) 137 raise exceptions.IOError("Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag))
134 138
135 rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3]) 139 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])) 140 #print "Msg ID 0x%02x" % (ord(rx[4]))
137 if False and ord(rx[4]) != MSG_CMDREP: 141 if False and ord(rx[4]) != MSG_CMDREP:
138 raise "Unexpected Msg ID 0x%02x" % (ord(rx[4])) 142 raise exceptions.IOError("Unexpected Msg ID 0x%02x" % (ord(rx[4])))
139 143
140 if ord(rx[5]) != 0: 144 if ord(rx[5]) != 0:
141 print "Mystery byte %d != 0" % (ord(rx[5])) 145 print "Mystery byte %d != 0" % (ord(rx[5]))
142 # Fetch the actual reply now we know the length 146 # Fetch the actual reply now we know the length
143 reply = '' 147 reply = ''
144 while len(reply) < rxlen: 148 while len(reply) < rxlen:
145 rx = self.s1.recv(rxlen) 149 rx = self.s1.recv(rxlen)
146 if rx == '': 150 if rx == '':
147 raise "EOF from device" 151 raise exceptions.IOError("EOF from device")
148 reply += rx 152 reply += rx
149 153
150 return(reply) 154 return(reply)
151 155
152 def queryrsb(self): 156 def queryrsb(self):
155 159
156 reply = '' 160 reply = ''
157 while len(reply) < 7: 161 while len(reply) < 7:
158 rx = self.s2.recv(7) 162 rx = self.s2.recv(7)
159 if rx == '': 163 if rx == '':
160 raise "EOF from device" 164 raise exceptions.IOError("EOF from device")
161 reply += rx 165 reply += rx
162 166
163 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4 167 # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4
164 if rx[4] != '\x80': 168 if rx[4] != '\x80':
165 raise "Incorrect Msg ID in response to STB query" 169 raise exceptions.IOError("Incorrect Msg ID in response to STB query")
166 170
167 return ord(rx[5]) 171 return ord(rx[5])
168 172
169 def waitsrq(self): 173 def waitsrq(self):
170 msg = '\x00\x00\x00\x00\xb1\x00\x00' 174 msg = '\x00\x00\x00\x00\xb1\x00\x00'
174 self.s2.send(msg) 178 self.s2.send(msg)
175 reply = '' 179 reply = ''
176 while len(reply) < 7: 180 while len(reply) < 7:
177 rx = self.s2.recv(7) 181 rx = self.s2.recv(7)
178 if rx == '': 182 if rx == '':
179 raise "EOF from device" 183 raise exceptions.IOError("EOF from device")
180 reply += rx 184 reply += rx
181 185
182 # 00 00 00 00 80 14 08 - SRQ = 0 186 # 00 00 00 00 80 14 08 - SRQ = 0
183 # 00 00 00 00 a0 64 07 - SRQ = 1 187 # 00 00 00 00 a0 64 07 - SRQ = 1
184 if rx == '\x00\x00\x00\x00\x80\x14\x08': 188 if rx == '\x00\x00\x00\x00\x80\x14\x08':
185 return False 189 return False
186 elif rx == '\x00\x00\x00\x00\xa0\x64\x07': 190 elif rx == '\x00\x00\x00\x00\xa0\x64\x07':
187 return True 191 return True
188 else: 192 else:
189 raise "Unknown SRQ byte sequence - " + str(map(ord, rx)) 193 raise exceptions.IOError("Unknown SRQ byte sequence - " + str(map(ord, rx)))