Skip to content

Commit 6afeb8b

Browse files
author
Felix Nieuwenhuizen
committed
refactor send and recv methods
1 parent 0a2aa84 commit 6afeb8b

File tree

1 file changed

+50
-63
lines changed

1 file changed

+50
-63
lines changed

can/interfaces/etas/__init__.py

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,22 @@ def __init__(
147147
def _recv_internal(
148148
self, timeout: Optional[float]
149149
) -> 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)
154153

155154
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
158156
t = OCI_Time(round(timeout * self.tickFrequency))
159-
else:
157+
else: # wait indefinitely
160158
t = OCI_NO_TIME
161159
ec = OCI_ReadCANDataEx(
162160
self.rxQueue,
163161
t,
164-
canMessages,
162+
ociMsgs,
165163
1,
166164
ctypes.byref(count),
167-
ctypes.byref(remaining),
165+
None,
168166
)
169167
if ec != 0x0:
170168
text = ctypes.create_string_buffer(500)
@@ -174,52 +172,44 @@ def _recv_internal(
174172
msg = None
175173

176174
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
179177
msg = can.Message(
180-
timestamp=float(m.data.canFDRxMessage.timeStamp)
181-
/ self.tickFrequency
178+
timestamp=float(ociRxMsg.timeStamp) / self.tickFrequency
182179
+ 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),
187182
is_remote_frame=bool(
188-
m.data.canFDRxMessage.flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
183+
ociRxMsg.flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
189184
),
190185
# is_error_frame=False,
191186
# 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],
194189
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),
198191
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
200193
),
201194
# error_state_indicator=False,
202195
# check=False,
203196
)
204-
elif m.type == OCI_CAN_RX_MESSAGE.value:
197+
elif ociMsg.type == OCI_CAN_RX_MESSAGE.value:
198+
ociRxMsg = ociMsg.data.rxMessage
205199
msg = can.Message(
206-
timestamp=float(m.data.rxMessage.timeStamp) / self.tickFrequency
200+
timestamp=float(ociRxMsg.timeStamp) / self.tickFrequency
207201
+ 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),
212204
is_remote_frame=bool(
213-
m.data.rxMessage.flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
205+
ociRxMsg.flags & OCI_CAN_MSG_FLAG_REMOTE_FRAME
214206
),
215207
# is_error_frame=False,
216208
# 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],
219211
# 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),
223213
# bitrate_switch=False,
224214
# error_state_indicator=False,
225215
# check=False,
@@ -228,37 +218,34 @@ def _recv_internal(
228218
return (msg, True)
229219

230220
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)
232224

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)
234242

235243
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
244245
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)
262249
if ec != 0x0:
263250
raise CanOperationError(f"OCI_WriteCANDataEx failed with error 0x{ec:X}")
264251

0 commit comments

Comments
 (0)