comparison scpisock.py @ 74:b6ebe05f250f default tip

Add some commentry about what it works with
author Daniel O'Connor <doconnor@gsoft.com.au>
date Wed, 25 Sep 2024 21:10:01 +0930
parents c6be52360c2f
children
comparison
equal deleted inserted replaced
73:ca5a822c550a 74:b6ebe05f250f
34 # ToDo: Implement the SRQ & DCL channel (I don't have any supported hardware) 34 # ToDo: Implement the SRQ & DCL channel (I don't have any supported hardware)
35 35
36 SCPI_PORT = 5025 36 SCPI_PORT = 5025
37 37
38 class SCPISockDevice(object): 38 class SCPISockDevice(object):
39 def __init__(self, host, port = None): 39 def __init__(self, host, port = SCPI_PORT):
40 if port == None:
41 port = SCPI_PORT
42 self.sock = socket.create_connection((host, port)) 40 self.sock = socket.create_connection((host, port))
43 41
44 def flush(self): 42 def flush(self):
45 while True: 43 while True:
46 r, w, x = select.select([self.sock], [], [], 0) 44 r, w, x = select.select([self.sock], [], [], 0)
48 break 46 break
49 self.sock.recv(1024) 47 self.sock.recv(1024)
50 48
51 def write(self, data): 49 def write(self, data):
52 trail = '' 50 trail = ''
53 if data[-1] != '\n': 51 if data[-1] != b'\n':
54 trail = '\n' 52 trail = b'\n'
55 53
56 self.sock.send(data + trail) 54 self.sock.send(data + trail)
57 55
58 def read(self, timeout = None): 56 def read(self, timeout = None):
59 res = '' 57 res = b''
60 if timeout == None: 58 if timeout == None:
61 timeout = 0.1 59 timeout = 0.1
62 60
63 while True: 61 while True:
64 r, w, x = select.select([self.sock], [], [], timeout) 62 r, w, x = select.select([self.sock], [], [], timeout)
65 if len(r) == 0: 63 if len(r) == 0:
66 break 64 break
67 res = res + self.sock.recv(1024) 65 res = res + self.sock.recv(1024)
68 if res[-1] == '\n': 66 if res[-1] == b'\n':
69 break 67 break
70 68
71 return res.rstrip('\n') 69 return res.rstrip(b'\n')
72 70