Python基于PCANBasic二次开发使用方法

疯狂的机器人 2021-10-26 11:45:07
Python 开发 二次 二次开发 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:\Users\Admin\Desktop"
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]

 

 
版权声明
本文为[疯狂的机器人]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/airb/p/15464907.html

  1. L'implémentation Python lit simplement le contenu d'Excel et signale les erreurs
  2. 用Python定义一个函数,接收n个数字,求这些参数数字的和
  3. Définissez une fonction en python, recevez n nombres et additionnez ces nombres de paramètres
  4. 上电Python写文件后,再断电后导致文件内容丢失
  5. 上電Python寫文件後,再斷電後導致文件內容丟失
  6. Une fois que Python est allumé pour écrire des fichiers, le contenu des fichiers est perdu après une panne de courant
  7. python套接字编程报错:ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
  8. 【Python从入门到精通】(二)怎么运行Python呢?有哪些好的开发工具(PyCharm)
  9. 【Python从入门到精通】(二)怎么运行Python呢?有哪些好的开发工具(PyCharm)
  10. Python语法1
  11. 2018年度最受推荐的10本Python书籍(初学者必看)
  12. Les 10 livres Python les plus recommandés en 2018 (obligatoire pour les débutants)
  13. Syntaxe Python 1
  14. Python语法1
  15. 在python 运行celery时候 AttributeError: 'NoneType' object has no attribute 'Redis'错误
  16. Attributeerror: 'nonetype' Object has no attribute 'redis' Error when Celery is running in Python
  17. Syntaxe Python 1
  18. Python celery is a plug-in that focuses on distributed asynchronous task processing and task scheduling!
  19. Python celery is a plug-in that focuses on distributed asynchronous task processing and task scheduling!
  20. 在python,使用scrapy爬虫框架
  21. It's time for everyone to see your blog written in Django (including deployment tutorial video)
  22. Python扩展速记符 要求用for循环,if语句
  23. Python擴展速記符 要求用for循環,if語句
  24. Le sténographe d'extension Python nécessite une boucle pour, si instruction
  25. Python+人工智能就业班v5.0wumi
  26. python编程技术的题目,希望能得到解答
  27. Pandas determines the header row dynamically
  28. 关于#python#的问题:模拟登陆后能获取到cookie,就是cookie一天就过期
  29. pandas为dataframe添加新的数据行(rows)、在dataframe后面纵向添加一行数据(数据为列表list形式)、列有不匹配将会使用NA值进行填补
  30. pandas使用组合条件筛选、过滤数据行
  31. 热烈祝贺1024,求解Python3.10闪退问题
  32. Python基础题练习题库有没呢?
  33. python构建神经网络,正向和反向传播
  34. python爬虫输入数字翻页才成功,用变量代替不成功,为什么?
  35. 【Python 爬虫】 2、HTTP基本原理
  36. 【Python 爬虫】 1、爬虫基础概念
  37. Python中如何用find函数计数?
  38. 一文搞懂Python装饰器
  39. python数据结构之递归
  40. 关于#python#的问题:为什么这个open函数会报错
  41. Python:多输入数字求和(Python 程序控制结构)
  42. python忽略警告
  43. Python多维数组问题(编写程序统计成绩)
  44. 一步一步展示并总结Python的异常【建议收藏】
  45. Python中奇葩的round函数!
  46. 总结一下Python的模块加载解析
  47. 保姆级指导给Python库创建桌面快捷方式【赶紧收藏】
  48. 多图速成Python基础语法下篇【万字建议收藏】
  49. Python 命令行工具辅助getopt使用解析!
  50. 【python种子项目ppc】保姆级别指导给项目添加测试
  51. 【python种子项目ppc】一行代码生成项目与开发详细指导
  52. 保姆级教程带你开发优质的Python库之下篇【种子项目】
  53. 保姆级教程带你开发优质的Python库之中篇【命令行发行】
  54. 保姆级教程带你开发优质的Python库之上篇【建议收藏】
  55. python列表自动计算总分程序
  56. 某企业职工的月薪问题python 求解 不知道自己哪里写错了
  57. Python中外部函数对class类中的属性的调用
  58. 朋友股票亏惨了,我一怒用Python爬取了证券最新数据
  59. 习题8和9怎么写(Python基础)
  60. python数据分析,求任务4,5