Skip to content

Commit a9b1512

Browse files
committed
fix open/close FD being unclean after successive open/closes, add associated test
1 parent a7e0f7d commit a9b1512

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

plin/device.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
PLIOSETLEDSTATE = IOW(ord('u'), 40, PLINUSBLEDState)
4242

4343

44+
class PLINException(Exception):
45+
pass
46+
47+
4448
class PLIN:
4549
def __init__(self, interface: str):
4650
self.interface = interface
@@ -72,19 +76,23 @@ def start(self, mode: PLINMode, baudrate: int = 19200):
7276
self.mode = mode
7377
self.baudrate = baudrate
7478

75-
if not self.fd:
79+
if self.fd:
80+
raise PLINException("Already connected to PLIN device!")
81+
else:
7682
self.fd = os.open(self.interface, os.O_RDWR)
77-
78-
self.reset()
79-
buffer = PLINUSBInitHardware(self.baudrate, self.mode, 0)
80-
self._ioctl(PLIOHWINIT, buffer)
83+
self.reset()
84+
buffer = PLINUSBInitHardware(self.baudrate, self.mode, 0)
85+
self._ioctl(PLIOHWINIT, buffer)
8186

8287
def stop(self):
8388
'''
8489
Disconnects from the PLIN device by closing the file descriptor.
8590
'''
8691
if self.fd:
8792
os.close(self.fd)
93+
self.fd = None
94+
else:
95+
raise PLINException("Not connected to PLIN device!")
8896

8997
def set_frame_entry(self,
9098
id: int,

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "plin-linux"
7-
version = "0.0.2"
7+
version = "0.0.3"
88
authors = [
99
{ name="William Zhang", email="[email protected]" },
1010
]

tests/integration/test_plin.py

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from plin.enums import PLINMode, PLINMessageType, PLINFrameDirection, PLINFrameChecksumType
44
from plin.structs import *
55

6+
67
@pytest.fixture
78
def plin_interface():
89
return "/dev/plin0"
@@ -47,6 +48,24 @@ def test_start(plin_interface, plin):
4748
plin.stop()
4849

4950

51+
def test_start_stop_start(plin_interface, plin):
52+
expected_baudrate = 1000
53+
expected_mode = PLINMode.SLAVE
54+
55+
plin.start(mode=expected_mode, baudrate=expected_baudrate)
56+
assert plin.get_baudrate() == expected_baudrate
57+
assert plin.get_mode() == expected_mode
58+
plin.stop()
59+
60+
expected_baudrate = 19200
61+
expected_mode = PLINMode.MASTER
62+
63+
plin.start(mode=expected_mode, baudrate=expected_baudrate)
64+
assert plin.get_baudrate() == expected_baudrate
65+
assert plin.get_mode() == expected_mode
66+
plin.stop()
67+
68+
5069
def test_read_nonblocking(plin_master_19200):
5170
assert None == plin_master_19200.read(block=False)
5271

0 commit comments

Comments
 (0)