changeset 26:226ea06bc050

Use "new" style exceptions.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 21 Sep 2011 14:54:34 +0930
parents 438a5f1ddcd7
children e2833d081413
files rsib.py
diffstat 1 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/rsib.py	Thu Aug 25 16:46:34 2011 +0930
+++ b/rsib.py	Wed Sep 21 14:54:34 2011 +0930
@@ -76,13 +76,17 @@
 MSG_CMDREP = 0x80  # Reply from the instrument to us
 MSG_CMD = 0x90     # Command to the instrument
 MSG_SRQ = 0x91	   # Query SRQ
+
+import exceptions
 import socket
 
 class RSIBDevice(object):
     hello = '\x00\x00\x00\x40'
     docmd = '\x00\x00\x00\x05\x90\x00\x01'
 
-    def __init__(self, host, port = 2525):
+    def __init__(self, host, port = None):
+        if port == None:
+            port = 2525
         self.host = host
         self.port = port
 
@@ -94,7 +98,7 @@
         while len(rx) < 8:
             rx = rx + s1.recv(8)
             if rx == '':
-                raise "EOF from device"
+                raise exceptions.IOError("EOF from device")
 
         s2 = socket.socket()
         s2.settimeout(5)
@@ -110,7 +114,7 @@
         self.s1.settimeout(timeout)
 
         if len(cmd) > 0x99:
-            raise "Command too long"
+            raise exceptions.ValueError("Command too long")
         
         # Pre-increment for easy comparison in read
         self.tag = (self.tag + 1) & 0xff
@@ -127,15 +131,15 @@
         while len(rx) < 7:
             rx = self.s1.recv(7)
             if rx == '':
-                raise "EOF from device"
+                raise exceptions.IOError("EOF from device")
         
         if self.tag != ord(rx[6]):
-            raise "Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag)
+            raise exceptions.IOError("Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag))
 
         rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3])
-        print "Msg ID 0x%02x" % (ord(rx[4]))
+        #print "Msg ID 0x%02x" % (ord(rx[4]))
         if False and ord(rx[4]) != MSG_CMDREP:
-            raise "Unexpected Msg ID 0x%02x" % (ord(rx[4]))
+            raise exceptions.IOError("Unexpected Msg ID 0x%02x" % (ord(rx[4])))
         
         if ord(rx[5]) != 0:
             print "Mystery byte %d != 0" % (ord(rx[5]))
@@ -144,7 +148,7 @@
         while len(reply) < rxlen:
             rx = self.s1.recv(rxlen)
             if rx == '':
-                raise "EOF from device"
+                raise exceptions.IOError("EOF from device")
             reply += rx
             
         return(reply)
@@ -157,12 +161,12 @@
         while len(reply) < 7:
             rx = self.s2.recv(7)
             if rx == '':
-                raise "EOF from device"
+                raise exceptions.IOError("EOF from device")
             reply += rx
 
         # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4
         if rx[4] != '\x80':
-            raise "Incorrect Msg ID in response to STB query"
+            raise exceptions.IOError("Incorrect Msg ID in response to STB query")
         
         return ord(rx[5])
 
@@ -176,7 +180,7 @@
         while len(reply) < 7:
             rx = self.s2.recv(7)
             if rx == '':
-                raise "EOF from device"
+                raise exceptions.IOError("EOF from device")
             reply += rx
             
         # 00 00 00 00 80 14 08 - SRQ = 0
@@ -186,4 +190,4 @@
         elif rx == '\x00\x00\x00\x00\xa0\x64\x07':
             return True
         else:
-            raise "Unknown SRQ byte sequence - " + str(map(ord, rx))
+            raise exceptions.IOError("Unknown SRQ byte sequence - " + str(map(ord, rx)))