根据can高,can低分别接线,我的根据手册就是31、32、33、34,瑞泰有两路can(可以短接自己测试,31连接33,32连接34)
注1:需要终端自己带120Ω电阻!
注2:用户需要有sudo权限
import os
os.system('sudo modprobe can')
os.system('sudo modprobe can_raw')
os.system('sudo modprobe mttcan')
os.system('sudo ip link set can0 type can bitrate 500000')
os.system('sudo ip link set up can0')
os.system('sudo ip link set can1 type can bitrate 500000')
os.system('sudo ip link set up can1')
import time
import os
import can
import threading
can0 =can.interface.Bus(channel='can0',bustype='socketcan')
b0=0x10
b1=0x00
b2=0x00
b3=0x04
b4=0x00
b5=0x55
b6=0x20
b7=0x61
msg =can.Message(arbitration_id=0x469,data=[b0,b1,b2,b3,b4,b5,b6,b7],extended_id=False)
#msg =can.Message(arbitration_id=0x469,data=[0,1,2,3,4,5,6,7],extended_id=False)
can0.send(msg)
time.sleep(1)
import os
import time
import can
import threading
can0 = can.interface.Bus(channel='can0',bustype='socketcan')
while 1:
msg=can0.recv()
print(msg)
import os
os.system('sudo ip link set can0 down')
os.system('sudo ip link set can1 down')
import os
import time
os.system('sudo ip link set can0 down')
os.system('sudo ip link set can1 down')
time.sleep(1)
os.system('sudo modprobe can')
os.system('sudo modprobe can_raw')
os.system('sudo modprobe mttcan')
os.system('sudo ip link set can0 type can bitrate 500000')
os.system('sudo ip link set up can0')
os.system('sudo ip link set can1 type can bitrate 500000')
os.system('sudo ip link set up can1')
项目需求线控转向,目前就是输入一个角度,控制他转到指定角度即可
import time
import os
import can
import threading
import sys
angle=0
if __name__=='__main__':
can0 =can.interface.Bus(channel='can0',bustype='socketcan')
angle = int(sys.argv[1]) + 1024
b0=0x20
b1=0x00
b2=0x00
b3=angle>>8
b4=angle - (b3<<8)
b5=0x00
b6=0x64
b7=b0^b1^b2^b3^b4^b5^b6
#print([b0,b1,b2,b3,b4,b5,b6,b7])
msg =can.Message(arbitration_id=0x469,data=[b0,b1,b2,b3,b4,b5,b6,b7],extended_id=False)
#msg =can.Message(arbitration_id=0x469,data=[0,1,2,3,4,5,6,7],extended_id=False)
can0.send(msg)
time.sleep(0.1)