Skip to content

Commit f03c494

Browse files
committed
Begin update to new protocol
1 parent e1066ea commit f03c494

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

pslab/connection/connection.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ def write(self, data: bytes) -> int:
7070
"""
7171
...
7272

73+
def exchange(self, cmd: bytes, data: bytes = b"") -> bytes:
74+
"""Send command and input data to device, and return output data.
75+
76+
Parameters
77+
----------
78+
cmd : int
79+
Command code.
80+
data : bytes, default b''
81+
Input data for command, if any.
82+
83+
Returns
84+
-------
85+
bytes
86+
Output data from command, if any.
87+
"""
88+
(cmd_int,) = CP.ShortInt.unpack(cmd)
89+
header = CP.Header.pack(cmd_int, len(data))
90+
self.write(header + data)
91+
status, response_size = CP.Header.unpack(self.read(CP.Header.size))
92+
93+
if status:
94+
raise Exception(status)
95+
96+
response = self.read(response_size)
97+
98+
if len(response) < response_size:
99+
raise TimeoutError
100+
101+
return response
102+
73103
def get_byte(self) -> int:
74104
"""Read a single one-byte of integer value.
75105
@@ -164,10 +194,7 @@ def get_version(self) -> str:
164194
str
165195
Version string.
166196
"""
167-
self.send_byte(CP.COMMON)
168-
self.send_byte(CP.GET_VERSION)
169-
version_length = 9
170-
version = self.read(version_length)
197+
version = self.exchange(CP.COMMON + CP.GET_VERSION)
171198

172199
try:
173200
if b"PSLab" not in version:
@@ -177,23 +204,16 @@ def get_version(self) -> str:
177204
msg = "device not found"
178205
raise ConnectionError(msg) from exc
179206

180-
return version.decode("utf-8")
207+
return version.rstrip(b"\x00").decode("utf-8")
181208

182209
def get_firmware_version(self) -> FirmwareVersion:
183210
"""Get firmware version.
184211
185212
Returns
186213
-------
187-
tuple[int, int, int]
214+
FirmwareVersion
188215
major, minor, patch.
189216
190217
"""
191-
self.send_byte(CP.COMMON)
192-
self.send_byte(CP.GET_FW_VERSION)
193-
194-
# Firmware version query was added in firmware version 3.0.0.
195-
major = self.get_byte()
196-
minor = self.get_byte()
197-
patch = self.get_byte()
198-
218+
major, minor, patch = self.exchange(CP.COMMON + CP.GET_FW_VERSION)
199219
return FirmwareVersion(major, minor, patch)

pslab/protocol.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
ShortInt = struct.Struct("H") # size 2
1010
Integer = struct.Struct("I") # size 4
1111

12+
Header = struct.Struct("<HHxxxx")
13+
1214
ACKNOWLEDGE = Byte.pack(254)
1315
MAX_SAMPLES = 10000
1416
DATA_SPLITTING = 200

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy