Skip to content

Commit a857470

Browse files
Merge pull request #1143 from annie-xd-wang/Fix-serial-port-monitor
2 parents e05c8e8 + 2f4fb44 commit a857470

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/navigate/model/device_startup_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
# Third Party Imports
4242

4343
# Local Imports
44-
from navigate.tools.common_functions import build_ref_name, load_param_from_module
44+
from navigate.tools.common_functions import build_ref_name, load_param_from_module, decode_bytes
4545
from navigate.tools.decorators import performance_monitor
4646
from navigate.model.devices.device_types import (
4747
SerialDevice,
@@ -184,7 +184,7 @@ def build_connection(
184184
if serial_conn is not None:
185185
serial_conn.write = performance_monitor(
186186
prefix="Serial",
187-
display_args=lambda d: f"{str(port)}-{d.decode(errors='ignore')}"
187+
display_args=lambda d: f"{str(port)}-{decode_bytes(d)}"
188188
)(serial_conn.write)
189189
serial_conn.readline = performance_monitor(
190190
prefix="Serial",

src/navigate/tools/common_functions.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import importlib
3535
from threading import Lock
3636
from types import TracebackType
37-
from typing import Optional, Any, Type
37+
from typing import Optional, Any, Type, Union
3838

3939
# Third-party imports
4040

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

161161

162+
def decode_bytes(value: Union[bytes, memoryview]):
163+
"""Decode bytes or memoryview into readable string.
164+
165+
Parameters
166+
----------
167+
value : bytes or memoryview
168+
the value
169+
170+
Returns
171+
-------
172+
result : str
173+
a readable string
174+
"""
175+
if isinstance(value, memoryview):
176+
value = value.tobytes()
177+
178+
if not isinstance(value, (bytes, bytearray)):
179+
return ""
180+
try:
181+
return value.decode(errors="ignore")
182+
except Exception:
183+
return ""
184+
162185
class VariableWithLock:
163186
def __init__(self, variable_type: Any) -> None:
164187
"""This class is a wrapper for a variable with a lock to ensure thread safety.

0 commit comments

Comments
 (0)