找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 13374|回复: 13
打印 上一主题 下一主题
收起左侧

简单的Python程序实现PCAN自动发送CAN报文

  [复制链接]
跳转到指定楼层
楼主
ID:531389 发表于 2019-5-9 11:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
简单的Python程序(基于Python3.7)实现PCAN自动发送CAN报文。。。。

程序定义了每隔10ms、100ms、1000ms各发送若干帧数据。
修改main.py文件中的msg.ID、msg.MSGTYPE、msg.DATA可以修改帧ID、帧类型和数据。

Python源程序如下:
  1. #  PCANBasic.py
  2. #
  3. #  ~~~~~~~~~~~~
  4. #
  5. #  PCAN-Basic API
  6. #
  7. #  ~~~~~~~~~~~~
  8. #
  9. #  ------------------------------------------------------------------
  10. #  Author : Keneth Wagner
  11. #
  12. #  Language: Python 2.7
  13. #  ------------------------------------------------------------------

  14. # Module Imports
  15. #
  16. from ctypes import *
  17. import platform

  18. # ///////////////////////////////////////////////////////////
  19. # Type definitions
  20. # ///////////////////////////////////////////////////////////

  21. TPCANHandle = c_ushort  # Represents a PCAN hardware channel handle
  22. TPCANStatus = int  # Represents a PCAN status/error code
  23. TPCANParameter = c_ubyte  # Represents a PCAN parameter to be read or set
  24. TPCANDevice = c_ubyte  # Represents a PCAN device
  25. TPCANMessageType = c_ubyte  # Represents the type of a PCAN message
  26. TPCANType = c_ubyte  # Represents the type of PCAN hardware to be initialized
  27. TPCANMode = c_ubyte  # Represents a PCAN filter mode
  28. TPCANBaudrate = c_ushort  # Represents a PCAN Baud rate register value
  29. TPCANBitrateFD = c_char_p  # Represents a PCAN-FD bit rate string
  30. TPCANTimestampFD = c_ulonglong  # Represents a timestamp of a received PCAN FD message

  31. # ///////////////////////////////////////////////////////////
  32. # Value definitions
  33. # ///////////////////////////////////////////////////////////

  34. # Currently defined and supported PCAN channels
  35. #
  36. PCAN_NONEBUS = TPCANHandle(0x00)  # Undefined/default value for a PCAN bus

  37. PCAN_ISABUS1 = TPCANHandle(0x21)  # PCAN-ISA interface, channel 1
  38. PCAN_ISABUS2 = TPCANHandle(0x22)  # PCAN-ISA interface, channel 2
  39. PCAN_ISABUS3 = TPCANHandle(0x23)  # PCAN-ISA interface, channel 3
  40. PCAN_ISABUS4 = TPCANHandle(0x24)  # PCAN-ISA interface, channel 4
  41. PCAN_ISABUS5 = TPCANHandle(0x25)  # PCAN-ISA interface, channel 5
  42. PCAN_ISABUS6 = TPCANHandle(0x26)  # PCAN-ISA interface, channel 6
  43. PCAN_ISABUS7 = TPCANHandle(0x27)  # PCAN-ISA interface, channel 7
  44. PCAN_ISABUS8 = TPCANHandle(0x28)  # PCAN-ISA interface, channel 8

  45. PCAN_DNGBUS1 = TPCANHandle(0x31)  # PCAN-Dongle/LPT interface, channel 1

  46. PCAN_PCIBUS1 = TPCANHandle(0x41)  # PCAN-PCI interface, channel 1
  47. PCAN_PCIBUS2 = TPCANHandle(0x42)  # PCAN-PCI interface, channel 2
  48. PCAN_PCIBUS3 = TPCANHandle(0x43)  # PCAN-PCI interface, channel 3
  49. PCAN_PCIBUS4 = TPCANHandle(0x44)  # PCAN-PCI interface, channel 4
  50. PCAN_PCIBUS5 = TPCANHandle(0x45)  # PCAN-PCI interface, channel 5
  51. PCAN_PCIBUS6 = TPCANHandle(0x46)  # PCAN-PCI interface, channel 6
  52. PCAN_PCIBUS7 = TPCANHandle(0x47)  # PCAN-PCI interface, channel 7
  53. PCAN_PCIBUS8 = TPCANHandle(0x48)  # PCAN-PCI interface, channel 8
  54. PCAN_PCIBUS9 = TPCANHandle(0x409)  # PCAN-PCI interface, channel 9
  55. PCAN_PCIBUS10 = TPCANHandle(0x40A)  # PCAN-PCI interface, channel 10
  56. PCAN_PCIBUS11 = TPCANHandle(0x40B)  # PCAN-PCI interface, channel 11
  57. PCAN_PCIBUS12 = TPCANHandle(0x40C)  # PCAN-PCI interface, channel 12
  58. PCAN_PCIBUS13 = TPCANHandle(0x40D)  # PCAN-PCI interface, channel 13
  59. PCAN_PCIBUS14 = TPCANHandle(0x40E)  # PCAN-PCI interface, channel 14
  60. PCAN_PCIBUS15 = TPCANHandle(0x40F)  # PCAN-PCI interface, channel 15
  61. PCAN_PCIBUS16 = TPCANHandle(0x410)  # PCAN-PCI interface, channel 16

  62. PCAN_USBBUS1 = TPCANHandle(0x51)  # PCAN-USB interface, channel 1
  63. PCAN_USBBUS2 = TPCANHandle(0x52)  # PCAN-USB interface, channel 2
  64. PCAN_USBBUS3 = TPCANHandle(0x53)  # PCAN-USB interface, channel 3
  65. PCAN_USBBUS4 = TPCANHandle(0x54)  # PCAN-USB interface, channel 4
  66. PCAN_USBBUS5 = TPCANHandle(0x55)  # PCAN-USB interface, channel 5
  67. PCAN_USBBUS6 = TPCANHandle(0x56)  # PCAN-USB interface, channel 6
  68. PCAN_USBBUS7 = TPCANHandle(0x57)  # PCAN-USB interface, channel 7
  69. PCAN_USBBUS8 = TPCANHandle(0x58)  # PCAN-USB interface, channel 8
  70. PCAN_USBBUS9 = TPCANHandle(0x509)  # PCAN-USB interface, channel 9
  71. PCAN_USBBUS10 = TPCANHandle(0x50A)  # PCAN-USB interface, channel 10
  72. PCAN_USBBUS11 = TPCANHandle(0x50B)  # PCAN-USB interface, channel 11
  73. PCAN_USBBUS12 = TPCANHandle(0x50C)  # PCAN-USB interface, channel 12
  74. PCAN_USBBUS13 = TPCANHandle(0x50D)  # PCAN-USB interface, channel 13
  75. PCAN_USBBUS14 = TPCANHandle(0x50E)  # PCAN-USB interface, channel 14
  76. PCAN_USBBUS15 = TPCANHandle(0x50F)  # PCAN-USB interface, channel 15
  77. PCAN_USBBUS16 = TPCANHandle(0x510)  # PCAN-USB interface, channel 16

  78. PCAN_PCCBUS1 = TPCANHandle(0x61)  # PCAN-PC Card interface, channel 1
  79. PCAN_PCCBUS2 = TPCANHandle(0x62)  # PCAN-PC Card interface, channel 2

  80. PCAN_LANBUS1 = TPCANHandle(0x801)  # PCAN-LAN interface, channel 1
  81. PCAN_LANBUS2 = TPCANHandle(0x802)  # PCAN-LAN interface, channel 2
  82. PCAN_LANBUS3 = TPCANHandle(0x803)  # PCAN-LAN interface, channel 3
  83. PCAN_LANBUS4 = TPCANHandle(0x804)  # PCAN-LAN interface, channel 4
  84. PCAN_LANBUS5 = TPCANHandle(0x805)  # PCAN-LAN interface, channel 5
  85. PCAN_LANBUS6 = TPCANHandle(0x806)  # PCAN-LAN interface, channel 6
  86. PCAN_LANBUS7 = TPCANHandle(0x807)  # PCAN-LAN interface, channel 7
  87. PCAN_LANBUS8 = TPCANHandle(0x808)  # PCAN-LAN interface, channel 8
  88. PCAN_LANBUS9 = TPCANHandle(0x809)  # PCAN-LAN interface, channel 9
  89. PCAN_LANBUS10 = TPCANHandle(0x80A)  # PCAN-LAN interface, channel 10
  90. PCAN_LANBUS11 = TPCANHandle(0x80B)  # PCAN-LAN interface, channel 11
  91. PCAN_LANBUS12 = TPCANHandle(0x80C)  # PCAN-LAN interface, channel 12
  92. PCAN_LANBUS13 = TPCANHandle(0x80D)  # PCAN-LAN interface, channel 13
  93. PCAN_LANBUS14 = TPCANHandle(0x80E)  # PCAN-LAN interface, channel 14
  94. PCAN_LANBUS15 = TPCANHandle(0x80F)  # PCAN-LAN interface, channel 15
  95. PCAN_LANBUS16 = TPCANHandle(0x810)  # PCAN-LAN interface, channel 16

  96. # Represent the PCAN error and status codes
  97. #
  98. PCAN_ERROR_OK = TPCANStatus(0x00000)  # No error
  99. PCAN_ERROR_XMTFULL = TPCANStatus(0x00001)  # Transmit buffer in CAN controller is full
  100. PCAN_ERROR_OVERRUN = TPCANStatus(0x00002)  # CAN controller was read too late
  101. PCAN_ERROR_BUSLIGHT = TPCANStatus(0x00004)  # Bus error: an error counter reached the 'light' limit
  102. PCAN_ERROR_BUSHEAVY = TPCANStatus(0x00008)  # Bus error: an error counter reached the 'heavy' limit
  103. PCAN_ERROR_BUSWARNING = TPCANStatus(PCAN_ERROR_BUSHEAVY)  # Bus error: an error counter reached the 'warning' limit
  104. PCAN_ERROR_BUSPASSIVE = TPCANStatus(0x40000)  # Bus error: the CAN controller is error passive
  105. PCAN_ERROR_BUSOFF = TPCANStatus(0x00010)  # Bus error: the CAN controller is in bus-off state
  106. PCAN_ERROR_ANYBUSERR = TPCANStatus(
  107.     PCAN_ERROR_BUSWARNING | PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF | PCAN_ERROR_BUSPASSIVE)  # Mask for all bus errors
  108. PCAN_ERROR_QRCVEMPTY = TPCANStatus(0x00020)  # Receive queue is empty
  109. PCAN_ERROR_QOVERRUN = TPCANStatus(0x00040)  # Receive queue was read too late
  110. PCAN_ERROR_QXMTFULL = TPCANStatus(0x00080)  # Transmit queue is full
  111. PCAN_ERROR_REGTEST = TPCANStatus(0x00100)  # Test of the CAN controller hardware registers failed (no hardware found)
  112. PCAN_ERROR_NODRIVER = TPCANStatus(0x00200)  # Driver not loaded
  113. PCAN_ERROR_HWINUSE = TPCANStatus(0x00400)  # Hardware already in use by a Net
  114. PCAN_ERROR_NETINUSE = TPCANStatus(0x00800)  # A Client is already connected to the Net
  115. PCAN_ERROR_ILLHW = TPCANStatus(0x01400)  # Hardware handle is invalid
  116. PCAN_ERROR_ILLNET = TPCANStatus(0x01800)  # Net handle is invalid
  117. PCAN_ERROR_ILLCLIENT = TPCANStatus(0x01C00)  # Client handle is invalid
  118. PCAN_ERROR_ILLHANDLE = TPCANStatus(
  119.     PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT)  # Mask for all handle errors
  120. PCAN_ERROR_RESOURCE = TPCANStatus(0x02000)  # Resource (FIFO, Client, timeout) cannot be created
  121. PCAN_ERROR_ILLPARAMTYPE = TPCANStatus(0x04000)  # Invalid parameter
  122. PCAN_ERROR_ILLPARAMVAL = TPCANStatus(0x08000)  # Invalid parameter value
  123. PCAN_ERROR_UNKNOWN = TPCANStatus(0x10000)  # Unknown error
  124. PCAN_ERROR_ILLDATA = TPCANStatus(0x20000)  # Invalid data, function, or action
  125. PCAN_ERROR_CAUTION = TPCANStatus(
  126.     0x2000000)  # An operation was successfully carried out, however, irregularities were registered
  127. PCAN_ERROR_INITIALIZE = TPCANStatus(
  128.     0x4000000)  # Channel is not initialized [Value was changed from 0x40000 to 0x4000000]
  129. PCAN_ERROR_ILLOPERATION = TPCANStatus(0x8000000)  # Invalid operation [Value was changed from 0x80000 to 0x8000000]

  130. # PCAN devices
  131. #
  132. PCAN_NONE = TPCANDevice(0x00)  # Undefined, unknown or not selected PCAN device value
  133. PCAN_PEAKCAN = TPCANDevice(0x01)  # PCAN Non-Plug&Play devices. NOT USED WITHIN PCAN-Basic API
  134. PCAN_ISA = TPCANDevice(0x02)  # PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
  135. PCAN_DNG = TPCANDevice(0x03)  # PCAN-Dongle
  136. PCAN_PCI = TPCANDevice(0x04)  # PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
  137. PCAN_USB = TPCANDevice(0x05)  # PCAN-USB and PCAN-USB Pro
  138. PCAN_PCC = TPCANDevice(0x06)  # PCAN-PC Card
  139. PCAN_VIRTUAL = TPCANDevice(0x07)  # PCAN Virtual hardware. NOT USED WITHIN PCAN-Basic API
  140. PCAN_LAN = TPCANDevice(0x08)  # PCAN Gateway devices

  141. # PCAN parameters
  142. #
  143. PCAN_DEVICE_NUMBER = TPCANParameter(0x01)  # PCAN-USB device number parameter
  144. PCAN_5VOLTS_POWER = TPCANParameter(0x02)  # PCAN-PC Card 5-Volt power parameter
  145. PCAN_RECEIVE_EVENT = TPCANParameter(0x03)  # PCAN receive event handler parameter
  146. PCAN_MESSAGE_FILTER = TPCANParameter(0x04)  # PCAN message filter parameter
  147. PCAN_API_VERSION = TPCANParameter(0x05)  # PCAN-Basic API version parameter
  148. PCAN_CHANNEL_VERSION = TPCANParameter(0x06)  # PCAN device channel version parameter
  149. PCAN_BUSOFF_AUTORESET = TPCANParameter(0x07)  # PCAN Reset-On-Busoff parameter
  150. PCAN_LISTEN_ONLY = TPCANParameter(0x08)  # PCAN Listen-Only parameter
  151. PCAN_LOG_LOCATION = TPCANParameter(0x09)  # Directory path for log files
  152. PCAN_LOG_STATUS = TPCANParameter(0x0A)  # Debug-Log activation status
  153. PCAN_LOG_CONFIGURE = TPCANParameter(0x0B)  # Configuration of the debugged information (LOG_FUNCTION_***)
  154. PCAN_LOG_TEXT = TPCANParameter(0x0C)  # Custom insertion of text into the log file
  155. PCAN_CHANNEL_CONDITION = TPCANParameter(0x0D)  # Availability status of a PCAN-Channel
  156. PCAN_HARDWARE_NAME = TPCANParameter(0x0E)  # PCAN hardware name parameter
  157. PCAN_RECEIVE_STATUS = TPCANParameter(0x0F)  # Message reception status of a PCAN-Channel
  158. PCAN_CONTROLLER_NUMBER = TPCANParameter(0x10)  # CAN-Controller number of a PCAN-Channel
  159. PCAN_TRACE_LOCATION = TPCANParameter(0x11)  # Directory path for PCAN trace files
  160. PCAN_TRACE_STATUS = TPCANParameter(0x12)  # CAN tracing activation status
  161. PCAN_TRACE_SIZE = TPCANParameter(0x13)  # Configuration of the maximum file size of a CAN trace
  162. PCAN_TRACE_CONFIGURE = TPCANParameter(0x14)  # Configuration of the trace file storing mode (TRACE_FILE_***)
  163. PCAN_CHANNEL_IDENTIFYING = TPCANParameter(
  164.     0x15)  # Physical identification of a USB based PCAN-Channel by blinking its associated LED
  165. PCAN_CHANNEL_FEATURES = TPCANParameter(0x16)  # Capabilities of a PCAN device (FEATURE_***)
  166. PCAN_BITRATE_ADAPTING = TPCANParameter(0x17)  # Using of an existing bit rate (PCAN-View connected to a channel)
  167. PCAN_BITRATE_INFO = TPCANParameter(0x18)  # Configured bit rate as Btr0Btr1 value
  168. PCAN_BITRATE_INFO_FD = TPCANParameter(0x19)  # Configured bit rate as TPCANBitrateFD string
  169. PCAN_BUSSPEED_NOMINAL = TPCANParameter(0x1A)  # Configured nominal CAN Bus speed as Bits per seconds
  170. PCAN_BUSSPEED_DATA = TPCANParameter(0x1B)  # Configured CAN data speed as Bits per seconds
  171. PCAN_IP_ADDRESS = TPCANParameter(0x1C)  # Remote address of a LAN channel as string in IPv4 format
  172. PCAN_LAN_SERVICE_STATUS = TPCANParameter(0x1D)  # Status of the Virtual PCAN-Gateway Service

  173. # PCAN parameter values
  174. #
  175. PCAN_PARAMETER_OFF = int(0x00)  # The PCAN parameter is not set (inactive)
  176. PCAN_PARAMETER_ON = int(0x01)  # The PCAN parameter is set (active)
  177. PCAN_FILTER_CLOSE = int(0x00)  # The PCAN filter is closed. No messages will be received
  178. PCAN_FILTER_OPEN = int(0x01)  # The PCAN filter is fully opened. All messages will be received
  179. PCAN_FILTER_CUSTOM = int(0x02)  # The PCAN filter is custom configured. Only registered messages will be received
  180. PCAN_CHANNEL_UNAVAILABLE = int(0x00)  # The PCAN-Channel handle is illegal, or its associated hardware is not available
  181. PCAN_CHANNEL_AVAILABLE = int(
  182.     0x01)  # The PCAN-Channel handle is available to be connected (Plug&Play Hardware: it means furthermore that the hardware is plugged-in)
  183. PCAN_CHANNEL_OCCUPIED = int(0x02)  # The PCAN-Channel handle is valid, and is already being used
  184. PCAN_CHANNEL_PCANVIEW = PCAN_CHANNEL_AVAILABLE | PCAN_CHANNEL_OCCUPIED  # The PCAN-Channel handle is already being used by a PCAN-View application, but is available to connect

  185. LOG_FUNCTION_DEFAULT = int(0x00)  # Logs system exceptions / errors
  186. LOG_FUNCTION_ENTRY = int(0x01)  # Logs the entries to the PCAN-Basic API functions
  187. LOG_FUNCTION_PARAMETERS = int(0x02)  # Logs the parameters passed to the PCAN-Basic API functions
  188. LOG_FUNCTION_LEAVE = int(0x04)  # Logs the exits from the PCAN-Basic API functions
  189. LOG_FUNCTION_WRITE = int(0x08)  # Logs the CAN messages passed to the CAN_Write function
  190. LOG_FUNCTION_READ = int(0x10)  # Logs the CAN messages received within the CAN_Read function
  191. LOG_FUNCTION_ALL = int(0xFFFF)  # Logs all possible information within the PCAN-Basic API functions

  192. TRACE_FILE_SINGLE = int(0x00)  # A single file is written until it size reaches PAN_TRACE_SIZE
  193. TRACE_FILE_SEGMENTED = int(0x01)  # Traced data is distributed in several files with size PAN_TRACE_SIZE
  194. TRACE_FILE_DATE = int(0x02)  # Includes the date into the name of the trace file
  195. TRACE_FILE_TIME = int(0x04)  # Includes the start time into the name of the trace file
  196. TRACE_FILE_OVERWRITE = int(0x80)  # Causes the overwriting of available traces (same name)

  197. FEATURE_FD_CAPABLE = int(0x01)  # Device supports flexible data-rate (CAN-FD)

  198. SERVICE_STATUS_STOPPED = int(0x01)  # The service is not running
  199. SERVICE_STATUS_RUNNING = int(0x04)  # The service is running

  200. # PCAN message types
  201. #
  202. PCAN_MESSAGE_STANDARD = TPCANMessageType(0x00)  # The PCAN message is a CAN Standard Frame (11-bit identifier)
  203. PCAN_MESSAGE_RTR = TPCANMessageType(0x01)  # The PCAN message is a CAN Remote-Transfer-Request Frame
  204. PCAN_MESSAGE_EXTENDED = TPCANMessageType(0x02)  # The PCAN message is a CAN Extended Frame (29-bit identifier)
  205. PCAN_MESSAGE_FD = TPCANMessageType(0x04)  # The PCAN message represents a FD frame in terms of CiA Specs
  206. PCAN_MESSAGE_BRS = TPCANMessageType(
  207.     0x08)  # The PCAN message represents a FD bit rate switch (CAN data at a higher bit rate)
  208. PCAN_MESSAGE_ESI = TPCANMessageType(
  209.     0x10)  # The PCAN message represents a FD error state indicator(CAN FD transmitter was error active)
  210. PCAN_MESSAGE_STATUS = TPCANMessageType(0x80)  # The PCAN message represents a PCAN status message

  211. # Frame Type / Initialization Mode
  212. #
  213. PCAN_MODE_STANDARD = PCAN_MESSAGE_STANDARD
  214. PCAN_MODE_EXTENDED = PCAN_MESSAGE_EXTENDED

  215. # Baud rate codes = BTR0/BTR1 register values for the CAN controller.
  216. # You can define your own Baud rate with the BTROBTR1 register.
  217. # Take a look at www.peak-system.com for our free software "BAUDTOOL"
  218. # to calculate the BTROBTR1 register for every bit rate and sample point.
  219. #
  220. PCAN_BAUD_1M = TPCANBaudrate(0x0014)  # 1 MBit/s
  221. PCAN_BAUD_800K = TPCANBaudrate(0x0016)  # 800 kBit/s
  222. PCAN_BAUD_500K = TPCANBaudrate(0x001C)  # 500 kBit/s
  223. PCAN_BAUD_250K = TPCANBaudrate(0x011C)  # 250 kBit/s
  224. PCAN_BAUD_125K = TPCANBaudrate(0x031C)  # 125 kBit/s
  225. PCAN_BAUD_100K = TPCANBaudrate(0x432F)  # 100 kBit/s
  226. PCAN_BAUD_95K = TPCANBaudrate(0xC34E)  # 95,238 kBit/s
  227. PCAN_BAUD_83K = TPCANBaudrate(0x852B)  # 83,333 kBit/s
  228. PCAN_BAUD_50K = TPCANBaudrate(0x472F)  # 50 kBit/s
  229. PCAN_BAUD_47K = TPCANBaudrate(0x1414)  # 47,619 kBit/s
  230. PCAN_BAUD_33K = TPCANBaudrate(0x8B2F)  # 33,333 kBit/s
  231. PCAN_BAUD_20K = TPCANBaudrate(0x532F)  # 20 kBit/s
  232. PCAN_BAUD_10K = TPCANBaudrate(0x672F)  # 10 kBit/s
  233. PCAN_BAUD_5K = TPCANBaudrate(0x7F7F)  # 5 kBit/s

  234. # Represents the configuration for a CAN bit rate
  235. # Note:
  236. #    * Each parameter and its value must be separated with a '='.
  237. #    * Each pair of parameter/value must be separated using ','.
  238. #
  239. # Example:
  240. #    f_clock=80000000,nom_brp=0,nom_tseg1=13,nom_tseg2=0,nom_sjw=0,data_brp=0,data_tseg1=13,data_tseg2=0,data_sjw=0
  241. #
  242. # PCAN_BR_CLOCK = TPCANBitrateFD("f_clock")
  243. # PCAN_BR_CLOCK_MHZ = TPCANBitrateFD("f_clock_mhz")
  244. # PCAN_BR_NOM_BRP = TPCANBitrateFD("nom_brp")
  245. # PCAN_BR_NOM_TSEG1 = TPCANBitrateFD("nom_tseg1")
  246. # PCAN_BR_NOM_TSEG2 = TPCANBitrateFD("nom_tseg2")
  247. # PCAN_BR_NOM_SJW = TPCANBitrateFD("nom_sjw")
  248. # PCAN_BR_NOM_SAMPLE = TPCANBitrateFD("nom_sam")
  249. # PCAN_BR_DATA_BRP = TPCANBitrateFD("data_brp")
  250. # PCAN_BR_DATA_TSEG1 = TPCANBitrateFD("data_tseg1")
  251. # PCAN_BR_DATA_TSEG2 = TPCANBitrateFD("data_tseg2")
  252. # PCAN_BR_DATA_SJW = TPCANBitrateFD("data_sjw")
  253. # PCAN_BR_DATA_SAMPLE = TPCANBitrateFD("data_ssp_offset")

  254. # Supported No-Plug-And-Play Hardware types
  255. #
  256. PCAN_TYPE_ISA = TPCANType(0x01)  # PCAN-ISA 82C200
  257. PCAN_TYPE_ISA_SJA = TPCANType(0x09)  # PCAN-ISA SJA1000
  258. PCAN_TYPE_ISA_PHYTEC = TPCANType(0x04)  # PHYTEC ISA
  259. PCAN_TYPE_DNG = TPCANType(0x02)  # PCAN-Dongle 82C200
  260. PCAN_TYPE_DNG_EPP = TPCANType(0x03)  # PCAN-Dongle EPP 82C200
  261. PCAN_TYPE_DNG_SJA = TPCANType(0x05)  # PCAN-Dongle SJA1000
  262. PCAN_TYPE_DNG_SJA_EPP = TPCANType(0x06)  # PCAN-Dongle EPP SJA1000


  263. # Represents a PCAN message
  264. #
  265. class TPCANMsg(Structure):
  266.     """
  267.     Represents a PCAN message
  268.     """
  269.     _fields_ = [("ID", c_uint),  # 11/29-bit message identifier
  270.                 ("MSGTYPE", TPCANMessageType),  # Type of the message
  271.                 ("LEN", c_ubyte),  # Data Length Code of the message (0..8)
  272.                 ("DATA", c_ubyte * 8)]  # Data of the message (DATA[0]..DATA[7])


  273. # Represents a timestamp of a received PCAN message
  274. # Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
  275. #
  276. class TPCANTimestamp(Structure):
  277.     """
  278.     Represents a timestamp of a received PCAN message
  279.     Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
  280.     """
  281.     _fields_ = [("millis", c_uint),  # Base-value: milliseconds: 0.. 2^32-1
  282.                 ("millis_overflow", c_ushort),  # Roll-arounds of millis
  283.                 ("micros", c_ushort)]  # Microseconds: 0..999


  284. # Represents a PCAN message from a FD capable hardware
  285. #
  286. class TPCANMsgFD(Structure):
  287.     """
  288.     Represents a PCAN message
  289.     """
  290.     _fields_ = [("ID", c_uint),  # 11/29-bit message identifier
  291.                 ("MSGTYPE", TPCANMessageType),  # Type of the message
  292.                 ("DLC", c_ubyte),  # Data Length Code of the message (0..15)
  293.                 ("DATA", c_ubyte * 64)]  # Data of the message (DATA[0]..DATA[63])


  294. # ///////////////////////////////////////////////////////////
  295. # PCAN-Basic API function declarations
  296. # ///////////////////////////////////////////////////////////

  297. # PCAN-Basic API class implementation
  298. #
  299. class PCANBasic:
  300.     """
  301.       PCAN-Basic API class implementation
  302.     """

  303.     def __init__(self):
  304.         # Loads the PCANBasic.dll
  305.         #     
  306.         if platform.system() == 'Windows':
  307.             self.__m_dllBasic = windll.LoadLibrary("PCANBasic")
  308.         else:
  309.             self.__m_dllBasic = cdll.LoadLibrary("libpcanbasic.so")
  310.         if self.__m_dllBasic == None:
  311.             print("Exception: The PCAN-Basic DLL couldn't be loaded!")

  312.     # Initializes a PCAN Channel
  313.     #
  314.     def Initialize(
  315.             self,
  316.             Channel,
  317.             Btr0Btr1,
  318.             HwType=TPCANType(0),
  319.             IOPort=c_uint(0),
  320.             Interrupt=c_ushort(0)):

  321.         """
  322.           Initializes a PCAN Channel

  323.         Parameters:
  324.           Channel  : A TPCANHandle representing a PCAN Channel
  325.           Btr0Btr1 : The speed for the communication (BTR0BTR1 code)
  326.           HwType   : NON PLUG&PLAY: The type of hardware and operation mode
  327.           IOPort   : NON PLUG&PLAY: The I/O address for the parallel port
  328.           Interrupt: NON PLUG&PLAY: Interrupt number of the parallel port
  329.         
  330.         Returns:
  331.           A TPCANStatus error code
  332.         """
  333.         try:
  334.             res = self.__m_dllBasic.CAN_Initialize(Channel, Btr0Btr1, HwType, IOPort, Interrupt)
  335.             return TPCANStatus(res)
  336.         except:
  337.             print("Exception on PCANBasic.Initialize")
  338.             raise

  339.     # Initializes a FD capable PCAN Channel  
  340.     #
  341.     def InitializeFD(
  342.             self,
  343.             Channel,
  344.             BitrateFD):

  345.         """
  346.           Initializes a FD capable PCAN Channel  

  347.         Parameters:
  348.           Channel  : The handle of a FD capable PCAN Channel
  349.           BitrateFD : The speed for the communication (FD bit rate string)

  350.                 Remarks:
  351.                   See PCAN_BR_* values.
  352.           * parameter and values must be separated by '='
  353.           * Couples of Parameter/value must be separated by ','
  354.           * Following Parameter must be filled out: f_clock, data_brp, data_sjw, data_tseg1, data_tseg2,
  355.             nom_brp, nom_sjw, nom_tseg1, nom_tseg2.
  356.           * Following Parameters are optional (not used yet): data_ssp_offset, nom_samp

  357.                 Example:
  358.           f_clock_mhz=80,nom_brp=0,nom_tseg1=13,nom_tseg2=0,nom_sjw=0,data_brp=0,data_tseg1=13,data_tseg2=0,data_sjw=0

  359.         Returns:
  360.           A TPCANStatus error code
  361.         """
  362.         try:
  363.             res = self.__m_dllBasic.CAN_InitializeFD(Channel, BitrateFD)
  364.             return TPCANStatus(res)
  365.         except:
  366.             print("Exception on PCANBasic.InitializeFD")
  367.             raise

  368.     #  Uninitializes one or all PCAN Channels initialized by CAN_Initialize
  369.     #
  370.     def Uninitialize(
  371.             self,
  372.             Channel):

  373.         """
  374.           Uninitializes one or all PCAN Channels initialized by CAN_Initialize
  375.          
  376.         Remarks:
  377.           Giving the TPCANHandle value "PCAN_NONEBUS", uninitialize all initialized channels
  378.          
  379.         Parameters:
  380.           Channel  : A TPCANHandle representing a PCAN Channel
  381.         
  382.         Returns:
  383.           A TPCANStatus error code
  384.         """
  385.         try:
  386.             res = self.__m_dllBasic.CAN_Uninitialize(Channel)
  387.             return TPCANStatus(res)
  388.         except:
  389.             print("Exception on PCANBasic.Uninitialize")
  390.             raise

  391.     #  Resets the receive and transmit queues of the PCAN Channel
  392.     #
  393.     def Reset(
  394.             self,
  395.             Channel):

  396.         """
  397.           Resets the receive and transmit queues of the PCAN Channel
  398.          
  399.         Remarks:
  400.           A reset of the CAN controller is not performed
  401.          
  402.         Parameters:
  403.           Channel  : A TPCANHandle representing a PCAN Channel
  404.         
  405.         Returns:
  406.           A TPCANStatus error code
  407.         """
  408.         try:
  409.             res = self.__m_dllBasic.CAN_Reset(Channel)
  410.             return TPCANStatus(res)
  411.         except:
  412.             print("Exception on PCANBasic.Reset")
  413.             raise

  414.     #  Gets the current status of a PCAN Channel
  415.     #
  416.     def GetStatus(
  417.             self,
  418.             Channel):

  419.         """
  420.           Gets the current status of a PCAN Channel
  421.          
  422.         Parameters:
  423.           Channel  : A TPCANHandle representing a PCAN Channel
  424.         
  425.         Returns:
  426.           A TPCANStatus error code
  427.         """
  428.         try:
  429.             res = self.__m_dllBasic.CAN_GetStatus(Channel)
  430.             return TPCANStatus(res)
  431.         except:
  432.             print("Exception on PCANBasic.GetStatus")
  433.             raise

  434.     # Reads a CAN message from the receive queue of a PCAN Channel
  435.     #
  436.     def Read(
  437.             self,
  438.             Channel):

  439.         """
  440.           Reads a CAN message from the receive queue of a PCAN Channel

  441.         Remarks:
  442.           The return value of this method is a 3-touple, where
  443.           the first value is the result (TPCANStatus) of the method.
  444.           The order of the values are:
  445.           [0]: A TPCANStatus error code
  446.           [1]: A TPCANMsg structure with the CAN message read
  447.           [2]: A TPCANTimestamp structure with the time when a message was read
  448.          
  449.         Parameters:
  450.           Channel  : A TPCANHandle representing a PCAN Channel
  451.         
  452.         Returns:
  453.           A touple with three values
  454.         """
  455.         try:
  456.             msg = TPCANMsg()
  457.             timestamp = TPCANTimestamp()
  458.             res = self.__m_dllBasic.CAN_Read(Channel, byref(msg), byref(timestamp))
  459.             return TPCANStatus(res), msg, timestamp
  460.         except:
  461.             print("Exception on PCANBasic.Read")
  462.             raise

  463.             # Reads a CAN message from the receive queue of a FD capable PCAN Channel

  464.     #
  465.     def ReadFD(
  466.             self,
  467.             Channel):

  468.         """
  469.           Reads a CAN message from the receive queue of a FD capable PCAN Channel

  470.         Remarks:
  471.           The return value of this method is a 3-touple, where
  472.           the first value is the result (TPCANStatus) of the method.
  473.           The order of the values are:
  474.           [0]: A TPCANStatus error code
  475.           [1]: A TPCANMsgFD structure with the CAN message read
  476.           [2]: A TPCANTimestampFD that is the time when a message was read
  477.          
  478.         Parameters:
  479.           Channel  : The handle of a FD capable PCAN Channel
  480.         
  481.         Returns:
  482.           A touple with three values
  483.         """
  484.         try:
  485.             msg = TPCANMsgFD()
  486.             timestamp = TPCANTimestampFD()
  487.             res = self.__m_dllBasic.CAN_ReadFD(Channel, byref(msg), byref(timestamp))
  488.             return TPCANStatus(res), msg, timestamp
  489.         except:
  490.             print("Exception on PCANBasic.ReadFD")
  491.             raise

  492.             # Transmits a CAN message

  493.     #
  494.     def Write(
  495.             self,
  496.             Channel,
  497.             MessageBuffer):

  498.         """
  499.           Transmits a CAN message
  500.          
  501.         Parameters:
  502.           Channel      : A TPCANHandle representing a PCAN Channel
  503.           MessageBuffer: A TPCANMsg representing the CAN message to be sent
  504.         
  505.         Returns:
  506.           A TPCANStatus error code
  507.         """
  508.         try:
  509.             res = self.__m_dllBasic.CAN_Write(Channel, byref(MessageBuffer))
  510.             return TPCANStatus(res)
  511.         except:
  512.             print("Exception on PCANBasic.Write")
  513.             raise

  514.     # Transmits a CAN message over a FD capable PCAN Channel
  515.     #
  516.     def WriteFD(
  517.             self,
  518.             Channel,
  519.             MessageBuffer):

  520.         """
  521.           Transmits a CAN message over a FD capable PCAN Channel
  522.          
  523.         Parameters:
  524.           Channel      : The handle of a FD capable PCAN Channel
  525.           MessageBuffer: A TPCANMsgFD buffer with the message to be sent
  526.         
  527.         Returns:
  528.           A TPCANStatus error code
  529.         """
  530.         try:
  531.             res = self.__m_dllBasic.CAN_WriteFD(Channel, byref(MessageBuffer))
  532.             return TPCANStatus(res)
  533.         except:
  534.             print("Exception on PCANBasic.WriteFD")
  535.             raise

  536.     # Configures the reception filter
  537.     #
  538.     def FilterMessages(
  539.             self,
  540.             Channel,
  541.             FromID,
  542.             ToID,
  543.             Mode):

  544.         """
  545.           Configures the reception filter

  546.         Remarks:
  547.           The message filter will be expanded with every call to this function.
  548.           If it is desired to reset the filter, please use the 'SetValue' function.
  549.         
  550.         Parameters:
  551.           Channel : A TPCANHandle representing a PCAN Channel
  552.           FromID  : A c_uint value with the lowest CAN ID to be received
  553.           ToID    : A c_uint value with the highest CAN ID to be received
  554.           Mode    : A TPCANMode representing the message type (Standard, 11-bit
  555.                     identifier, or Extended, 29-bit identifier)
  556.         
  557.         Returns:
  558.           A TPCANStatus error code
  559.         """
  560.         try:
  561.             res = self.__m_dllBasic.CAN_FilterMessages(Channel, FromID, ToID, Mode)
  562.             return TPCANStatus(res)
  563.         except:
  564.             print("Exception on PCANBasic.FilterMessages")
  565.             raise

  566.     # Retrieves a PCAN Channel value
  567.     #
  568.     def GetValue(
  569.             self,
  570.             Channel,
  571.             Parameter):

  572.         """
  573.           Retrieves a PCAN Channel value

  574.         Remarks:
  575.           Parameters can be present or not according with the kind
  576.           of Hardware (PCAN Channel) being used. If a parameter is not available,
  577.           a PCAN_ERROR_ILLPARAMTYPE error will be returned.
  578.          
  579.           The return value of this method is a 2-touple, where
  580.           the first value is the result (TPCANStatus) of the method and
  581.           the second one, the asked value
  582.          
  583.         Parameters:
  584.           Channel   : A TPCANHandle representing a PCAN Channel
  585.           Parameter : The TPCANParameter parameter to get
  586.         
  587.         Returns:
  588.           A touple with 2 values
  589.         """
  590.         try:
  591.             if Parameter == PCAN_API_VERSION or Parameter == PCAN_HARDWARE_NAME or Parameter == PCAN_CHANNEL_VERSION or Parameter == PCAN_LOG_LOCATION or Parameter == PCAN_TRACE_LOCATION or Parameter == PCAN_BITRATE_INFO_FD or Parameter == PCAN_IP_ADDRESS:
  592.                 mybuffer = create_string_buffer(256)
  593.             else:
  594.                 mybuffer = c_int(0)

  595.             res = self.__m_dllBasic.CAN_GetValue(Channel, Parameter, byref(mybuffer), sizeof(mybuffer))
  596. ……………………

  597. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
