import sys
import getopt
import os
import time
import can
"""
can.message(timestamp=0.0, //时间戳
arbitration_id=0, //报文id
is_extended_id=True, //是否为扩展帧
is_remote_frame=False, //是否为远程帧
is_error_frame=False, //是否为错误帧
channel=None, //通道
dlc=None, //dlc大小
data=None, //数据
is_fd=False, //是否为canfd
is_rx=True, //是否为接收帧
bitrate_switch=False, //比特率开关
error_state_indicator=False,
check=False)
"""
"""
recv(timeout=None)
Block waiting for a message from the Bus.
None to wait indefinitely.
"""
def udsidscan (canbus):
idstart = 0x700
idend = 0x7FF
datareq = [0x02, 0x10, 0x01, 0x55, 0x55, 0x55, 0x55, 0x55]
dataresp = [0x06, 0x50, 0x01]
print("\r\nMessage sent on {}".format(canbus.channel_info))
for id in range(idstart, idend):
msgreq = can.Message(timestamp=time.time(), arbitration_id=id, data=datareq, is_extended_id=False)
canbus.send(msgreq)
t_end = time.time() + 1 # 超过100ms未收到uds响应报文则认为该id不支持诊断
while time.time() < t_end:
msgresp = canbus.recv(0.03) # 阻塞接收报文的时间,超过100ms未收到任何报文则函数执行结束,100ms内收到任一报文则函数执行结束
if msgresp is not None and list(msgresp.data)[0:3] == dataresp:
print("----------------------------------------------------------------------")
print(msgreq)
print(msgresp)
print("----------------------------------------------------------------------")
helpinfo = "udscan caninterface [options]\n" \
"-h,--help \n" \
"-f 0:can 1:canfd\n" \
"--bitrate default 500000\n" \
"--dbitrate default 2000000\n" \
"--bustype can bus type\n" \
"--channel can channel"
def main (argv):
print()
if len(sys.argv) < 2:
print("arguments number [%d] is invalid." %(len(sys.argv)))
exit(0)
if len(sys.argv) == 2:
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
for opt, arg in opts:
if opt in ("-h", "--help"):
print(helpinfo)
exit(0)
canintf = sys.argv[1]
# default configurations
canfd = 0
bitrate = 500000
dbitrate = 2000000
channel = "can0"
bustype = "socketcan"
# arguments parser
opts, args = getopt.getopt(sys.argv[2:], "f", ["bitrate=", "dbitrate=", "channel=", "bustype="])
for opt, arg in opts:
if opt in ("-f"):
canfd = 1
elif opt in ("--bitrate"):
bitrate = arg
elif opt in ("--dbitrate"):
dbitrate = arg
elif opt in ("--channel"):
channel = arg
elif opt in ("--bustype"):
bustype = arg
print("caninterface [%s] canfd[%d] bitrate[%d] dbitrate[%d] bustype[%s] channel[%s]"
%(canintf, canfd, bitrate, dbitrate, bustype, channel))
# can interface up
if canfd:
os.system("sudo ip link set %s up type can bitrate %d dbitrate %d fd on" %(canintf, bitrate, dbitrate))
#cmd = "sudo ip link set " + canintf + " up type can bitrate " + bitrate + " dbitrate " + dbitrate + " fd on";
else:
os.system("sudo ip link set %s up type can bitrate %d fd on" %(canintf, bitrate))
#cmd = "sudo ip link set " + canintf + " up type can bitrate " + bitrate + " on";
# bus = can.interface.Bus(bustype='bmcan', channel=0, bitrate=500000, data_bitrate=2000000, tres=True)
# bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=250000)
# bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=250000)
# bus = can.interface.Bus(bustype='ixxat', channel=0, bitrate=250000)
# bus = can.interface.Bus(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000)
canbus = can.interface.Bus(bustype=bustype, channel=channel)
udsidscan(canbus)
canbus.shutdown()
if __name__ == "__main__":
main(sys.argv)