@@ -42,7 +42,7 @@ def __init__(self,
4242 self .connected = True
4343 except socket .error as e :
4444 self .connected = False
45- logging .error (f"Could not reconnect : { e } . Retrying..." )
45+ logging .error (f"Could not connect : { e } . Retrying..." )
4646 self .s .close ()
4747 sleep (reconnect_delay )
4848
@@ -65,42 +65,46 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:
6565 struct .pack_into ('>L' , frame_id , 0 , int (msg .arbitration_id )) # Next 4 bytes contain CAN ID
6666
6767 frame_data = bytearray (8 )
68- frame_data [0 :0 + len (msg .data )] = msg .data # Following 8 bytes contain CAN data
68+ frame_data [0 :len (msg .data )] = msg .data # Following 8 bytes contain CAN data
6969
7070 raw_message = frame_information + frame_id + frame_data # Combine to make full message
7171
7272 # Set timeout for sending
7373 if timeout is not None :
7474 self .s .settimeout (timeout )
75-
7675 try :
7776 self .s .send (raw_message )
7877 except TimeoutError :
79- self .s .settimeout (None )
80- return (None , False )
78+ # Timeout
79+ msg = None
80+ pass
8181 except socket .error as e :
8282 self .connected = False
8383 logging .error (f"Socket error: { e } " )
8484 if self .reconnect :
85- while not self .connected :
86- try :
87- logging .error ("Reconnecting..." )
88- self .s .close ()
89- self .s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
90- self .s .connect ((self .host , self .port ))
91- self .connected = True
92- logging .error ("Reconnected." )
93- except Exception as e :
94- logging .error (f"Could not reconnect: { e } . Retrying..." )
95- sleep (self .reconnect_delay )
85+ self .do_reconnect ()
9686 else :
97- return (None , False )
87+ msg = None
88+ finally :
89+ # Reset timeout
90+ if timeout is not None :
91+ self .s .settimeout (None )
9892
99- # Reset timeout
100- if timeout is not None :
101- self .s .settimeout (None )
10293 return (msg , False )
10394
95+ def do_reconnect (self ):
96+ while not self .connected :
97+ try :
98+ logging .error ("Reconnecting..." )
99+ self .s .close ()
100+ self .s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
101+ self .s .connect ((self .host , self .port ))
102+ self .connected = True
103+ logging .error ("Reconnected." )
104+ except Exception as e :
105+ logging .error (f"Could not reconnect: { e } . Retrying..." )
106+ sleep (self .reconnect_delay )
107+
104108 def _recv_internal (self , timeout : Optional [float ]) -> Tuple [Optional [Message ], bool ]:
105109 """Expect a message to receive from the socket
106110 :param timeout: timeout (in seconds) to wait for expected data to come in.
@@ -115,6 +119,9 @@ def _recv_internal(self, timeout: Optional[float]) -> Tuple[Optional[Message], b
115119 flag_success = False
116120 while not flag_success :
117121 try :
122+ # The USR-CANET will always return 13 bytes per CAN packet
123+ # But sometimes will return TCP packets with 2 CAN packets sandwiched together
124+ # This will seperate the sandwich.
118125 data = self .s .recv (13 )
119126 flag_success = True
120127 except TimeoutError :
@@ -124,25 +131,22 @@ def _recv_internal(self, timeout: Optional[float]) -> Tuple[Optional[Message], b
124131 self .connected = False
125132 logging .error (f"Socket error: { e } " )
126133 if self .reconnect :
127- while not self .connected :
128- try :
129- logging .error ("Reconnecting..." )
130- self .s .close ()
131- self .s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
132- self .s .connect ((self .host , self .port ))
133- self .connected = True
134- logging .error ("Reconnected." )
135- except Exception as e :
136- logging .error (f"Could not reconnect: { e } . Retrying..." )
137- sleep (self .reconnect_delay )
134+ self .do_reconnect ()
138135 else :
139- return (None , False )
136+ self .s .settimeout (None )
137+ return (None , False )
138+
140139
141140 # Check received length
142141 if len (data ) == 0 :
142+ self .s .settimeout (None )
143143 return (None , False )
144144
145145 # Decode CAN frame
146+ # CAN frame from USR-CANET200 looks like:
147+ # --------------------- ------------------ --------------------
148+ # | frame_info (1 byte) | can_id (4 bytes) | can_data (8 bytes) |
149+ # --------------------- ------------------ --------------------
146150 CAN_FRAME = struct .Struct ('>BI8s' )
147151 frame_info , can_id , can_data = CAN_FRAME .unpack_from (data )
148152 dlc = frame_info & 0x0F # Last 4 bits indicate data length
0 commit comments