Mercurial > ~darius > hgwebdir.cgi > pyinst
view rsib.py @ 41:df1b4d7e988f
Reword text to be clearer.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Wed, 28 Sep 2011 13:35:45 +0930 |
parents | 226ea06bc050 |
children | 91b476ebc0f2 |
line wrap: on
line source
#!/usr/bin/env python # Copyright (c) 2009 # Daniel O'Connor <darius@dons.net.au>. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # Reverse engineered from the Linux library & example program at # http://epsrv.astro.umk.pl/~ep/irbene/rsib/library/RSIB-Linux.zip # # There are 2 socket connections for some reason. # The first seems to do [nearly] all the work # # Packets to the instrument # On connection: # -> 00 00 00 40 # <- 00 00 00 40 a0 04 00 00 # # We need to echo back the last 4 bytes of the reply to a second # socket opened to the same port. I don't know what that socket is for # though. # # Send a command: # -> 00 00 00 05 90 00 01 2a 49 44 4e 3f .......* IDN? # # Offs Value Meaning # 00 00 ? # 01 00 ? # 02 00 ? # 03 05 Length # 04 90 MsgID # 05 00 ? # 06 03 Seq number # 07 2a (*) Cmd byte 0 # 08 49 (I) Cmd byte 1 # 09 44 (D) Cmd byte 2 # 10 4a (N) Cmd byte 3 # 10 3f (?) Cmd byte 4 # # Interactive program seems to cap length at 0x99 but perhaps the # first 4 bytes are length. # # Reply to command: # <- 00 00 00 23 80 00 01 52 6f 68 ... # # Offs Value Meaning # 00 00 Length 31..24 # 01 00 23..16 # 02 00 15..8 # 03 23 7..0 # 04 80 MsgID # 05 00 ? # 06 01 Seq number MSG_HELLO = 0x40 # We send this on connect 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 = None): if port == None: port = 2525 self.host = host self.port = port s1 = socket.socket() s1.settimeout(5) s1.connect((self.host, self.port)) s1.send('\x00\x00\x00\x40') rx = '' while len(rx) < 8: rx = rx + s1.recv(8) if rx == '': raise exceptions.IOError("EOF from device") s2 = socket.socket() s2.settimeout(5) s2.connect((self.host, self.port)) s2.send(rx[4:]) self.s1 = s1 self.s2 = s2 self.tag = 0 def write(self, cmd, timeout = 0.5): """Send data (string) to the instrument""" self.s1.settimeout(timeout) if len(cmd) > 0x99: raise exceptions.ValueError("Command too long") # Pre-increment for easy comparison in read self.tag = (self.tag + 1) & 0xff msg = '\x00\x00\x00' + chr(len(cmd)) + '\x90\x00' + chr(self.tag) + cmd self.s1.send(msg) def read(self, timeout = 0.5): """Read data from the device, waits for up to timeout seconds for each TCP read""" self.s1.settimeout(timeout) # Fetch the header rx = '' while len(rx) < 7: rx = self.s1.recv(7) if rx == '': raise exceptions.IOError("EOF from device") if self.tag != ord(rx[6]): 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])) if False and ord(rx[4]) != MSG_CMDREP: raise exceptions.IOError("Unexpected Msg ID 0x%02x" % (ord(rx[4]))) if ord(rx[5]) != 0: print "Mystery byte %d != 0" % (ord(rx[5])) # Fetch the actual reply now we know the length reply = '' while len(reply) < rxlen: rx = self.s1.recv(rxlen) if rx == '': raise exceptions.IOError("EOF from device") reply += rx return(reply) def queryrsb(self): msg = '\x00\x00\x00\x00\xd1\x18\x00' self.s2.send(msg) reply = '' while len(reply) < 7: rx = self.s2.recv(7) if rx == '': raise exceptions.IOError("EOF from device") reply += rx # '\x00\x00\x00\x00\x80\x04\x01' => STB = 4 if rx[4] != '\x80': raise exceptions.IOError("Incorrect Msg ID in response to STB query") return ord(rx[5]) def waitsrq(self): msg = '\x00\x00\x00\x00\xb1\x00\x00' def testsrq(self): msg = '\x00\x00\x00\x00\x91\x00\x00' self.s2.send(msg) reply = '' while len(reply) < 7: rx = self.s2.recv(7) if rx == '': raise exceptions.IOError("EOF from device") reply += rx # 00 00 00 00 80 14 08 - SRQ = 0 # 00 00 00 00 a0 64 07 - SRQ = 1 if rx == '\x00\x00\x00\x00\x80\x14\x08': return False elif rx == '\x00\x00\x00\x00\xa0\x64\x07': return True else: raise exceptions.IOError("Unknown SRQ byte sequence - " + str(map(ord, rx)))