comparison rsib.py @ 6:85dfc0babc36

Add basic RSIB parser and example program. There are still a few questions about this stuff..
author Daniel O'Connor <darius@dons.net.au>
date Sat, 16 May 2009 23:30:59 +0930
parents
children 55c8dae6d1db
comparison
equal deleted inserted replaced
5:51d1fc44a753 6:85dfc0babc36
1 #!/usr/bin/env python
2
3 # Copyright (c) 2009
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27
28 # Reverse engineered from the Linux library & example program at
29 # http://epsrv.astro.umk.pl/~ep/irbene/rsib/library/RSIB-Linux.zip
30 #
31 # There are 2 socket connections for some reason.
32 # The first seems to do [nearly] all the work
33 #
34 # Packets to the instrument
35 # On connection:
36 # -> 00 00 00 40
37 # <- 00 00 00 40 a0 04 00 00
38 #
39 # We need to echo back the last 4 bytes of the reply to a second
40 # socket opened to the same port. I don't know what that socket is for
41 # though.
42 #
43 # Send a command:
44 # -> 00 00 00 05 90 00 01 2a 49 44 4e 3f .......* IDN?
45 #
46 # Offs Value Meaning
47 # 00 00 ?
48 # 01 00 ?
49 # 02 00 ?
50 # 03 05 Length
51 # 04 90 MsgID
52 # 05 00 ?
53 # 06 03 Seq number
54 # 07 2a (*) Cmd byte 0
55 # 08 49 (I) Cmd byte 1
56 # 09 44 (D) Cmd byte 2
57 # 10 4a (N) Cmd byte 3
58 # 10 3f (?) Cmd byte 4
59 #
60 # Interactive program seems to cap length at 0x99 but perhaps the
61 # first 4 bytes are length.
62 #
63 # Reply to command:
64 # <- 00 00 00 23 80 00 01 52 6f 68 ...
65 #
66 # Offs Value Meaning
67 # 00 00 Length 31..24
68 # 01 00 23..16
69 # 02 00 15..8
70 # 03 23 7..0
71 # 04 80 MsgID
72 # 05 00 ?
73 # 06 01 Seq number
74
75 MSG_HELLO = 0x40 # We send this on connect
76 MSG_CMDREP = 0x80 # Reply from the instrument to us
77 MSG_CMD = 0x90 # Command to the instrument
78
79 import socket
80
81 class RSIBDevice(object):
82 hello = '\x00\x00\x00\x40'
83 docmd = '\x00\x00\x00\x05\x90\x00\x01'
84
85 def __init__(self, host, port = 2525):
86 self.host = host
87 self.port = port
88
89 s1 = socket.socket()
90 s1.settimeout(5)
91 s1.connect((self.host, self.port))
92 s1.send('\x00\x00\x00\x40')
93 rx = ''
94 while len(rx) < 8:
95 rx = rx + s1.recv(8)
96 if rx == '':
97 raise "EOF from device"
98
99 s2 = socket.socket()
100 s2.settimeout(5)
101 s2.connect((self.host, self.port))
102 s2.send(rx[4:])
103
104 self.s1 = s1
105 self.s2 = s2
106 self.tag = 0
107
108 def write(self, cmd, timeout = 0.5):
109 """Send data (string) to the instrument"""
110 self.s1.settimeout(timeout)
111
112 if len(cmd) > 0x99:
113 raise "Command too long"
114
115 # Pre-increment for easy comparison in read
116 self.tag = (self.tag + 1) & 0xff
117 msg = '\x00\x00\x00' + chr(len(cmd)) + '\x90\x00' + chr(self.tag) + cmd
118 self.s1.send(msg)
119
120 def read(self, timeout = 0.5):
121 """Read data from the device, waits for up to timeout seconds for each TCP read"""
122
123 self.s1.settimeout(timeout)
124
125 # Fetch the header
126 rx = ''
127 while len(rx) < 7:
128 rx = self.s1.recv(7)
129 if rx == '':
130 raise "EOF from device"
131
132 if self.tag != ord(rx[6]):
133 raise "Reply out of order, got 0x%02x expected 0x%02x" % (ord(rx[6]), self.tag)
134
135 rxlen = ord(rx[0]) << 24 | ord(rx[1]) << 16 | ord(rx[2]) << 8 | ord(rx[3])
136 if ord(rx[5]) != 0:
137 print "Mystery byte %d != 0" % (ord(rx[5]))
138
139 # Fetch the actual reply now we know the length
140 reply = ''
141 while len(reply) < rxlen:
142 rx = self.s1.recv(rxlen)
143 if rx == '':
144 raise "EOF from device"
145 reply += rx
146
147 return(reply)
148
149
150 def test(r):
151 import numpy
152 from matplotlib import pylab
153
154 r.write('*IDN?')
155 print "ID is " + r.read(5)
156 r.write("*RST")
157 r.write("INIT:CONT OFF")
158 r.write("SYST:DISP:UPD ON")
159 r.write("FREQ:STAR 85MHz;STOP 125MHz")
160 r.write("DISP:WIND:TRAC:T:RLEV -20dBm")
161 r.write("INIT;*WAI")
162 r.write("*OPC?")
163 print "OPC - " + r.read(10)
164 r.write("CALC:MARK:PEXC 6DB")
165 r.write("CALC:MARK:FUNC:TOI ON")
166 r.write("CALC:MARK:FUNC:TOI:RES?")
167 print "Result " + r.read(10)
168
169 r.write("FORM:DAYA ASC")
170 r.write("CALC:LIM5:NAME 'TEST1'")
171 r.write("CALC:LIM5:COMM 'Upper limit line'")
172 r.write("CALC1:LIM5:TRAC 2")
173 r.write("TRAC1? TRACE1")
174 data = r.read(10)
175 #print "Data - " + dat
176 data = map(float, data.split(','))
177 ary = numpy.array(data)
178 pylab.plot(ary)
179 pylab.show()
180
181