annotate scpisock.py @ 73:ca5a822c550a

Fix for Python 3 Add getbin function to read large non-arrat data blocks (eg screen capture)
author Daniel O'Connor <doconnor@gsoft.com.au>
date Wed, 25 Sep 2024 20:57:26 +0930
parents c6be52360c2f
children b6ebe05f250f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 # Copyright (c) 2011
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 #
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 # Redistribution and use in source and binary forms, with or without
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 # modification, are permitted provided that the following conditions
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 # are met:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 # notice, this list of conditions and the following disclaimer.
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 # 2. Redistributions in binary form must reproduce the above copyright
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer in the
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 # documentation and/or other materials provided with the distribution.
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 #
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ef713fe03db8 Add SCPI socket class.
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
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 # SUCH DAMAGE.
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 #
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 import select
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 import socket
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 # See this for more info
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 # http://www.staff.uni-bayreuth.de/~btp918/cmt2007/geraete/agilent_34410A_dmm/manual/SCPI_Sockets.pdf
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 #
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 # ToDo: Implement the SRQ & DCL channel (I don't have any supported hardware)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 SCPI_PORT = 5025
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 class SCPISockDevice(object):
28
c6be52360c2f Handle default port better so passing None in picks the default.
Daniel O'Connor <darius@dons.net.au>
parents: 22
diff changeset
39 def __init__(self, host, port = None):
c6be52360c2f Handle default port better so passing None in picks the default.
Daniel O'Connor <darius@dons.net.au>
parents: 22
diff changeset
40 if port == None:
c6be52360c2f Handle default port better so passing None in picks the default.
Daniel O'Connor <darius@dons.net.au>
parents: 22
diff changeset
41 port = SCPI_PORT
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 self.sock = socket.create_connection((host, port))
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 def flush(self):
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 while True:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 r, w, x = select.select([self.sock], [], [], 0)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 if len(r) == 0:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 break
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 self.sock.recv(1024)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 def write(self, data):
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 trail = ''
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 if data[-1] != '\n':
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 trail = '\n'
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 self.sock.send(data + trail)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 def read(self, timeout = None):
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 res = ''
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 if timeout == None:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 timeout = 0.1
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 while True:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 r, w, x = select.select([self.sock], [], [], timeout)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 if len(r) == 0:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 break
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 res = res + self.sock.recv(1024)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 if res[-1] == '\n':
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 break
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 return res.rstrip('\n')
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72