Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/navigate/model/device_startup_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# Third Party Imports

# Local Imports
from navigate.tools.common_functions import build_ref_name, load_param_from_module
from navigate.tools.common_functions import build_ref_name, load_param_from_module, decode_bytes
from navigate.tools.decorators import performance_monitor
from navigate.model.devices.device_types import (
SerialDevice,
Expand Down Expand Up @@ -184,7 +184,7 @@ def build_connection(
if serial_conn is not None:
serial_conn.write = performance_monitor(
prefix="Serial",
display_args=lambda d: f"{str(port)}-{d.decode(errors='ignore')}"
display_args=lambda d: f"{str(port)}-{decode_bytes(d)}"
)(serial_conn.write)
serial_conn.readline = performance_monitor(
prefix="Serial",
Expand Down
25 changes: 24 additions & 1 deletion src/navigate/tools/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import importlib
from threading import Lock
from types import TracebackType
from typing import Optional, Any, Type
from typing import Optional, Any, Type, Union

# Third-party imports

Expand Down Expand Up @@ -159,6 +159,29 @@ def load_param_from_module(module_name: str, param_name: str) -> Optional[any]:
return param


def decode_bytes(value: Union[bytes, memoryview]):
"""Decode bytes or memoryview into readable string.

Parameters
----------
value : bytes or memoryview
the value

Returns
-------
result : str
a readable string
"""
if isinstance(value, memoryview):
value = value.tobytes()

if not isinstance(value, (bytes, bytearray)):
return ""
try:
return value.decode(errors="ignore")
except Exception:
return ""

class VariableWithLock:
def __init__(self, variable_type: Any) -> None:
"""This class is a wrapper for a variable with a lock to ensure thread safety.
Expand Down
Loading