serial-com 0.1.3__tar.gz → 0.1.4__tar.gz
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.
- {serial_com-0.1.3 → serial_com-0.1.4}/PKG-INFO +1 -1
- {serial_com-0.1.3 → serial_com-0.1.4}/pyproject.toml +1 -1
- {serial_com-0.1.3 → serial_com-0.1.4}/serial_com/serial_com.py +24 -8
- {serial_com-0.1.3 → serial_com-0.1.4}/README.md +0 -0
- {serial_com-0.1.3 → serial_com-0.1.4}/serial_com/__init__.py +0 -0
- {serial_com-0.1.3 → serial_com-0.1.4}/serial_com/ip_port.py +0 -0
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
if sys.platform == "linux":
|
|
4
|
+
from signal import signal, SIGPIPE, SIG_DFL
|
|
5
|
+
signal(SIGPIPE, SIG_DFL)
|
|
6
|
+
|
|
3
7
|
|
|
4
8
|
import socket
|
|
5
9
|
import time
|
|
6
|
-
from typing import Iterator
|
|
10
|
+
from typing import Iterator, Literal
|
|
7
11
|
import select
|
|
8
12
|
from sys import platform
|
|
9
13
|
from abc import ABC, abstractmethod
|
|
@@ -24,6 +28,7 @@ logger = Logger("serial_com", logging.DEBUG)
|
|
|
24
28
|
|
|
25
29
|
SERIAL_PORT = "serial"
|
|
26
30
|
NETWORK_PORT = "network"
|
|
31
|
+
PORT_TYPE = Literal[SERIAL_PORT, NETWORK_PORT]
|
|
27
32
|
PORT_ACCESS = Event()
|
|
28
33
|
PORT_ACCESS.clear()
|
|
29
34
|
MIN_TIMEOUT = 0.05
|
|
@@ -97,6 +102,16 @@ class SerialPort(serial.Serial):
|
|
|
97
102
|
return SerialPort(port=self.port, baudrate=self.baudrate, bytesize=self.bytesize, parity=self.parity,
|
|
98
103
|
stopbits=self.stopbits, timeout=self.timeout, write_timeout=self.write_timeout)
|
|
99
104
|
|
|
105
|
+
@property
|
|
106
|
+
def read_timeout(self) -> float:
|
|
107
|
+
return self.timeout
|
|
108
|
+
|
|
109
|
+
@read_timeout.setter
|
|
110
|
+
def read_timeout(self, timeout: float):
|
|
111
|
+
if not isinstance(timeout, float) or isinstance(timeout, int):
|
|
112
|
+
raise ValueError(F"Invalid timeout of type {type(timeout)}. Expected type float or int")
|
|
113
|
+
self.timeout = timeout
|
|
114
|
+
|
|
100
115
|
def read_until(self, eol: bytes):
|
|
101
116
|
if eol == b"\r\n":
|
|
102
117
|
return self.readline()
|
|
@@ -265,7 +280,7 @@ class WorkerProperty:
|
|
|
265
280
|
port_type: str
|
|
266
281
|
name: str
|
|
267
282
|
|
|
268
|
-
def __init__(self, port_type: str =
|
|
283
|
+
def __init__(self, port_type: str = PORT_TYPE, name: str = "", listener: str = "", baudrate: int = 19200): #TODO: stopbits etc.
|
|
269
284
|
if port_type not in (SERIAL_PORT, NETWORK_PORT):
|
|
270
285
|
raise ValueError(f"Invalid port type for PortProperty {port_type}")
|
|
271
286
|
if port_type == SERIAL_PORT:
|
|
@@ -465,7 +480,7 @@ def ser_request(request: bytearray|bytes, attempt: int, port: Port, length=None,
|
|
|
465
480
|
else:
|
|
466
481
|
timeout = port.read_timeout
|
|
467
482
|
ans = bytearray()
|
|
468
|
-
init_time = time()
|
|
483
|
+
init_time = time.time()
|
|
469
484
|
end_time = init_time + float(timeout)
|
|
470
485
|
while True:
|
|
471
486
|
try:
|
|
@@ -476,9 +491,9 @@ def ser_request(request: bytearray|bytes, attempt: int, port: Port, length=None,
|
|
|
476
491
|
ans.extend(port.read(port.in_waiting))
|
|
477
492
|
finally:
|
|
478
493
|
sleep(MIN_TIMEOUT)
|
|
479
|
-
if time() > end_time:
|
|
494
|
+
if time.time() > end_time:
|
|
480
495
|
break
|
|
481
|
-
ser_ans = bytes(
|
|
496
|
+
ser_ans = bytes(ans)
|
|
482
497
|
else:
|
|
483
498
|
ser_ans = port.read(length)
|
|
484
499
|
# make sure there is no trailing data, e.g. in passive reading
|
|
@@ -531,6 +546,7 @@ def find_com_ports() -> list:
|
|
|
531
546
|
|
|
532
547
|
return ports
|
|
533
548
|
|
|
549
|
+
|
|
534
550
|
def change_netw_addr_n_crc(request: str, netw_addr: int) -> str:
|
|
535
551
|
"""Convert request to one with correct network address
|
|
536
552
|
and CRC. netw_addr must be in range(0, 256)"""
|
|
@@ -601,4 +617,4 @@ def find_connected_mbs_devices(device, pause_event: Event, stop_event: Event) ->
|
|
|
601
617
|
|
|
602
618
|
|
|
603
619
|
|
|
604
|
-
CONNECTOR = PortsUsed()
|
|
620
|
+
CONNECTOR = PortsUsed()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|