I want to be able to send a message on canbus one with 2 seconds between them
have tried some python can examples with the same result can send and receive with python-can code
now I don't know if I'm doing something wrong or the code
run with a raspberry pi 4b with a can bus module mcp2515 os bookworm 64bit have installed python-can 4.5
import can
with can.interface.Bus(interface="socketcan", channel="can0") as bus:
msg = can.Message(
arbitration_id=0x020,
data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],
is_extended_id=False
)
task = bus.send_periodic(msgs=msg, period=1)
Exception has occurred: CanOperationError
Couldn't send CAN BCM frame due to OS Error: Invalid argument You are probably referring to a non-existing frame. [Error Code 22]
OSError: [Errno 22] Invalid argument
The above exception was the direct cause of the following exception:
File "/home/pican/foobar/project/test_5.py", line 7, in <module>
task = bus.send_periodic(msgs=msg,period=1,store_task=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
can.exceptions.CanOperationError: Couldn't send CAN BCM frame due to OS Error: Invalid argument You are probably referring to a non-existing frame. [Error Code 22]
#!/usr/bin/env python
"""
This example exercises the periodic task's multiple message sending capabilities
to send a message containing a counter and a checksum.
Expects a vcan0 interface:
python3 -m examples.crc
"""
import logging
import time
import can
logging.basicConfig(level=logging.INFO)
def crc_send(bus):
"""
Sends periodic messages every 1 s with no explicit timeout. Modifies messages
after 8 seconds, sends for 10 more seconds, then stops.
"""
msg = can.Message(arbitration_id=0x12345678, data=[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07],is_extended_id=True)
messages = build_crc_msgs(msg)
print("Starting to send a message with updating counter and checksum every 1 s for 8 s")
task = BUS.send_periodic(msg, 1)
assert isinstance(task, can.CyclicSendTaskABC)
time.sleep(8)
msg = can.Message(arbitration_id=0x12345678, data=[8, 9, 10, 11, 12, 13, 14, 0])
messages = build_crc_msgs(msg)
print("Sending modified message data every 1 s for 10 s")
task.modify_data(messages)
time.sleep(10)
task.stop()
print("stopped cyclic send")
def build_crc_msgs(msg):
"""
Using the input message as base, create 16 messages with SAE J1939 SPN 3189 counters
and SPN 3188 checksums placed in the final byte.
"""
messages = []
for counter in range(16):
checksum = compute_xbr_checksum(msg, counter)
msg.data[7] = counter + (checksum << 4)
messages.append(
can.Message(arbitration_id=msg.arbitration_id, data=msg.data[:])
)
return messages
def compute_xbr_checksum(message, counter):
"""
Computes an XBR checksum per SAE J1939 SPN 3188.
"""
checksum = sum(message.data[:7])
checksum += sum(message.arbitration_id.to_bytes(length=4, byteorder="big"))
checksum += counter & 0x0F
xbr_checksum = ((checksum >> 4) + checksum) & 0x0F
return xbr_checksum
if __name__ == "__main__":
for interface, channel in [('socketcan', 'can0')]:
print(f"Carrying out crc test with {interface} interface")
with can.interface.Bus(channel='can0',interface='socketcan', bitrate=500000) as BUS:
crc_send(BUS)
time.sleep(2)
Exception has occurred: CanOperationError
Couldn't send CAN BCM frame due to OS Error: Invalid argument You are probably referring to a non-existing frame. [Error Code 22]
OSError: [Errno 22] Invalid argument
The above exception was the direct cause of the following exception:
File "/home/pican/foobar/project/crc_3.py", line 30, in crc_send
task = BUS.send_periodic(msg, 1)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pican/foobar/project/crc_3.py", line 78, in <module>
crc_send(BUS)
can.exceptions.CanOperationError: Couldn't send CAN BCM frame due to OS Error: Invalid argument You are probably referring to a non-existing frame. [Error Code 22]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745158189a4614251.html
评论列表(0条)