# HG changeset patch # User Daniel O'Connor # Date 1242482459 -34200 # Node ID 85dfc0babc36e8b93bda1d07f9a9a050773e9341 # Parent 51d1fc44a7533525b55542f6239d76c030d57462 Add basic RSIB parser and example program. There are still a few questions about this stuff.. diff -r 51d1fc44a753 -r 85dfc0babc36 example2.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example2.py Sat May 16 23:30:59 2009 +0930 @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# Copyright (c) 2009 +# Daniel O'Connor . 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. +# + +import rsib +import numpy +from matplotlib import pylab + +def test(r): + r.write('*IDN?') + print "ID is " + r.read(5) + r.write("*RST") + r.write("INIT:CONT OFF") + r.write("SYST:DISP:UPD ON") + r.write("FREQ:STAR 85MHz;STOP 125MHz") + r.write("DISP:WIND:TRAC:T:RLEV -20dBm") + r.write("INIT;*WAI") + r.write("*OPC?") + print "OPC - " + r.read(10) + r.write("CALC:MARK:PEXC 6DB") + r.write("CALC:MARK:FUNC:TOI ON") + r.write("CALC:MARK:FUNC:TOI:RES?") + print "Result " + r.read(10) + + r.write("FORM:DATA ASC") + r.write("CALC:LIM5:NAME 'TEST1'") + r.write("CALC:LIM5:COMM 'Upper limit line'") + r.write("CALC1:LIM5:TRAC 2") + r.write("TRAC1? TRACE1") + data = r.read(10) + #print "Data - " + dat + data = map(float, data.split(',')) + ary = numpy.array(data) + pylab.plot(ary) + pylab.show() + +if __name__ == '__main__': + r = rsib.RSIBDevice('localhost') + test(r) + diff -r 51d1fc44a753 -r 85dfc0babc36 rsib.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rsib.py Sat May 16 23:30:59 2009 +0930 @@ -0,0 +1,181 @@ +#!/usr/bin/env python + +# Copyright (c) 2009 +# Daniel O'Connor . 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 + +import socket + +class RSIBDevice(object): + hello = '\x00\x00\x00\x40' + docmd = '\x00\x00\x00\x05\x90\x00\x01' + + def __init__(self, host, 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 "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 "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 "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) + + rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3]) + 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 "EOF from device" + reply += rx + + return(reply) + + +def test(r): + import numpy + from matplotlib import pylab + + r.write('*IDN?') + print "ID is " + r.read(5) + r.write("*RST") + r.write("INIT:CONT OFF") + r.write("SYST:DISP:UPD ON") + r.write("FREQ:STAR 85MHz;STOP 125MHz") + r.write("DISP:WIND:TRAC:T:RLEV -20dBm") + r.write("INIT;*WAI") + r.write("*OPC?") + print "OPC - " + r.read(10) + r.write("CALC:MARK:PEXC 6DB") + r.write("CALC:MARK:FUNC:TOI ON") + r.write("CALC:MARK:FUNC:TOI:RES?") + print "Result " + r.read(10) + + r.write("FORM:DAYA ASC") + r.write("CALC:LIM5:NAME 'TEST1'") + r.write("CALC:LIM5:COMM 'Upper limit line'") + r.write("CALC1:LIM5:TRAC 2") + r.write("TRAC1? TRACE1") + data = r.read(10) + #print "Data - " + dat + data = map(float, data.split(',')) + ary = numpy.array(data) + pylab.plot(ary) + pylab.show() + +