Skip to content

Commit

Permalink
Add get_firmware_version
Browse files Browse the repository at this point in the history
  • Loading branch information
bessman committed Aug 22, 2024
1 parent 6e52888 commit 3742998
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions pslab/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
GET_INDUCTANCE = Byte.pack(4)

GET_VERSION = Byte.pack(5)
GET_FW_VERSION = Byte.pack(6)

RETRIEVE_BUFFER = Byte.pack(8)
GET_HIGH_FREQUENCY = Byte.pack(9)
Expand Down
51 changes: 51 additions & 0 deletions pslab/serial_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
>>> device.disconnect()
"""

from __future__ import annotations

try:
import grp
except ImportError:
Expand All @@ -18,6 +20,7 @@
import platform
import struct
import time
from dataclasses import dataclass
from functools import partial, update_wrapper
from typing import List, Union

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


@dataclass
class FirmwareVersion:
"""Version of pslab-firmware running on connected device.
Uses semantic versioning conventions.
Attributes
----------
major : int
Major version. Incremented when backward imcompatible changes are made.
minor : int
Minor version. Incremented when new functionality is added, or existing
functionality is changed in a backward compatible manner.
patch : int
Patch version. Incremented when bug fixes are made with do not change the
PSLab's documented behavior.
"""

major: int
minor: int
patch: int


class SerialHandler:
"""Provides methods for communicating with the PSLab hardware.
Expand Down Expand Up @@ -105,6 +131,7 @@ def __init__(
self.check_serial_access_permission()
self.connect(port=port, baudrate=baudrate, timeout=timeout)
self.connected = self.interface.is_open
self.firmware = self.get_firmware_version()

@staticmethod
def check_serial_access_permission():
Expand Down Expand Up @@ -264,6 +291,30 @@ def get_version(self) -> str:
version = self.interface.readline()
return version.decode("utf-8")

def get_firmware_version(self) -> FirmwareVersion:
"""Get firmware version.
Returns
-------
tuple[int, int, int]
major, minor, patch.
"""
self.send_byte(CP.COMMON)
self.send_byte(CP.GET_FW_VERSION)

try:
# Firmware version query was added in firmware version 3.0.0.
major = self.get_byte()
minor = self.get_byte()
patch = self.get_byte()
except serial.SerialException:
major = 2
minor = 0
patch = 0

return FirmwareVersion(major, minor, patch)

def get_ack(self) -> int:
"""Get response code from PSLab.
Expand Down

0 comments on commit 3742998

Please sign in to comment.