周立功USBCANFD_200U的Python调用

参考前篇 周立功USBCAN-II的Python调用

周立功USBCANFD_200U的Python调用_第1张图片

当普通CAN用, 500K, 程序参考如下(未验证, 如需CANFD Fast传输, 把发送函数的BRS参数提出来, 采样点设置暂无)

from zlgcan import *
import time
import platform

zcanlib = ZCAN() 

def open_usbcan2():
    device_handle = zcanlib.OpenDevice(ZCAN_USBCANFD_200U, 0,0)
    if device_handle == INVALID_DEVICE_HANDLE:
        print("Open Device failed!")
        exit(0)
    print("device handle:%d." %(device_handle))
    # info = zcanlib.GetDeviceInf(device_handle)
    # print("Device Information:\n%s" %(info))
    return device_handle

def open_channel(device_handle, channel):
    chn_init_cfg = ZCAN_CHANNEL_INIT_CONFIG()
    chn_init_cfg.can_type = ZCAN_TYPE_CANFD
    chn_init_cfg.config.canfd.mode = 0
    # From dev_info.json
    chn_init_cfg.config.canfd.abit_timing = 104286	# 500K
    chn_init_cfg.config.canfd.dbit_timing = 4260362	# 2M
    chn_handle = zcanlib.InitCAN(device_handle, channel, chn_init_cfg)
    if chn_handle is None:
        return None
    zcanlib.StartCAN(chn_handle)
    return chn_handle

def transmit_can(chn_handle, stdorext, id, data, len):
    transmit_num = 1
    msgs = (ZCAN_TransmitFD_Data * transmit_num)()
    for i in range(transmit_num):
        msgs[i].transmit_type = 0 #Send Self
        msgs[i].frame.eff     = 0
        if stdorext:
            msgs[i].frame.eff = 1 #extern frame
        msgs[i].frame.rtr     = 0 #remote frame
        msgs[i].frame.can_id  = id
		msgs[i].frame.brs     = 0 # 1: fast, for examplt: 2M
		msgs[i].frame.len     = len
        for j in range(len):
            msgs[i].frame.data[j] = data[j]
    ret = zcanlib.TransmitFD(chn_handle, msgs, transmit_num)

def receive_can(chn_handle):
    rcv_num = zcanlib.GetReceiveNum(chn_handle, ZCAN_TYPE_CANFD)
    if rcv_num:
        print("Receive CAN message number:%d" % rcv_num)
        rcv_msg, rcv_num = zcanlib.ReceiveFD(chn_handle, rcv_num)
        for i in range(rcv_num):
            print("[%d]:ts:%d, id:0x%x, dlc:%d, eff:%d, rtr:%d, data:%s" %(i, rcv_msg[i].timestamp, 
                  rcv_msg[i].frame.can_id, rcv_msg[i].frame.can_dlc, 
                  rcv_msg[i].frame.eff, rcv_msg[i].frame.rtr,
                  ''.join(hex(rcv_msg[i].frame.data[j])[2:] + ' ' for j in range(rcv_msg[i].frame.can_dlc))))

if __name__ == "__main__":

    # dll support
    if platform.python_version()>='3.8.0':
        import os
        os.add_dll_directory(os.getcwd())

    # open device and channel 0
    dev_handle = open_usbcan2()
    chn_handle = open_channel(dev_handle, 0)
    chn1_handle = open_channel(dev_handle, 1)
    print("channel 0 handle:%d." %(chn_handle))
    print("channel 1 handle:%d." %(chn_handle))

    # send can message
    data = [0,1,2,3,4,5,6,0xFF]
    for i in range(2):
        transmit_can(chn_handle, 0, 0x100, data, 6)
        transmit_can(chn1_handle, 0, 0x101, data, 7)
        transmit_can(chn_handle, 1, 0x12345678, data, 8)
        transmit_can(chn1_handle, 1, 0x12345679, data, 8)
        data[0] = data[0] + 1
        time.sleep(0.1)

    # receive can message
    zcanlib.ClearBuffer(chn_handle)
    time.sleep(3)
    receive_can(chn_handle)
    receive_can(chn1_handle)

    #Close Channel
    zcanlib.ResetCAN(chn_handle)
    zcanlib.ResetCAN(chn1_handle)
    #Close Device
    zcanlib.CloseDevice(dev_handle)
    print("Finished")

欢迎扫描二维码关注本人微信公众号, 及时获取最新文章:
在这里插入图片描述

你可能感兴趣的:(自动驾驶,python,周立功,usbcanfd_200u,500K,2M,canfd)