modbus_MediaPipe/import_can.py

20 lines
496 B
Python

import can
import time
bus = can.interface.Bus(bustype="socketcan", channel="can0", bitrate=500000)
node_id = 1
cob_id = 0x200 + node_id # 0x201
value = 1234
data_bytes = value.to_bytes(2, byteorder="little", signed=True)
msg = can.Message(arbitration_id=cob_id, data=data_bytes, is_extended_id=False)
for i in range(10):
try:
bus.send(msg)
print(f"Sent {value} to PLC (COB-ID=0x{cob_id:X})")
except can.CanError:
print("Message NOT sent")
time.sleep(1)