python-can-csscan-serial 2.4.0__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.
- python_can_csscan_serial/CSSCANSerial.py +278 -0
- python_can_csscan_serial/__init__.py +0 -0
- python_can_csscan_serial-2.4.0.dist-info/METADATA +22 -0
- python_can_csscan_serial-2.4.0.dist-info/RECORD +7 -0
- python_can_csscan_serial-2.4.0.dist-info/WHEEL +4 -0
- python_can_csscan_serial-2.4.0.dist-info/entry_points.txt +2 -0
- python_can_csscan_serial-2.4.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
|
|
3
|
+
import can
|
|
4
|
+
import os
|
|
5
|
+
import serial
|
|
6
|
+
import struct
|
|
7
|
+
|
|
8
|
+
from can import Message, CanInterfaceNotImplementedError
|
|
9
|
+
from collections import deque
|
|
10
|
+
from serial.serialutil import SerialException, Timeout
|
|
11
|
+
from typing import Any, List, Optional, Tuple
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CSSCANSerial(can.BusABC):
|
|
15
|
+
|
|
16
|
+
_VID_CLX000 = 0x1CBE
|
|
17
|
+
_VID_CANmod = 0x04D8
|
|
18
|
+
|
|
19
|
+
_PID_CLX000 = 0x021A
|
|
20
|
+
_PID_CANmod = 0xEB18
|
|
21
|
+
|
|
22
|
+
def __init__(self, channel: Any, can_filters: Optional[can.typechecking.CanFilters] = None, **kwargs: object):
|
|
23
|
+
super().__init__(channel, can_filters, **kwargs)
|
|
24
|
+
|
|
25
|
+
# Serial interface to the device
|
|
26
|
+
try:
|
|
27
|
+
# Open the channel with a data timeout of 1 ms
|
|
28
|
+
self._channel = serial.Serial(channel, timeout=0.001)
|
|
29
|
+
except SerialException:
|
|
30
|
+
self._channel = None
|
|
31
|
+
|
|
32
|
+
if self._channel is None:
|
|
33
|
+
raise CanInterfaceNotImplementedError(f"Could not open {channel}")
|
|
34
|
+
|
|
35
|
+
self.channel_info = f"CSSCAN serial on {channel}"
|
|
36
|
+
|
|
37
|
+
# Event queue, contains parsed frames
|
|
38
|
+
self._events = deque()
|
|
39
|
+
|
|
40
|
+
# Data queue, contains raw interface data
|
|
41
|
+
self._data = deque()
|
|
42
|
+
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
def _recv_internal(self, timeout: Optional[float]) -> Tuple[Optional[Message], bool]:
|
|
46
|
+
result = None
|
|
47
|
+
|
|
48
|
+
t = Timeout(timeout)
|
|
49
|
+
|
|
50
|
+
# Check if there are any pending events in the queue. If so, return this
|
|
51
|
+
while len(self._events) == 0 and t.expired() is False:
|
|
52
|
+
# Receive new data from the serial interface and pass it to the parser
|
|
53
|
+
data = self._channel.read(1000000)
|
|
54
|
+
self._data.extend(data)
|
|
55
|
+
|
|
56
|
+
# Parse as many frames as possible
|
|
57
|
+
self._parse()
|
|
58
|
+
|
|
59
|
+
if len(self._events) > 0:
|
|
60
|
+
result = self._events.pop()
|
|
61
|
+
|
|
62
|
+
return result, False
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def _detect_available_configs(cls) -> List[can.typechecking.AutoDetectedConfig]:
|
|
66
|
+
# Import ports based on platform, using the tools provided by pyserial
|
|
67
|
+
if os.name == 'nt':
|
|
68
|
+
from serial.tools.list_ports_windows import comports
|
|
69
|
+
elif os.name == 'posix':
|
|
70
|
+
from serial.tools.list_ports_posix import comports
|
|
71
|
+
else:
|
|
72
|
+
return []
|
|
73
|
+
|
|
74
|
+
result = []
|
|
75
|
+
ports = comports(False)
|
|
76
|
+
|
|
77
|
+
for port in ports:
|
|
78
|
+
# Check for VID for CLX000, CANmod
|
|
79
|
+
if port.vid not in [cls._VID_CLX000, cls._VID_CANmod]:
|
|
80
|
+
continue
|
|
81
|
+
|
|
82
|
+
# Check for PID for CLX000, CANmod
|
|
83
|
+
if port.pid not in[cls._PID_CLX000, cls._PID_CANmod]:
|
|
84
|
+
continue
|
|
85
|
+
|
|
86
|
+
current = {
|
|
87
|
+
"interface": "csscan_serial",
|
|
88
|
+
"channel": port.device,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
result.append(current)
|
|
92
|
+
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
def send(self, msg: Message, timeout: Optional[float] = None) -> None:
|
|
96
|
+
payload = bytearray()
|
|
97
|
+
|
|
98
|
+
# All transmission requests are ID 0x03 with flags for frame type
|
|
99
|
+
tx_req = 0x03
|
|
100
|
+
|
|
101
|
+
if msg.is_remote_frame is True:
|
|
102
|
+
tx_req |= 0x10
|
|
103
|
+
elif msg.is_fd is True:
|
|
104
|
+
tx_req |= 0x20
|
|
105
|
+
|
|
106
|
+
payload.append(tx_req)
|
|
107
|
+
|
|
108
|
+
# Encode ID
|
|
109
|
+
id = msg.arbitration_id
|
|
110
|
+
if msg.is_extended_id is True:
|
|
111
|
+
id |= 0x20000000
|
|
112
|
+
payload.extend(struct.pack("!I", id))
|
|
113
|
+
|
|
114
|
+
# Encode DLC
|
|
115
|
+
payload.append(msg.dlc)
|
|
116
|
+
|
|
117
|
+
# Encode data
|
|
118
|
+
payload.extend(msg.data)
|
|
119
|
+
|
|
120
|
+
# Calculate checksum over the data and append
|
|
121
|
+
calculated_checksum = self.calculate_crc(payload)
|
|
122
|
+
calculated_checksum_bytes = calculated_checksum.to_bytes(2, byteorder="big")
|
|
123
|
+
payload.extend(calculated_checksum_bytes)
|
|
124
|
+
|
|
125
|
+
# Stuff frame and pack in frame delimiters
|
|
126
|
+
packed_frame = bytearray()
|
|
127
|
+
packed_frame.append(0x7E)
|
|
128
|
+
|
|
129
|
+
for i in range(len(payload)):
|
|
130
|
+
if (payload[i] == 0x7D) or (payload[i] == 0x7E):
|
|
131
|
+
packed_frame.append(0x7D)
|
|
132
|
+
packed_frame.append(payload[i] ^ 0b00100000)
|
|
133
|
+
else:
|
|
134
|
+
packed_frame.append(payload[i])
|
|
135
|
+
|
|
136
|
+
packed_frame.append(0x7E)
|
|
137
|
+
|
|
138
|
+
self._channel.write(packed_frame)
|
|
139
|
+
|
|
140
|
+
return
|
|
141
|
+
|
|
142
|
+
def shutdown(self) -> None:
|
|
143
|
+
super().shutdown()
|
|
144
|
+
|
|
145
|
+
if self._channel is not None:
|
|
146
|
+
self._channel.close()
|
|
147
|
+
self._channel = None
|
|
148
|
+
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
def _parse(self):
|
|
152
|
+
# If there are fewer bytes available than a single frame, stop parsing
|
|
153
|
+
if len(self._data) < 16:
|
|
154
|
+
return
|
|
155
|
+
|
|
156
|
+
while len(self._data) >= 16:
|
|
157
|
+
# Find start and end indices of next frame
|
|
158
|
+
try:
|
|
159
|
+
start_of_frame = self._data.index(0x7E, 0)
|
|
160
|
+
except ValueError:
|
|
161
|
+
# No start flag
|
|
162
|
+
break
|
|
163
|
+
|
|
164
|
+
try:
|
|
165
|
+
end_of_frame = self._data.index(0x7E, start_of_frame + 1)
|
|
166
|
+
except ValueError:
|
|
167
|
+
# No end flag (yet)
|
|
168
|
+
break
|
|
169
|
+
|
|
170
|
+
# Skip all bytes up to the start of frame
|
|
171
|
+
for _ in range(start_of_frame):
|
|
172
|
+
self._data.popleft()
|
|
173
|
+
|
|
174
|
+
# Extract the frame
|
|
175
|
+
current_frame_raw = bytearray()
|
|
176
|
+
|
|
177
|
+
for _ in range(end_of_frame - start_of_frame + 1):
|
|
178
|
+
current_frame_raw.append(self._data.popleft())
|
|
179
|
+
|
|
180
|
+
parsed_frame = self._parse_frame(current_frame_raw)
|
|
181
|
+
if parsed_frame is not None:
|
|
182
|
+
self._events.append(parsed_frame)
|
|
183
|
+
|
|
184
|
+
return
|
|
185
|
+
|
|
186
|
+
def _parse_frame(self, current_frame_raw):
|
|
187
|
+
frame = can.Message()
|
|
188
|
+
|
|
189
|
+
# Set common frame properties
|
|
190
|
+
frame.is_fd = False
|
|
191
|
+
frame.is_error_frame = False
|
|
192
|
+
frame.bitrate_switch = False
|
|
193
|
+
frame.error_state_indicator = False
|
|
194
|
+
|
|
195
|
+
# Remove the start and end of frame markers
|
|
196
|
+
current_frame_raw = current_frame_raw[1:-1]
|
|
197
|
+
|
|
198
|
+
# Look for any stuffed bytes, and revert them
|
|
199
|
+
current_frame = bytearray()
|
|
200
|
+
stuff_next = False
|
|
201
|
+
for i in range(len(current_frame_raw)):
|
|
202
|
+
if current_frame_raw[i] == 0x7D:
|
|
203
|
+
stuff_next = True
|
|
204
|
+
continue
|
|
205
|
+
|
|
206
|
+
current_byte = current_frame_raw[i]
|
|
207
|
+
if stuff_next is True:
|
|
208
|
+
current_byte ^= 0b00100000
|
|
209
|
+
stuff_next = False
|
|
210
|
+
else:
|
|
211
|
+
current_byte = current_frame_raw[i]
|
|
212
|
+
|
|
213
|
+
current_frame.append(current_byte)
|
|
214
|
+
|
|
215
|
+
# Extract the checksum
|
|
216
|
+
checksum = current_frame[-2:]
|
|
217
|
+
current_frame = current_frame[:-2]
|
|
218
|
+
calculated_checksum = self.calculate_crc(current_frame).to_bytes(2, byteorder="big")
|
|
219
|
+
if checksum != calculated_checksum:
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
# Read the frame type
|
|
223
|
+
frame_type = current_frame[0]
|
|
224
|
+
|
|
225
|
+
if (frame_type == 0x01) or (frame_type == 0x02) or (frame_type == 0x21) or (frame_type == 0x22):
|
|
226
|
+
# Regular CAN traffic, frame either received or transmitted.
|
|
227
|
+
# Extract the metadata
|
|
228
|
+
seconds, milliseconds, message_id, data_length = struct.unpack_from("!IHIB", current_frame, 1)
|
|
229
|
+
|
|
230
|
+
frame.timestamp = datetime.datetime.now(tz=datetime.timezone.utc).timestamp()
|
|
231
|
+
frame.arbitration_id = message_id & 0x1FFFFFFF
|
|
232
|
+
frame.is_extended_id = (message_id & 0x20000000) == 0x20000000
|
|
233
|
+
frame.is_rx = True if frame_type == 1 else False
|
|
234
|
+
frame.dlc = self.dlc_to_length(data_length)
|
|
235
|
+
|
|
236
|
+
frame.is_remote_frame = (frame_type & 0x10) == 0x10
|
|
237
|
+
frame.is_fd = (frame_type & 0x20) == 0x20
|
|
238
|
+
|
|
239
|
+
frame.data = current_frame[12:12 + frame.dlc]
|
|
240
|
+
else:
|
|
241
|
+
# Unknown frame type
|
|
242
|
+
pass
|
|
243
|
+
|
|
244
|
+
return frame
|
|
245
|
+
|
|
246
|
+
@staticmethod
|
|
247
|
+
def dlc_to_length(dlc):
|
|
248
|
+
length = dlc
|
|
249
|
+
|
|
250
|
+
if 8 >= dlc:
|
|
251
|
+
length = dlc
|
|
252
|
+
elif 12 >= dlc > 8:
|
|
253
|
+
length = 4 * (dlc - 9) + 12
|
|
254
|
+
elif 15 >= dlc > 12:
|
|
255
|
+
length = 16 * (dlc - 13) + 32
|
|
256
|
+
|
|
257
|
+
return length
|
|
258
|
+
|
|
259
|
+
@staticmethod
|
|
260
|
+
def calculate_crc(data: bytes) -> int:
|
|
261
|
+
initial = 0x0000
|
|
262
|
+
polynomial = 0xA001
|
|
263
|
+
final = 0x0000
|
|
264
|
+
|
|
265
|
+
crc = initial
|
|
266
|
+
|
|
267
|
+
for b in data:
|
|
268
|
+
crc ^= b
|
|
269
|
+
|
|
270
|
+
for i in range(8):
|
|
271
|
+
if crc & 0x0001:
|
|
272
|
+
crc >>= 1
|
|
273
|
+
crc ^= polynomial
|
|
274
|
+
else:
|
|
275
|
+
crc >>= 1
|
|
276
|
+
|
|
277
|
+
return crc ^ final
|
|
278
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: python-can-csscan-serial
|
|
3
|
+
Version: 2.4.0
|
|
4
|
+
Summary: Interact with CSS Electronics USB devices through Python
|
|
5
|
+
Project-URL: Homepage, https://canlogger.csselectronics.com/tools-docs/csscan_serial/python_csscan/
|
|
6
|
+
Author: CSS Electronics
|
|
7
|
+
Author-email: contact@csselectronics.com
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.7
|
|
14
|
+
Requires-Dist: pyserial>=3.5.0
|
|
15
|
+
Requires-Dist: python-can>=4.3.1
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# python-can-csscan-serial
|
|
19
|
+
|
|
20
|
+
The **python-can-csscan-serial** package allows for communication over USB with devices from [CSS Electronics](https://www.csselectronics.com/).
|
|
21
|
+
|
|
22
|
+
For usage guidelines, consult the manual or the documentation for the [**python-can**](https://python-can.readthedocs.io/en/stable/) package.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
python_can_csscan_serial/CSSCANSerial.py,sha256=ze9eFOxMJSGXpvZ22H8J705pT3QVoWa4cMtjGsVHOh4,9118
|
|
2
|
+
python_can_csscan_serial/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
python_can_csscan_serial-2.4.0.dist-info/METADATA,sha256=WgVztPVKgCGlshsqy7qtp3rBvcagC3lv3tw510w1Q7Q,924
|
|
4
|
+
python_can_csscan_serial-2.4.0.dist-info/WHEEL,sha256=TJPnKdtrSue7xZ_AVGkp9YXcvDrobsjBds1du3Nx6dc,87
|
|
5
|
+
python_can_csscan_serial-2.4.0.dist-info/entry_points.txt,sha256=1FnKqCf9HuERSJK6nEw2H4gO2TYcVSnW7h9RX8gqdQ0,83
|
|
6
|
+
python_can_csscan_serial-2.4.0.dist-info/licenses/LICENSE,sha256=5PR6baFF-VyANXuj_nFw8Cr4rKZbjojX3-I12Nk3oas,1091
|
|
7
|
+
python_can_csscan_serial-2.4.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 CSS-Electronics
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|