安装好python-can 后,接者又安装了can-utils。
用
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes')
就一直报错,后面找到资料用can.rc来配置才成功。创建python代码如下:
#!/usr/bin/env python
"""
This example shows how sending a single message works.
"""
import time
import can
from can.interface import Bus
can.rc['bitrate'] = '500000'
can.rc['interface']='socketcan'
can.rc['channel'] = 'can0'
can.rc['bitrate'] = '500000'
bus = Bus()
def send_one():
msg = can.Message(
arbitration_id=0xC0FFEE, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=True
)
try:
bus.send(msg)
print(f"Message sent on {bus.channel_info}")
except can.CanError:
print("Message NOT sent")
if __name__ == "__main__":
while True:
send_one()
time.sleep(1)
接上can分析仪就可以实现用python发送数据了:
更多回帖