annotate rsib.py @ 14:55c8dae6d1db

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