@@ -147,24 +147,22 @@ def __init__(
147
147
def _recv_internal (
148
148
self , timeout : Optional [float ]
149
149
) -> Tuple [Optional [can .Message ], bool ]:
150
- canMessages = (ctypes .POINTER (OCI_CANMessageEx ) * 1 )()
151
-
152
- m = OCI_CANMessageEx ()
153
- canMessages [0 ].contents = m
150
+ ociMsgs = (ctypes .POINTER (OCI_CANMessageEx ) * 1 )()
151
+ ociMsg = OCI_CANMessageEx ()
152
+ ociMsgs [0 ] = ctypes .pointer (ociMsg )
154
153
155
154
count = ctypes .c_uint32 ()
156
- remaining = ctypes .c_uint32 ()
157
- if timeout is not None :
155
+ if timeout is not None : # wait for specified time
158
156
t = OCI_Time (round (timeout * self .tickFrequency ))
159
- else :
157
+ else : # wait indefinitely
160
158
t = OCI_NO_TIME
161
159
ec = OCI_ReadCANDataEx (
162
160
self .rxQueue ,
163
161
t ,
164
- canMessages ,
162
+ ociMsgs ,
165
163
1 ,
166
164
ctypes .byref (count ),
167
- ctypes . byref ( remaining ) ,
165
+ None ,
168
166
)
169
167
if ec != 0x0 :
170
168
text = ctypes .create_string_buffer (500 )
@@ -174,52 +172,44 @@ def _recv_internal(
174
172
msg = None
175
173
176
174
if count .value != 0 :
177
- m = canMessages [ 0 ]. contents
178
- if m . type == OCI_CANFDRX_MESSAGE . value :
175
+ if ociMsg . type == OCI_CANFDRX_MESSAGE . value :
176
+ ociRxMsg = ociMsg . data . canFDRxMessage
179
177
msg = can .Message (
180
- timestamp = float (m .data .canFDRxMessage .timeStamp )
181
- / self .tickFrequency
178
+ timestamp = float (ociRxMsg .timeStamp ) / self .tickFrequency
182
179
+ self .timeOffset ,
183
- arbitration_id = m .data .canFDRxMessage .frameID ,
184
- is_extended_id = bool (
185
- m .data .canFDRxMessage .flags & OCI_CAN_MSG_FLAG_EXTENDED
186
- ),
180
+ arbitration_id = ociRxMsg .frameID ,
181
+ is_extended_id = bool (ociRxMsg .flags & OCI_CAN_MSG_FLAG_EXTENDED ),
187
182
is_remote_frame = bool (
188
- m . data . canFDRxMessage .flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
183
+ ociRxMsg .flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
189
184
),
190
185
# is_error_frame=False,
191
186
# channel=None,
192
- dlc = m . data . canFDRxMessage .size ,
193
- data = m .data . canFDRxMessage . data [0 : m . data . canFDRxMessage .size ],
187
+ dlc = ociRxMsg .size ,
188
+ data = ociRxMsg .data [0 : ociRxMsg .size ],
194
189
is_fd = True ,
195
- is_rx = not bool (
196
- m .data .canFDRxMessage .flags & OCI_CAN_MSG_FLAG_SELFRECEPTION
197
- ),
190
+ is_rx = not bool (ociRxMsg .flags & OCI_CAN_MSG_FLAG_SELFRECEPTION ),
198
191
bitrate_switch = bool (
199
- m . data . canFDRxMessage .flags & OCI_CAN_MSG_FLAG_FD_DATA_BIT_RATE
192
+ ociRxMsg .flags & OCI_CAN_MSG_FLAG_FD_DATA_BIT_RATE
200
193
),
201
194
# error_state_indicator=False,
202
195
# check=False,
203
196
)
204
- elif m .type == OCI_CAN_RX_MESSAGE .value :
197
+ elif ociMsg .type == OCI_CAN_RX_MESSAGE .value :
198
+ ociRxMsg = ociMsg .data .rxMessage
205
199
msg = can .Message (
206
- timestamp = float (m . data . rxMessage .timeStamp ) / self .tickFrequency
200
+ timestamp = float (ociRxMsg .timeStamp ) / self .tickFrequency
207
201
+ self .timeOffset ,
208
- arbitration_id = m .data .rxMessage .frameID ,
209
- is_extended_id = bool (
210
- m .data .rxMessage .flags & OCI_CAN_MSG_FLAG_EXTENDED
211
- ),
202
+ arbitration_id = ociRxMsg .frameID ,
203
+ is_extended_id = bool (ociRxMsg .flags & OCI_CAN_MSG_FLAG_EXTENDED ),
212
204
is_remote_frame = bool (
213
- m . data . rxMessage .flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
205
+ ociRxMsg .flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
214
206
),
215
207
# is_error_frame=False,
216
208
# channel=None,
217
- dlc = m . data . rxMessage .dlc ,
218
- data = m .data . rxMessage . data [0 : m . data . rxMessage .dlc ],
209
+ dlc = ociRxMsg .dlc ,
210
+ data = ociRxMsg .data [0 : ociRxMsg .dlc ],
219
211
# is_fd=False,
220
- is_rx = not bool (
221
- m .data .rxMessage .flags & OCI_CAN_MSG_FLAG_SELFRECEPTION
222
- ),
212
+ is_rx = not bool (ociRxMsg .flags & OCI_CAN_MSG_FLAG_SELFRECEPTION ),
223
213
# bitrate_switch=False,
224
214
# error_state_indicator=False,
225
215
# check=False,
@@ -228,37 +218,34 @@ def _recv_internal(
228
218
return (msg , True )
229
219
230
220
def send (self , msg : can .Message , timeout : Optional [float ] = None ) -> None :
231
- canMessages = (ctypes .POINTER (OCI_CANMessageEx ) * 1 )()
221
+ ociMsgs = (ctypes .POINTER (OCI_CANMessageEx ) * 1 )()
222
+ ociMsg = OCI_CANMessageEx ()
223
+ ociMsgs [0 ] = ctypes .pointer (ociMsg )
232
224
233
- m = OCI_CANMessageEx ()
225
+ if msg .is_fd :
226
+ ociMsg .type = OCI_CANFDTX_MESSAGE
227
+ ociTxMsg = ociMsg .data .canFDTxMessage
228
+ ociTxMsg .size = msg .dlc
229
+ else :
230
+ ociMsg .type = OCI_CAN_TX_MESSAGE
231
+ ociTxMsg = ociMsg .data .txMessage
232
+ ociTxMsg .dlc = msg .dlc
233
+
234
+ # set fields common to CAN / CAN-FD
235
+ ociTxMsg .frameID = msg .arbitration_id
236
+ ociTxMsg .flags = 0
237
+ if msg .is_extended_id :
238
+ ociTxMsg .flags |= OCI_CAN_MSG_FLAG_EXTENDED
239
+ if msg .is_remote_frame :
240
+ ociTxMsg .flags |= OCI_CAN_MSG_FLAG_REMOTE_FRAME
241
+ ociTxMsg .data = tuple (msg .data )
234
242
235
243
if msg .is_fd :
236
- m .type = OCI_CANFDTX_MESSAGE
237
- m .data .canFDTxMessage .frameID = msg .arbitration_id
238
- m .data .canFDTxMessage .flags = 0
239
- if msg .is_extended_id :
240
- m .data .canFDTxMessage .flags |= OCI_CAN_MSG_FLAG_EXTENDED
241
- if msg .is_remote_frame :
242
- m .data .canFDTxMessage .flags |= OCI_CAN_MSG_FLAG_REMOTE_FRAME
243
- m .data .canFDTxMessage .flags |= OCI_CAN_MSG_FLAG_FD_DATA
244
+ ociTxMsg .flags |= OCI_CAN_MSG_FLAG_FD_DATA
244
245
if msg .bitrate_switch :
245
- m .data .canFDTxMessage .flags |= OCI_CAN_MSG_FLAG_FD_DATA_BIT_RATE
246
- m .data .canFDTxMessage .size = msg .dlc
247
- m .data .canFDTxMessage .data = tuple (msg .data )
248
- else :
249
- m .type = OCI_CAN_TX_MESSAGE
250
- m .data .txMessage .frameID = msg .arbitration_id
251
- m .data .txMessage .flags = 0
252
- if msg .is_extended_id :
253
- m .data .txMessage .flags |= OCI_CAN_MSG_FLAG_EXTENDED
254
- if msg .is_remote_frame :
255
- m .data .txMessage .flags |= OCI_CAN_MSG_FLAG_REMOTE_FRAME
256
- m .data .txMessage .dlc = msg .dlc
257
- m .data .txMessage .data = tuple (msg .data )
258
-
259
- canMessages [0 ].contents = m
260
-
261
- ec = OCI_WriteCANDataEx (self .txQueue , OCI_NO_TIME , canMessages , 1 , None )
246
+ ociTxMsg .flags |= OCI_CAN_MSG_FLAG_FD_DATA_BIT_RATE
247
+
248
+ ec = OCI_WriteCANDataEx (self .txQueue , OCI_NO_TIME , ociMsgs , 1 , None )
262
249
if ec != 0x0 :
263
250
raise CanOperationError (f"OCI_WriteCANDataEx failed with error 0x{ ec :X} " )
264
251
0 commit comments