Skip to content

Commit 3742998

Browse files
committed
Add get_firmware_version
1 parent 6e52888 commit 3742998

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

pslab/protocol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
GET_INDUCTANCE = Byte.pack(4)
153153

154154
GET_VERSION = Byte.pack(5)
155+
GET_FW_VERSION = Byte.pack(6)
155156

156157
RETRIEVE_BUFFER = Byte.pack(8)
157158
GET_HIGH_FREQUENCY = Byte.pack(9)

pslab/serial_handler.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
>>> device.disconnect()
99
"""
1010

11+
from __future__ import annotations
12+
1113
try:
1214
import grp
1315
except ImportError:
@@ -18,6 +20,7 @@
1820
import platform
1921
import struct
2022
import time
23+
from dataclasses import dataclass
2124
from functools import partial, update_wrapper
2225
from typing import List, Union
2326

@@ -64,6 +67,29 @@ def _get_version(port: str) -> str:
6467
return version.decode("utf-8")
6568

6669

70+
@dataclass
71+
class FirmwareVersion:
72+
"""Version of pslab-firmware running on connected device.
73+
74+
Uses semantic versioning conventions.
75+
76+
Attributes
77+
----------
78+
major : int
79+
Major version. Incremented when backward imcompatible changes are made.
80+
minor : int
81+
Minor version. Incremented when new functionality is added, or existing
82+
functionality is changed in a backward compatible manner.
83+
patch : int
84+
Patch version. Incremented when bug fixes are made with do not change the
85+
PSLab's documented behavior.
86+
"""
87+
88+
major: int
89+
minor: int
90+
patch: int
91+
92+
6793
class SerialHandler:
6894
"""Provides methods for communicating with the PSLab hardware.
6995
@@ -105,6 +131,7 @@ def __init__(
105131
self.check_serial_access_permission()
106132
self.connect(port=port, baudrate=baudrate, timeout=timeout)
107133
self.connected = self.interface.is_open
134+
self.firmware = self.get_firmware_version()
108135

109136
@staticmethod
110137
def check_serial_access_permission():
@@ -264,6 +291,30 @@ def get_version(self) -> str:
264291
version = self.interface.readline()
265292
return version.decode("utf-8")
266293

294+
def get_firmware_version(self) -> FirmwareVersion:
295+
"""Get firmware version.
296+
297+
Returns
298+
-------
299+
tuple[int, int, int]
300+
major, minor, patch.
301+
302+
"""
303+
self.send_byte(CP.COMMON)
304+
self.send_byte(CP.GET_FW_VERSION)
305+
306+
try:
307+
# Firmware version query was added in firmware version 3.0.0.
308+
major = self.get_byte()
309+
minor = self.get_byte()
310+
patch = self.get_byte()
311+
except serial.SerialException:
312+
major = 2
313+
minor = 0
314+
patch = 0
315+
316+
return FirmwareVersion(major, minor, patch)
317+
267318
def get_ack(self) -> int:
268319
"""Get response code from PSLab.
269320

0 commit comments

Comments
 (0)