mchplnet 0.5.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mchplnet/__init__.py +6 -0
- mchplnet/examples/__init__.py +6 -0
- mchplnet/examples/auto_detect_example.py +87 -0
- mchplnet/examples/get_ram_example.py +35 -0
- mchplnet/examples/get_ram_example_auto.py +45 -0
- mchplnet/examples/get_ram_example_tcp.py +35 -0
- mchplnet/examples/simple_auto_detect.py +22 -0
- mchplnet/examples/tcp_server.py +134 -0
- mchplnet/interfaces/__init__.py +1 -0
- mchplnet/interfaces/abstract_interface.py +178 -0
- mchplnet/interfaces/can.py +299 -0
- mchplnet/interfaces/factory.py +63 -0
- mchplnet/interfaces/tcp_ip.py +68 -0
- mchplnet/interfaces/uart.py +250 -0
- mchplnet/lnet.py +188 -0
- mchplnet/lnetframe.py +223 -0
- mchplnet/services/__init__.py +1 -0
- mchplnet/services/frame_device_info.py +218 -0
- mchplnet/services/frame_getram.py +80 -0
- mchplnet/services/frame_load_parameter.py +80 -0
- mchplnet/services/frame_putram.py +94 -0
- mchplnet/services/frame_reboot.py +24 -0
- mchplnet/services/frame_save_parameter.py +55 -0
- mchplnet/services/scope.py +267 -0
- mchplnet-0.5.1.dist-info/METADATA +126 -0
- mchplnet-0.5.1.dist-info/RECORD +29 -0
- mchplnet-0.5.1.dist-info/WHEEL +5 -0
- mchplnet-0.5.1.dist-info/licenses/LICENSE +19 -0
- mchplnet-0.5.1.dist-info/top_level.txt +1 -0
mchplnet/__init__.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Example demonstrating auto-detection of LNet device on serial port.
|
|
2
|
+
|
|
3
|
+
This script shows how to use the AUTO port feature to automatically
|
|
4
|
+
detect which COM port has an LNet device connected.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
|
|
9
|
+
from mchplnet.interfaces.uart import LNetSerial
|
|
10
|
+
from mchplnet.lnet import LNet
|
|
11
|
+
|
|
12
|
+
# Enable logging to see auto-detection process
|
|
13
|
+
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
|
14
|
+
|
|
15
|
+
def test_auto_detection():
|
|
16
|
+
"""Test auto-detection with no port specified."""
|
|
17
|
+
print("\n=== Test 1: Auto-detection with no port specified ===")
|
|
18
|
+
try:
|
|
19
|
+
# Create interface without specifying port (will default to AUTO)
|
|
20
|
+
interface = LNetSerial()
|
|
21
|
+
lnet = LNet(interface)
|
|
22
|
+
|
|
23
|
+
# If we get here, auto-detection succeeded
|
|
24
|
+
device_info = lnet.get_device_info()
|
|
25
|
+
print("✓ Auto-detection successful!")
|
|
26
|
+
print(f" Connected port: {interface.com_port}")
|
|
27
|
+
print(f" Monitor version: {device_info.monitor_version}")
|
|
28
|
+
print(f" Processor width: {device_info.uc_width}-bit")
|
|
29
|
+
|
|
30
|
+
lnet.interface.stop()
|
|
31
|
+
return True
|
|
32
|
+
except Exception as e:
|
|
33
|
+
print(f"✗ Auto-detection failed: {e}")
|
|
34
|
+
return False
|
|
35
|
+
|
|
36
|
+
def test_explicit_auto():
|
|
37
|
+
"""Test auto-detection with explicit AUTO parameter."""
|
|
38
|
+
print("\n=== Test 2: Auto-detection with explicit 'AUTO' parameter ===")
|
|
39
|
+
try:
|
|
40
|
+
# Create interface with explicit AUTO
|
|
41
|
+
interface = LNetSerial(port="AUTO")
|
|
42
|
+
lnet = LNet(interface)
|
|
43
|
+
|
|
44
|
+
device_info = lnet.get_device_info()
|
|
45
|
+
print("✓ Auto-detection successful!")
|
|
46
|
+
print(f" Connected port: {interface.com_port}")
|
|
47
|
+
print(f" Monitor version: {device_info.monitor_version}")
|
|
48
|
+
|
|
49
|
+
lnet.interface.stop()
|
|
50
|
+
return True
|
|
51
|
+
except Exception as e:
|
|
52
|
+
print(f"✗ Auto-detection failed: {e}")
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
def test_explicit_port():
|
|
56
|
+
"""Test with explicit port specification (traditional method)."""
|
|
57
|
+
print("\n=== Test 3: Manual port specification ===")
|
|
58
|
+
try:
|
|
59
|
+
# This will only work if you change COM3 to your actual port
|
|
60
|
+
interface = LNetSerial(port="COM3")
|
|
61
|
+
lnet = LNet(interface)
|
|
62
|
+
|
|
63
|
+
device_info = lnet.get_device_info()
|
|
64
|
+
print("✓ Manual connection successful!")
|
|
65
|
+
print(f" Connected port: {interface.com_port}")
|
|
66
|
+
print(f" Monitor version: {device_info.monitor_version}")
|
|
67
|
+
|
|
68
|
+
lnet.interface.stop()
|
|
69
|
+
return True
|
|
70
|
+
except Exception as e:
|
|
71
|
+
print(f"✗ Manual connection failed: {e}")
|
|
72
|
+
print(" (This is expected if COM3 is not your device port)")
|
|
73
|
+
return False
|
|
74
|
+
|
|
75
|
+
if __name__ == "__main__":
|
|
76
|
+
print("=" * 60)
|
|
77
|
+
print("LNet Serial Port Auto-Detection Example")
|
|
78
|
+
print("=" * 60)
|
|
79
|
+
|
|
80
|
+
# Run tests
|
|
81
|
+
test_auto_detection()
|
|
82
|
+
test_explicit_auto()
|
|
83
|
+
test_explicit_port()
|
|
84
|
+
|
|
85
|
+
print("\n" + "=" * 60)
|
|
86
|
+
print("Auto-detection feature demonstration complete!")
|
|
87
|
+
print("=" * 60)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Example to configure and getting started with receiving variable Value using watchView functionality."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from mchplnet.interfaces.factory import InterfaceFactory
|
|
6
|
+
from mchplnet.interfaces.factory import InterfaceType as IType
|
|
7
|
+
from mchplnet.lnet import LNet
|
|
8
|
+
|
|
9
|
+
# Configure logging to aid in debugging
|
|
10
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
11
|
+
|
|
12
|
+
# Create an interface instance for communication with the microcontroller.
|
|
13
|
+
# Here, we are using a serial interface with specified port and baud-rate.
|
|
14
|
+
interface = InterfaceFactory.get_interface(IType.SERIAL, port="COM4", baudrate=115200)
|
|
15
|
+
|
|
16
|
+
# LNet is responsible for managing low-level communication with the microcontroller.
|
|
17
|
+
l_net = LNet(interface)
|
|
18
|
+
|
|
19
|
+
# Logging various information about the connected device.
|
|
20
|
+
logging.debug(l_net.device_info.monitor_date)
|
|
21
|
+
logging.debug(l_net.device_info.processor_id)
|
|
22
|
+
logging.debug(l_net.device_info.uc_width)
|
|
23
|
+
logging.debug(f"appversion:{l_net.device_info.app_version}....... DSP state:{l_net.device_info.dsp_state}")
|
|
24
|
+
|
|
25
|
+
# Reading a specific memory address from the RAM of the microcontroller.
|
|
26
|
+
# here we provide manually the address and the data type of the variable.
|
|
27
|
+
read_bytes = l_net.get_ram(4148, 2)
|
|
28
|
+
|
|
29
|
+
# Convert the read bytes to an integer for easier interpretation.
|
|
30
|
+
# The byte order is specified as 'little-endian'.
|
|
31
|
+
logging.debug(int.from_bytes(read_bytes, byteorder="little"))
|
|
32
|
+
|
|
33
|
+
# The following code is commented out. If executed, it would write a value to a specific memory address.
|
|
34
|
+
# put_value = l_net.put_ram(4148, 2, bytes(50))
|
|
35
|
+
# logging.debug(put_value)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Example showing auto-detection feature for getting variable values.
|
|
2
|
+
|
|
3
|
+
This example demonstrates how to use the auto-detection feature to
|
|
4
|
+
automatically find the COM port with an LNet device connected.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
|
|
9
|
+
from mchplnet.interfaces.factory import InterfaceFactory
|
|
10
|
+
from mchplnet.interfaces.factory import InterfaceType as IType
|
|
11
|
+
from mchplnet.lnet import LNet
|
|
12
|
+
|
|
13
|
+
# Configure logging to aid in debugging
|
|
14
|
+
logging.basicConfig(level=logging.INFO)
|
|
15
|
+
|
|
16
|
+
# Create an interface instance with AUTO port detection.
|
|
17
|
+
# The system will automatically scan available COM ports and connect to the first one
|
|
18
|
+
# that responds to LNet protocol.
|
|
19
|
+
print("Starting auto-detection of LNet device...")
|
|
20
|
+
interface = InterfaceFactory.get_interface(IType.SERIAL, port="AUTO", baudrate=115200)
|
|
21
|
+
|
|
22
|
+
# LNet is responsible for managing low-level communication with the microcontroller.
|
|
23
|
+
l_net = LNet(interface)
|
|
24
|
+
|
|
25
|
+
print(f"\nSuccessfully connected to {interface.com_port}")
|
|
26
|
+
|
|
27
|
+
# Logging various information about the connected device.
|
|
28
|
+
print(f"Monitor Date: {l_net.device_info.monitor_date}")
|
|
29
|
+
print(f"Processor ID: {l_net.device_info.processor_id}")
|
|
30
|
+
print(f"UC Width: {l_net.device_info.uc_width}-bit")
|
|
31
|
+
print(f"App Version: {l_net.device_info.app_version}")
|
|
32
|
+
print(f"DSP State: {l_net.device_info.dsp_state}")
|
|
33
|
+
|
|
34
|
+
# Reading a specific memory address from the RAM of the microcontroller.
|
|
35
|
+
# Here we provide manually the address and the data type of the variable.
|
|
36
|
+
read_bytes = l_net.get_ram(4148, 2)
|
|
37
|
+
|
|
38
|
+
# Convert the read bytes to an integer for easier interpretation.
|
|
39
|
+
# The byte order is specified as 'little-endian'.
|
|
40
|
+
value = int.from_bytes(read_bytes, byteorder="little")
|
|
41
|
+
print(f"\nValue at address 4148: {value}")
|
|
42
|
+
|
|
43
|
+
# Close the connection
|
|
44
|
+
l_net.interface.stop()
|
|
45
|
+
print("\nConnection closed.")
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Example to configure and getting started with receiving variable Value using watchView functionality."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from mchplnet.interfaces.factory import InterfaceFactory
|
|
6
|
+
from mchplnet.interfaces.factory import InterfaceType as IType
|
|
7
|
+
from mchplnet.lnet import LNet
|
|
8
|
+
|
|
9
|
+
# Configure logging to aid in debugging
|
|
10
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
11
|
+
|
|
12
|
+
# Create an interface instance for communication with the microcontroller.
|
|
13
|
+
# Here, we are using a serial interface with specified port and baud-rate.
|
|
14
|
+
interface = InterfaceFactory.get_interface(IType.TCP_IP, host="10.0.0.20")
|
|
15
|
+
|
|
16
|
+
# LNet is responsible for managing low-level communication with the microcontroller.
|
|
17
|
+
l_net = LNet(interface)
|
|
18
|
+
|
|
19
|
+
# Logging various information about the connected device.
|
|
20
|
+
logging.debug(l_net.device_info.monitor_date)
|
|
21
|
+
logging.debug(l_net.device_info.processor_id)
|
|
22
|
+
logging.debug(l_net.device_info.uc_width)
|
|
23
|
+
logging.debug(f"appversion:{l_net.device_info.app_version}....... DSP state:{l_net.device_info.dsp_state}")
|
|
24
|
+
|
|
25
|
+
# Reading a specific memory address from the RAM of the microcontroller.
|
|
26
|
+
# here we provide manually the address and the data type of the variable.
|
|
27
|
+
read_bytes = l_net.get_ram(4148, 2)
|
|
28
|
+
|
|
29
|
+
# Convert the read bytes to an integer for easier interpretation.
|
|
30
|
+
# The byte order is specified as 'little-endian'.
|
|
31
|
+
logging.debug(int.from_bytes(read_bytes, byteorder="little"))
|
|
32
|
+
|
|
33
|
+
# The following code is commented out. If executed, it would write a value to a specific memory address.
|
|
34
|
+
# put_value = l_net.put_ram(4148, 2, bytes(50))
|
|
35
|
+
# logging.debug(put_value)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Simple example of auto-detection feature."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from mchplnet.interfaces.uart import LNetSerial
|
|
6
|
+
from mchplnet.lnet import LNet
|
|
7
|
+
|
|
8
|
+
# Enable logging to see the auto-detection process
|
|
9
|
+
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
|
10
|
+
|
|
11
|
+
# Create interface with AUTO (or just don't specify port)
|
|
12
|
+
interface = LNetSerial(port="AUTO")
|
|
13
|
+
|
|
14
|
+
# LNet will auto-detect the correct port
|
|
15
|
+
lnet = LNet(interface)
|
|
16
|
+
|
|
17
|
+
print(f"✓ Connected to {interface.com_port}")
|
|
18
|
+
print(f" Monitor version: {lnet.device_info.monitor_version}")
|
|
19
|
+
print(f" Processor width: {lnet.device_info.uc_width}-bit")
|
|
20
|
+
|
|
21
|
+
# Clean up
|
|
22
|
+
lnet.interface.stop()
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""LNet TCP Server Example.
|
|
2
|
+
|
|
3
|
+
This module implements a simple TCP server that simulates an LNet device
|
|
4
|
+
for testing and development purposes. It responds to device info and
|
|
5
|
+
parameter loading requests using the LNet protocol using the TCP/IP protocol.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
python tcp_server.py
|
|
9
|
+
|
|
10
|
+
The server will start listening on port 12666 and respond to LNet protocol
|
|
11
|
+
requests from X2CScope clients.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import socket
|
|
15
|
+
|
|
16
|
+
# LNet protocol constants
|
|
17
|
+
LNET_START_BYTE = 0x55
|
|
18
|
+
LNET_DEVICE_INFO_REQUEST_LENGTH = 5
|
|
19
|
+
LNET_LOAD_PARAMETER_REQUEST_LENGTH = 7
|
|
20
|
+
LNET_DEVICE_INFO_COMMAND = 0x01
|
|
21
|
+
LNET_LOAD_PARAMETER_COMMAND = 0x03
|
|
22
|
+
LNET_LOAD_PARAMETER_SUBCOMMAND = 0x11
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def calc_checksum(frame_bytes):
|
|
26
|
+
"""Calculate checksum for LNet frame.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
frame_bytes (bytes): Frame data to checksum
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
int: Checksum value (0-255)
|
|
33
|
+
"""
|
|
34
|
+
return sum(frame_bytes) & 0xFF
|
|
35
|
+
|
|
36
|
+
def make_deviceinfo_response(slave_id=0x01, device_id=0x8240):
|
|
37
|
+
"""Create a device info response packet.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
slave_id (int): Slave ID for the device (default: 0x01)
|
|
41
|
+
device_id (int): Device ID (default: 0x8240)
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
bytes: Device info response packet
|
|
45
|
+
"""
|
|
46
|
+
response = bytearray(b'U.\x01\x00\x00\x05\x00\x01\x00\xff@\x82Nov1920251028\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\xfcT\x00\x00\xcd')
|
|
47
|
+
return bytes(response)
|
|
48
|
+
|
|
49
|
+
def make_loadparameter_response():
|
|
50
|
+
"""Create a load parameter response packet.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
bytearray: Load parameter response packet
|
|
54
|
+
"""
|
|
55
|
+
# Your provided response
|
|
56
|
+
resp = bytearray(b'U\x1f\x01\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1e@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x88\x13\x00\x00\x88\x13\x00\x00\x82\x9c')
|
|
57
|
+
return resp
|
|
58
|
+
|
|
59
|
+
def is_device_info_request(data, slave_id=0x01):
|
|
60
|
+
"""Check if received data is a device info request.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
data (bytes): Received data packet
|
|
64
|
+
slave_id (int): Expected slave ID (default: 0x01)
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
bool: True if this is a valid device info request
|
|
68
|
+
"""
|
|
69
|
+
return (
|
|
70
|
+
len(data) == LNET_DEVICE_INFO_REQUEST_LENGTH and
|
|
71
|
+
data[0] == LNET_START_BYTE and
|
|
72
|
+
data[1] == LNET_DEVICE_INFO_COMMAND and
|
|
73
|
+
data[2] == slave_id and
|
|
74
|
+
data[3] == 0x00
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def is_load_parameter_request(data, slave_id=0x01):
|
|
78
|
+
"""Check if received data is a load parameter request.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
data (bytes): Received data packet
|
|
82
|
+
slave_id (int): Expected slave ID (default: 0x01)
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
bool: True if this is a valid load parameter request
|
|
86
|
+
"""
|
|
87
|
+
return (
|
|
88
|
+
len(data) == LNET_LOAD_PARAMETER_REQUEST_LENGTH and
|
|
89
|
+
data[0] == LNET_START_BYTE and
|
|
90
|
+
data[1] == LNET_LOAD_PARAMETER_COMMAND and
|
|
91
|
+
data[2] == slave_id and
|
|
92
|
+
data[3] == LNET_LOAD_PARAMETER_SUBCOMMAND
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def run_server(port=12666, slave_id=0x01, device_id=0x8240):
|
|
96
|
+
"""Run the LNet TCP server.
|
|
97
|
+
|
|
98
|
+
This server listens for incoming TCP connections and responds to
|
|
99
|
+
LNet protocol requests for device info and parameter loading.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
port (int): Port to listen on (default: 12666)
|
|
103
|
+
slave_id (int): Slave ID for the device (default: 0x01)
|
|
104
|
+
device_id (int): Device ID (default: 0x8240)
|
|
105
|
+
"""
|
|
106
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
107
|
+
s.bind(('0.0.0.0', port))
|
|
108
|
+
s.listen(1)
|
|
109
|
+
print(f"LNet TCP server running on port {port}...")
|
|
110
|
+
while True:
|
|
111
|
+
conn, addr = s.accept()
|
|
112
|
+
with conn:
|
|
113
|
+
print(f"Connected by {addr}")
|
|
114
|
+
while True:
|
|
115
|
+
data = conn.recv(64)
|
|
116
|
+
if not data:
|
|
117
|
+
break
|
|
118
|
+
if is_device_info_request(data, slave_id):
|
|
119
|
+
resp = make_deviceinfo_response(slave_id, device_id)
|
|
120
|
+
conn.sendall(resp)
|
|
121
|
+
elif is_load_parameter_request(data, slave_id):
|
|
122
|
+
resp = make_loadparameter_response()
|
|
123
|
+
conn.sendall(resp)
|
|
124
|
+
else:
|
|
125
|
+
print(f"Unknown request: {data.hex()}")
|
|
126
|
+
|
|
127
|
+
if __name__ == "__main__":
|
|
128
|
+
# Run the server with default settings
|
|
129
|
+
# Port: 12666, Slave ID: 0x01, Device ID: 0x8240 (dsPIC33AK)
|
|
130
|
+
run_server()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Interface implementations for LNet protocol communication."""
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"""Abstract base class defining the interface contract for LNet communication."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Interface(ABC):
|
|
7
|
+
r"""An abstract base class defining the interface for a generic interface.
|
|
8
|
+
|
|
9
|
+
This abstract base class (ABC) defines the methods and attributes that must be implemented by
|
|
10
|
+
concrete interface classes. It serves as a blueprint for creating interface classes for various
|
|
11
|
+
communication protocols.
|
|
12
|
+
|
|
13
|
+
The interface provides built-in support for connection validation through a callback mechanism,
|
|
14
|
+
enabling features like automatic COM port detection for serial interfaces.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
_test_connection_callback (callable): Optional callback function to validate connection.
|
|
18
|
+
|
|
19
|
+
Methods:
|
|
20
|
+
__init__(\\*args, \\*\\*kwargs):
|
|
21
|
+
Constructor for the interface. Subclasses should call this method before implementing
|
|
22
|
+
their own constructor.
|
|
23
|
+
|
|
24
|
+
__del__():
|
|
25
|
+
Destructor for the interface. Stops the interface when the object is deleted.
|
|
26
|
+
|
|
27
|
+
write(data):
|
|
28
|
+
Write data to the interface. Subclasses should implement this method.
|
|
29
|
+
|
|
30
|
+
read():
|
|
31
|
+
Read data from the interface. Subclasses should implement this method.
|
|
32
|
+
|
|
33
|
+
start():
|
|
34
|
+
Start the interface. Subclasses should call this method after performing startup tasks.
|
|
35
|
+
If a test connection callback is set, it will be called to validate the connection.
|
|
36
|
+
|
|
37
|
+
stop():
|
|
38
|
+
Stop the interface. Subclasses should implement this method.
|
|
39
|
+
|
|
40
|
+
set_test_connection(callback):
|
|
41
|
+
Set a callback function to validate connections. Used for auto-detection features.
|
|
42
|
+
|
|
43
|
+
is_open() -> bool:
|
|
44
|
+
Check if the interface is open and operational. Subclasses should implement this method.
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
Define a concrete interface class that implements Interface::
|
|
48
|
+
|
|
49
|
+
class SerialInterface(Interface):
|
|
50
|
+
def __init__(self, port, baud_rate):
|
|
51
|
+
# Constructor implementation here
|
|
52
|
+
super().__init__() # call this method prior to implementing your own constructor
|
|
53
|
+
|
|
54
|
+
def write(self, data):
|
|
55
|
+
# Write data implementation here
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def read(self):
|
|
59
|
+
# Read data implementation here
|
|
60
|
+
pass
|
|
61
|
+
|
|
62
|
+
def start(self):
|
|
63
|
+
# Start implementation here
|
|
64
|
+
super().start() # call this method AFTER initialising the interface
|
|
65
|
+
|
|
66
|
+
def stop(self):
|
|
67
|
+
# Stop implementation here
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
def is_open(self):
|
|
71
|
+
# is_open implementation here
|
|
72
|
+
return True
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __init__(self, *args, **kwargs):
|
|
76
|
+
r"""Constructor for the interface.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
*args: Variable-length argument list.
|
|
80
|
+
**kwargs: Arbitrary keyword arguments.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
None
|
|
84
|
+
"""
|
|
85
|
+
self._test_connection_callback = None
|
|
86
|
+
self.initialised = False
|
|
87
|
+
|
|
88
|
+
def __del__(self):
|
|
89
|
+
"""Destructor for the interface.
|
|
90
|
+
|
|
91
|
+
Stops the interface when the object is deleted.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
None
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
None
|
|
98
|
+
"""
|
|
99
|
+
self.stop()
|
|
100
|
+
|
|
101
|
+
@abstractmethod
|
|
102
|
+
def write(self, data: bytearray):
|
|
103
|
+
"""Write data to the interface.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
data: The data to be written to the interface.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
None
|
|
110
|
+
"""
|
|
111
|
+
pass
|
|
112
|
+
|
|
113
|
+
@abstractmethod
|
|
114
|
+
def read(self) -> bytearray:
|
|
115
|
+
"""Read data from the interface.
|
|
116
|
+
|
|
117
|
+
This method includes logic to handle framing, which may be specific to the LNet protocol.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
A bytearray read from the interface or None.
|
|
121
|
+
"""
|
|
122
|
+
pass
|
|
123
|
+
|
|
124
|
+
def start(self) -> bool:
|
|
125
|
+
"""Starts the interface.
|
|
126
|
+
|
|
127
|
+
if test_connection is provided, it will be called to validate the connection.
|
|
128
|
+
Any subclass implementing this method should call the super() implementation after
|
|
129
|
+
performing startup tasks.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
None
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
None
|
|
136
|
+
"""
|
|
137
|
+
if self._test_connection_callback and not self.initialised:
|
|
138
|
+
if not self._test_connection_callback():
|
|
139
|
+
return False
|
|
140
|
+
self.initialised = True
|
|
141
|
+
return True
|
|
142
|
+
|
|
143
|
+
@abstractmethod
|
|
144
|
+
def stop(self):
|
|
145
|
+
"""Stops the interface.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
None
|
|
149
|
+
|
|
150
|
+
Returns:
|
|
151
|
+
None
|
|
152
|
+
"""
|
|
153
|
+
pass
|
|
154
|
+
|
|
155
|
+
def set_test_connection(self, callback):
|
|
156
|
+
"""Set a callback function to test if connection is valid.
|
|
157
|
+
|
|
158
|
+
Used for auto-detection to validate if a port has a valid device connected.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
callback (callable): Function that returns True if connection is valid, False otherwise.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
None
|
|
165
|
+
"""
|
|
166
|
+
self._test_connection_callback = callback
|
|
167
|
+
|
|
168
|
+
@abstractmethod
|
|
169
|
+
def is_open(self) -> bool:
|
|
170
|
+
"""Check if the interface is open and operational.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
None
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
bool: True if the interface is open, False otherwise.
|
|
177
|
+
"""
|
|
178
|
+
pass
|