|
周立功官网下载函数库和例程1、下载函数库点击下载上面那个(新一点,支持的型号也更多)下载解压后,把zlgcan.dll复制到python工程的目录下,和自己的代码文件同级就行了2、下载例程找到zlgcan.py文件,里面包括了can操作的基本函数主要的函数用pycharm打开zlgcan.py,浅浅阅读之后,发现重要的部分基本在ZCAN类里可以看到can设备的开启、can通信的开启,还有发送接收数据都在这里ZCAN的初始化里,调用了ddl库(也就是第一步下载的那个),这里建议把路径改为相对,不然可能报找不到文件的错classZCAN(object):def__init__(self):ifplatform.system()=="Windows":self.__dll=windll.LoadLibrary("E:/ITIProjects/VibrationMonitoring/VMProject/zlgcan.dll")else:print("Nosupportnow!")ifself.__dll==None:print("DLLcouldn'tbeloaded!")12345678'运行运行实践1、can参数初始化官方例程提供了一个can_start函数,在这里面可以修改can的相关配置defcan_start(zcanlib,device_handle,chn):ip=zcanlib.GetIProperty(device_handle)ret=zcanlib.SetValue(ip,str(chn)+"/clock","60000000")#时钟ifret!=ZCAN_STATUS_OK:print("SetCH%dCANFDclockfailed!"%(chn))ret=zcanlib.SetValue(ip,str(chn)+"/canfd_standard","0")#协议?这个置0/1都行ifret!=ZCAN_STATUS_OK:print("SetCH%dCANFDstandardfailed!"%(chn))ret=zcanlib.SetValue(ip,str(chn)+"/initenal_resistance","1")#这个我猜是终端电阻ifret!=ZCAN_STATUS_OK:print("OpenCH%dresistancefailed!"%(chn))ret=zcanlib.SetValue(ip,str(chn)+"/canfd_abit_baud_rate","500000")#应该是通道0波特率ifret!=ZCAN_STATUS_OK:print("SetCH%dCANFDcanfd_abit_baud_ratefailed!"%(chn))ret=zcanlib.SetValue(ip,str(chn)+"/canfd_dbit_baud_rate","2000000")#通道1波特率ifret!=ZCAN_STATUS_OK:print("SetCH%dCANFDcanfd_dbit_baud_ratefailed!"%(chn))zcanlib.ReleaseIProperty(ip)chn_init_cfg=ZCAN_CHANNEL_INIT_CONFIG()chn_init_cfg.can_type=ZCAN_TYPE_CANFD#chn_init_cfg.config.canfd.abit_timing=0x0001975e#1Mbps#chn_init_cfg.config.canfd.dbit_timing=101166#1Mbpschn_init_cfg.config.canfd.mode=0chn_handle=zcanlib.InitCAN(device_handle,chn,chn_init_cfg)ifchn_handleisNone:returnNonezcanlib.StartCAN(chn_handle)#开启returnchn_handle1234567891011121314151617181920212223242526272829'运行运行2、打开can设备和通道zcanlib=ZCAN()#创建can实例handle=zcanlib.OpenDevice(ZCAN_USBCANFD_200U,0,0)#打开can设备,这里第一个参数是设备型号,后面2个填0就行chn_handle=can_start(zcanlib,handle,0)1233、can接收whileTrue:rcv_num=zcanlib.GetReceiveNum(chn_handle,ZCAN_TYPE_CAN)#接收can信号rcv_canfd_num=zcanlib.GetReceiveNum(chn_handle,ZCAN_TYPE_CANFD)#接收fdcan信号ifrcv_num:#接收到了can信号print("ReceiveCANmessagenumber:%d"%rcv_num)rcv_msg,rcv_num=zcanlib.Receive(chn_handle,rcv_num)foriinrange(rcv_num):#下面的几个参数是can报文中的参数,不多解释print("[%d]:ts:%d,id:%d,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])forjinrange(rcv_msg[i].frame.can_dlc))))#hex是查看16进制的报文elifrcv_canfd_num:#接收到了fdcan信号print("ReceiveCANFDmessagenumber:%d"%rcv_canfd_num)rcv_canfd_msgs,rcv_canfd_num=zcanlib.ReceiveFD(chn_handle,rcv_canfd_num,1000)foriinrange(rcv_canfd_num):print("[%d]:ts:%d,id:%d,len:%d,eff:%d,rtr:%d,esi:%d,brs:%d,data:%s"%(i,rcv_canfd_msgs[i].timestamp,rcv_canfd_msgs[i].frame.can_id,rcv_canfd_msgs[i].frame.len,rcv_canfd_msgs[i].frame.eff,rcv_canfd_msgs[i].frame.rtr,rcv_canfd_msgs[i].frame.esi,rcv_canfd_msgs[i].frame.brs,''.join(hex(rcv_canfd_msgs[i].frame.data[j])forjinrange(rcv_canfd_msgs[i].frame.len))))else:print("nomsg")time.sleep(0.1)1234567891011121314151617181920212223接收函数就是这么个流程,按需修改即可4、can发送transmit_num=10#发送can报文数量msgs=(ZCAN_Transmit_Data*transmit_num)()#ZCAN_Transmit_Data是定义在前面的一个can报文结构体foriinrange(transmit_num):msgs[i].transmit_type=0#SendSelf#这个不知道是啥,其他参数都很简单msgs[i].frame.eff=0#externframemsgs[i].frame.rtr=0#remoteframemsgs[i].frame.can_id=imsgs[i].frame.can_dlc=8forjinrange(msgs[i].frame.can_dlc):msgs[i].frame.data[j]=j#这里定义数据内容ret=zcanlib.Transmit(chn_handle,msgs,transmit_num)#发送出去print("TranmitNum:%d."%ret)123456789101112
|
|