Skip to content

Commit 93ec706

Browse files
committed
new method for DMX class: is_connected. updated attribute use_device to device
1 parent 7689b19 commit 93ec706

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

dmx/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
__author__ = 'Rune Monzel'
1010
__email__ = 'runemonzel@googlemail.com'
11-
__version__ = '0.1.0'
11+
__version__ = '0.2.0'
1212
__all__ = ['DMX',
1313
'logger',
1414
'Device',
1515
'DEVICE_LIST',
16-
'sleep_us',]
16+
'sleep_us']

dmx/dmx.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# internal python modules
88
import logging
99
import time
10+
from typing import Union
1011

1112
# external python modules
1213
import serial
@@ -90,7 +91,7 @@ def __init__(self, num_of_channels: int = 512, serial_number: str = "") -> None:
9091
9192
:param num_of_channels: integer between 1 and 512
9293
:param serial_number: serial number of the RS-485 chip as string. If you want to know the current serial number
93-
of your device, call my_dmx.use_device.serial_number
94+
of your device, call my_dmx.device.serial_number
9495
:return None:
9596
"""
9697

@@ -102,7 +103,7 @@ def __init__(self, num_of_channels: int = 512, serial_number: str = "") -> None:
102103

103104
# Search for RS-485 devices, for this look into DEVICE_LIST
104105
self.ser = None
105-
self.use_device = None
106+
self.device = None
106107
for device in serial.tools.list_ports.comports():
107108
for known_device in DEVICE_LIST:
108109
if device.vid == known_device.vid and device.pid == known_device.pid and serial_number == "":
@@ -112,7 +113,7 @@ def __init__(self, num_of_channels: int = 512, serial_number: str = "") -> None:
112113
except (OSError, serial.SerialException):
113114
pass
114115
else:
115-
self.use_device = device
116+
self.device = device
116117
break
117118

118119
elif device.vid == known_device.vid and device.pid == known_device.pid and \
@@ -124,22 +125,22 @@ def __init__(self, num_of_channels: int = 512, serial_number: str = "") -> None:
124125
except (OSError, serial.SerialException) as error:
125126
raise error
126127
else:
127-
self.use_device = device
128+
self.device = device
128129
logger.info("Found device with serial number: " + serial_number)
129130
break
130-
if self.use_device:
131-
logger.info("Found RS-485 interface: " + self.use_device.description)
131+
if self.device:
132+
logger.info("Found RS-485 interface: " + self.device.description)
132133
break
133134

134-
if self.use_device is None:
135+
if self.device is None:
135136
raise ConnectionError("Could not find the RS-485 interface.")
136137

137-
if self.use_device.vid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.vid and \
138-
self.use_device.pid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.pid:
138+
if self.device.vid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.vid and \
139+
self.device.pid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.pid:
139140
self.start_byte = np.array([0x7E, 0x06, 0x01, 0x02, 0x00], np.uint8)
140141
self.end_byte = np.array([0xE7], np.uint8)
141142
self.num_of_channels = num_of_channels
142-
self.ser = serial.Serial(self.use_device.name,
143+
self.ser = serial.Serial(self.device.name,
143144
baudrate=250000,
144145
parity=serial.PARITY_NONE,
145146
bytesize=serial.EIGHTBITS,
@@ -149,7 +150,7 @@ def __init__(self, num_of_channels: int = 512, serial_number: str = "") -> None:
149150
self.start_byte = np.array([0x00], np.uint8)
150151
self.end_byte = np.array([], np.uint8)
151152
self.num_of_channels = num_of_channels
152-
self.ser = serial.Serial(self.use_device.name,
153+
self.ser = serial.Serial(self.device.name,
153154
baudrate=250000,
154155
parity=serial.PARITY_NONE,
155156
bytesize=serial.EIGHTBITS,
@@ -175,8 +176,8 @@ def num_of_channels(self, num_of_channels: int) -> None:
175176
if num_of_channels > 512:
176177
raise ValueError("Number of channels are maximal 512! Only channels 1 to 512 can be accessed. " +
177178
"Channel 0 is reserved as start channel.")
178-
if self.use_device.vid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.vid and \
179-
self.use_device.pid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.pid:
179+
if self.device.vid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.vid and \
180+
self.device.pid == EUROLITE_USB_DMX512_PRO_CABLE_INTERFACE.pid:
180181
self.start_byte[2] = (num_of_channels+1) & 0xFF
181182
self.start_byte[3] = ((num_of_channels+1) >> 8) & 0xFF
182183
self.__num_of_channels = num_of_channels
@@ -186,6 +187,19 @@ def num_of_channels(self, num_of_channels: int) -> None:
186187
for channel_id in range(min([len(old_data), len(self.data)])):
187188
self.data[channel_id] = old_data[channel_id]
188189

190+
def is_connected(self) -> bool:
191+
"""
192+
checks if the DMX class has a connection to the device
193+
194+
:return:
195+
"""
196+
connected = False
197+
devices = serial.tools.list_ports.comports()
198+
if self.device is not None:
199+
if self.device in devices:
200+
connected = True
201+
return connected
202+
189203
def set_data(self, channel_id: int, data: int, auto_send: bool = True) -> None:
190204
"""
191205
@@ -224,11 +238,12 @@ def __del__(self) -> None:
224238
"""
225239
if isinstance(self.ser, serial.Serial):
226240
if self.ser.is_open:
241+
if self.is_connected():
242+
self.num_of_channels = 512
243+
self.data = np.zeros([self.num_of_channels], np.uint8)
244+
self.send()
245+
self.send() # make sure it has been send
227246
print("close serial port")
228-
self.num_of_channels = 512
229-
self.data = np.zeros([self.num_of_channels], np.uint8)
230-
self.send()
231-
self.send() # make sure it is send
232247
self.ser.close()
233248

234249

@@ -289,7 +304,7 @@ def sleep_us(sleep_in_us: int) -> None:
289304
dmx.set_data(4, i)
290305
time.sleep(0.01)
291306

292-
my_device_sn = dmx.use_device.serial_number
307+
my_device_sn = dmx.device.serial_number
293308
del dmx
294309

295310
dmx2 = DMX(serial_number=my_device_sn)

0 commit comments

Comments
 (0)