annotate scpisock.py @ 84:4b4ae555067b

Use RMS detector and fix the sweep time for more accuracy. Add links to app notes discussing theory. Run forever without pause and print a summary on ctrl-c
author Daniel O'Connor <doconnor@gsoft.com.au>
date Thu, 03 Oct 2024 08:57:10 +0930
parents b6ebe05f250f
children
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):
74
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
39 def __init__(self, host, port = SCPI_PORT):
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 self.sock = socket.create_connection((host, port))
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 def flush(self):
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 while True:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 r, w, x = select.select([self.sock], [], [], 0)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 if len(r) == 0:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 break
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 self.sock.recv(1024)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 def write(self, data):
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 trail = ''
74
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
51 if data[-1] != b'\n':
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
52 trail = b'\n'
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
53
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 self.sock.send(data + trail)
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 def read(self, timeout = None):
74
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
57 res = b''
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 if timeout == None:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 timeout = 0.1
74
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
60
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 while True:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 r, w, x = select.select([self.sock], [], [], timeout)
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 if len(r) == 0:
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 break
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 res = res + self.sock.recv(1024)
74
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
66 if res[-1] == b'\n':
22
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 break
ef713fe03db8 Add SCPI socket class.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68
74
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
69 return res.rstrip(b'\n')
b6ebe05f250f Add some commentry about what it works with
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 28
diff changeset
70