Python3基于PCANBasic二次开发库使用方法
一、环境搭建
1.概述
PCAN-Basic 是 PCAN-Light 的后继者,是一个简单的 PCAN 系统编程接口。 通过 PCAN-Basic Dll,可以将自己的应用程序连接到设备驱动程序和 PCAN 硬件,以与 CAN 总线进行通信。支持C、C++、Delphi、JAVA、VB、Python等语言。
2.PCAN-Basic库和驱动下载地址
https://www.peak-system.com/fileadmin/media/files/pcan-basic.zip
3.Python安装
下载地址:https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe
二、PCANBasic函数方法使用
1.Initialize
初始化一个代表非即插即用 PCAN 设备的 PCAN 通道。
# The Not Plug & Play Channel (PCAN-DNG) is initialized # objPCAN = PCANBasic() result = objPCAN.Initialize(PCAN_DNGBUS1, PCAN_BAUD_500K, PCAN_TYPE_DNG_SJA, 0x378, 7) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "PCAN-DNG (Ch-1) was initialized" # All initialized channels are released # objPCAN.Uninitialize(PCAN_NONEBUS)
2.InitializeFD
初始化支持 FD 的 PCAN 通道。
# Defines a FD Bit rate string with nominal and data Bit rate set to 1 MB # bitrate = "f_clock_mhz=24, nom_brp=1, nom_tseg1=17, nom_tseg2=6, nom_sjw=1, data_brp=1, data_tseg1=16, data_tseg2=7, data_sjw=1" # The FD capable Channel (PCAN-USB Pro FD) is initialized # objPCAN = PCANBasic() result = objPCAN.InitializeFD(PCAN_USBBUS1, bitrate) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "PCAN-USB Pro FD (Ch-1) was initialized" # All initialized channels are released # objPCAN.Uninitialize(PCAN_NONEBUS)
3.Uninitialize
取消初始化 PCAN 通道。
# The Plug & Play Channel (PCAN-PCI) is initialized # objPCAN = PCANBasic() result = objPCAN.Initialize(PCAN_PCIBUS1, PCAN_BAUD_500K) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "PCAN-PCI (Ch-1) was initialized" .... # The PCI Channel is released # result = objPCAN.Uninitialize(PCAN_PCIBUS1) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "PCAN-PCI (Ch-1) was released"
4.Reset
重置 PCAN 通道的接收和发送队列。
# The PCI Channel is released # result = objPCAN.Reset(PCAN_PCIBUS1) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "PCAN-PCI (Ch-1) was reset"
5.GetStatus
获取 当前PCAN 通道的 BUS 状态。
# Check the status of the PCI Channel # result = objPCAN.GetStatus(PCAN_PCIBUS1) if result == PCAN_ERROR_BUSLIGHT: print "PCAN-PCI (Ch-1): Handling a BUS-LIGHT status..." elif result == PCAN_ERROR_BUSHEAVY: print "PCAN-PCI (Ch-1): Handling a BUS-HEAVY status..." elif result == PCAN_ERROR_BUSOFF: print "PCAN-PCI (Ch-1): Handling a BUS-OFF status..." elif result == PCAN_ERROR_OK: print "PCAN-PCI (Ch-1): Status is OK" else: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1]
6.Read
从 PCAN 通道的接收队列中读取 CAN 消息及其时间戳。
readResult = PCAN_ERROR_OK, while (readResult[0] & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY: # Check the receive queue for new messages # readResult = objPCAN.Read(PCAN_USBBUS1) if readResult[0] != PCAN_ERROR_QRCVEMPTY: # Process the received message # print "A message was received" ProcessMessage(result[1],result[2]) # Possible processing function, ProcessMessage(msg,timestamp) else: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(readResult[0]) print result[1] HandleReadError(readResult[0]) # Possible errors handling function, HandleError(function_result)
7.ReadFD
从支持 FD 的 PCAN 通道的接收队列中读取 CAN 消息及其时间戳。
readResult = PCAN_ERROR_OK, while (readResult[0] & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY: # Check the receive queue for new messages # readResult = objPCAN.ReadFD(PCAN_USBBUS1) if readResult[0] != PCAN_ERROR_QRCVEMPTY: # Process the received message # print "A message was received" ProcessMessage(result[1],result[2]) # Possible processing function, ProcessMessage(msg,timestamp) else: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(readResult[0]) print result[1] HandleReadError(readResult[0]) # Possible errors handling function, HandleError(function_result)
8.Write
发送 CAN 消息。
# A CAN message is configured # msg = TPCANMsg() msg.ID = 0x100 msg.MSGTYPE = PCAN_MESSAGE_STANDARD msg.LEN = 3 msg.DATA[0] = 1 msg.DATA[1] = 2 msg.DATA[2] = 3 # The message is sent using the PCAN-USB Channel 1 # result = objPCAN.Write(PCAN_USBBUS1,msg) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result else: print "Message sent successfully"
9.WriteFD
使用支持 FD 的 PCAN 通道传输 CAN 消息。
# A CAN message is configured # msg = TPCANMsgFD() msg.ID = 0x100 msg.MSGTYPE = PCAN_MESSAGE_STANDARD.value | PCAN_MESSAGE_FD.value # DLC 9 means 12 data bytes # msg.DLC = 9 for i in range(12): msg.DATA[i] = i # The message is sent using the PCAN-USB Channel 1 # result = objPCAN.WriteFD(PCAN_USBBUS1,msg) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result else: print "Message sent successfully"
10.GetValue
从 PCAN 通道检索信息。
# The status of the message filter of the PCAN-USB Channel 1 is asked # result = objPCAN.GetValue(PCAN_USBBUS1,PCAN_MESSAGE_FILTER) if result[0] != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result else: # A text is shown giving information about the current status of the filter # if result[1] == PCAN_FILTER_OPEN: print "The message filter for the PCAN-USB, channel 1, is completely opened." elif result[1] == PCAN_FILTER_CLOSE: print "The message filter for the PCAN-USB, channel 1, is closed." elif result[1] == PCAN_FILTER_CUSTOM: print "The message filter for the PCAN-USB, channel 1, is custom configured."
11.SetValue
设置 PCAN 通道内的配置或信息值。
# The path for the Log file is set. # Note that this parameter is set using the # default Channel (PCAN_NONEBUS) # strBuffer = "C:UsersAdminDesktop" result = objPCAN.SetValue(PCAN_NONEBUS,PCAN_LOG_LOCATION,strBuffer) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "Log path was successfully set"
12.FilterMessages
配置接收过滤器。
# The message filter is closed first to ensure the reception of the new range of IDs. # result = objPCAN.SetValue(PCAN_USBBUS1,PCAN_MESSAGE_FILTER,PCAN_FILTER_CLOSE) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: # The message filter is configured to receive the IDs 2,3,4 and 5 on the PCAN-USB, Channel 1 # result = objPCAN.FilterMessages(PCAN_USBBUS1,2,5,PCAN_MODE_STANDARD) if result != PCAN_ERROR_OK: # An error occurred, get a text describing the error and show it # result = objPCAN.GetErrorText(result) print result[1] else: print "Filter successfully configured for IDs 2,3,4 and 5"
12.GetErrorText
获取错误代码的信息。
# Gets the description text for PCAN_ERROR_INITIALIZE using the language ID for Spanish # objPCAN = PCANBasic() result = objPCAN.GetErrorText(PCAN_ERROR_INITIALIZE, 10) if result[0] != PCAN_ERROR_OK: # An error occurred, show a message indicating it # print "Error when recovering Error-Code's description" else: print result[1]