PEAK CAN自动发送CAN报文的python程序.zip (1.04 MB, 下载次数: 144)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:564725 发表于 2019-6-17 16:29 | 只看该作者
十分感谢
回复

使用道具 举报

板凳
ID:540469 发表于 2019-7-21 16:06 | 只看该作者
这个已经不是简单了好不好
回复

使用道具 举报

地板
ID:606733 发表于 2019-9-3 10:13 | 只看该作者
这个是python3.7可以编译的吗,里面的注释是python2.7版本啊
回复

使用道具 举报

5#
ID:460994 发表于 2019-10-18 10:38 | 只看该作者
学习,学习,再学习
回复

使用道具 举报

6#
ID:460994 发表于 2019-10-18 10:38 | 只看该作者
好资料,学习一下
回复

使用道具 举报

7#
ID:684664 发表于 2020-1-10 15:13 | 只看该作者
厉害楼主,最近就想学习这方面的知识
回复

使用道具 举报

8#
ID:760063 发表于 2020-5-24 18:49 | 只看该作者
优秀,学习了
回复

使用道具 举报

9#
ID:845606 发表于 2020-11-18 21:02 | 只看该作者
好资料,值得学习
回复

使用道具 举报

10#
ID:849066 发表于 2020-11-25 17:32 | 只看该作者
有人重新编译了吗?我怎么编译提示找不到PCANBasic模块,我是3.8.2的版本。
回复

使用道具 举报

11#
ID:864882 发表于 2020-12-21 17:29 | 只看该作者
有人编译过了吗? 相应速度怎么样?
回复

使用道具 举报

12#
ID:521263 发表于 2021-1-11 18:21 | 只看该作者
可以使用吗?
回复

使用道具 举报

13#
ID:914012 发表于 2021-4-29 13:30 | 只看该作者
有点难,拉下去学习看看
回复

使用道具 举报

14#
ID:634658 发表于 2021-11-5 13:35 | 只看该作者
有没有接收的程序 ?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表