1414
1515_WS_WIRE_LEN = 13 # fixed length of Waveshare TCP CAN frame
1616
17- class _WSFrameMethods :
17+
18+ @dataclass (slots = True )
19+ class _WSFrame :
20+ """Waveshare fixed 13-byte TCP wire frame (Py3.10+ with dataclass slots)."""
21+
22+ can_id : int
23+ data : bytes
24+ extended : bool
25+ rtr : bool
26+ dlc : int
27+
1828 @staticmethod
1929 def from_bytes (buf : bytes ) -> "_WSFrame" :
2030 """Decode a 13-byte buffer into a _WSFrame."""
@@ -27,7 +37,7 @@ def from_bytes(buf: bytes) -> "_WSFrame":
2737 if dlc > 8 :
2838 raise ValueError (f"Invalid DLC { dlc } " )
2939 can_id = int .from_bytes (buf [1 :5 ], "big" , signed = False )
30- data = bytes (buf [5 : 5 + dlc ])
40+ data = bytes (buf [5 : 5 + dlc ])
3141 return _WSFrame (can_id = can_id , data = data , extended = extended , rtr = rtr , dlc = dlc )
3242
3343 def to_bytes (self ) -> bytes :
@@ -41,34 +51,18 @@ def to_bytes(self) -> bytes:
4151 else :
4252 if self .dlc != len (self .data ):
4353 raise ValueError ("dlc must equal len(data) for data frames" )
44- b0 = (0x80 if self .extended else 0 ) | (0x40 if self .rtr else 0 ) | (self .dlc & 0x0F )
54+ b0 = (
55+ (0x80 if self .extended else 0 )
56+ | (0x40 if self .rtr else 0 )
57+ | (self .dlc & 0x0F )
58+ )
4559 out = bytearray (_WS_WIRE_LEN )
4660 out [0 ] = b0
4761 out [1 :5 ] = int (self .can_id ).to_bytes (4 , "big" , signed = False )
4862 if not self .rtr and self .dlc :
49- out [5 : 5 + self .dlc ] = self .data
63+ out [5 : 5 + self .dlc ] = self .data
5064 return bytes (out )
5165
52- if sys .version_info >= (3 , 10 ):
53- @dataclass (slots = True )
54- class _WSFrame (_WSFrameMethods ):
55- """Waveshare fixed 13-byte TCP wire frame (Py3.10+ with dataclass slots)."""
56- can_id : int
57- data : bytes
58- extended : bool
59- rtr : bool
60- dlc : int
61- else :
62- @dataclass
63- class _WSFrame (_WSFrameMethods ):
64- """Waveshare fixed 13-byte TCP wire frame (Py3.9 with manual __slots__)."""
65- __slots__ = ("can_id" , "data" , "extended" , "rtr" , "dlc" )
66- can_id : int
67- data : bytes
68- extended : bool
69- rtr : bool
70- dlc : int
71-
7266
7367def _matches_filter (msg : can .Message , flt : dict [str , Any ]) -> bool :
7468 """
@@ -268,7 +262,9 @@ def send(self, msg: can.Message, timeout: Optional[float] = None) -> None:
268262 try :
269263 _send_all (self ._sock , payload )
270264 except OSError as e :
271- raise can .CanError (f"WaveShareBus.send failed (id=0x{ arb_id :X} ): { e } " ) from e
265+ raise can .CanError (
266+ f"WaveShareBus.send failed (id=0x{ arb_id :X} ): { e } "
267+ ) from e
272268
273269 # record for own-echo suppression (if active)
274270 if self ._suppress_own :
0 commit comments