PCANBasic.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. // PCANBasic.cs
  2. //
  3. // ~~~~~~~~~~~~
  4. //
  5. // PCAN-Basic API
  6. //
  7. // ~~~~~~~~~~~~
  8. //
  9. // ------------------------------------------------------------------
  10. // Author : Keneth Wagner
  11. // Last change: 28.10.2019 Wagner
  12. //
  13. // Language: C# 1.0
  14. // ------------------------------------------------------------------
  15. //
  16. // Copyright (C) 1999-2019 PEAK-System Technik GmbH, Darmstadt
  17. // more Info at http://www.peak-system.com
  18. //
  19. using System;
  20. using System.Text;
  21. using System.Runtime.InteropServices;
  22. namespace SIASUN.Autopilot.Device
  23. {
  24. using TPCANHandle = System.UInt16;
  25. using TPCANBitrateFD = System.String;
  26. using TPCANTimestampFD = System.UInt64;
  27. #region Enumerations
  28. /// <summary>
  29. /// Represents a PCAN status/error code
  30. /// </summary>
  31. [Flags]
  32. public enum TPCANStatus : uint
  33. {
  34. /// <summary>
  35. /// No error
  36. /// </summary>
  37. PCAN_ERROR_OK = 0x00000,
  38. /// <summary>
  39. /// Transmit buffer in CAN controller is full
  40. /// </summary>
  41. PCAN_ERROR_XMTFULL = 0x00001,
  42. /// <summary>
  43. /// CAN controller was read too late
  44. /// </summary>
  45. PCAN_ERROR_OVERRUN = 0x00002,
  46. /// <summary>
  47. /// Bus error: an error counter reached the 'light' limit
  48. /// </summary>
  49. PCAN_ERROR_BUSLIGHT = 0x00004,
  50. /// <summary>
  51. /// Bus error: an error counter reached the 'heavy' limit
  52. /// </summary>
  53. PCAN_ERROR_BUSHEAVY = 0x00008,
  54. /// <summary>
  55. /// Bus error: an error counter reached the 'warning' limit
  56. /// </summary>
  57. PCAN_ERROR_BUSWARNING = PCAN_ERROR_BUSHEAVY,
  58. /// <summary>
  59. /// Bus error: the CAN controller is error passive
  60. /// </summary>
  61. PCAN_ERROR_BUSPASSIVE = 0x40000,
  62. /// <summary>
  63. /// Bus error: the CAN controller is in bus-off state
  64. /// </summary>
  65. PCAN_ERROR_BUSOFF = 0x00010,
  66. /// <summary>
  67. /// Mask for all bus errors
  68. /// </summary>
  69. PCAN_ERROR_ANYBUSERR = (PCAN_ERROR_BUSWARNING | PCAN_ERROR_BUSLIGHT | PCAN_ERROR_BUSHEAVY | PCAN_ERROR_BUSOFF | PCAN_ERROR_BUSPASSIVE),
  70. /// <summary>
  71. /// Receive queue is empty
  72. /// </summary>
  73. PCAN_ERROR_QRCVEMPTY = 0x00020,
  74. /// <summary>
  75. /// Receive queue was read too late
  76. /// </summary>
  77. PCAN_ERROR_QOVERRUN = 0x00040,
  78. /// <summary>
  79. /// Transmit queue is full
  80. /// </summary>
  81. PCAN_ERROR_QXMTFULL = 0x00080,
  82. /// <summary>
  83. /// Test of the CAN controller hardware registers failed (no hardware found)
  84. /// </summary>
  85. PCAN_ERROR_REGTEST = 0x00100,
  86. /// <summary>
  87. /// Driver not loaded
  88. /// </summary>
  89. PCAN_ERROR_NODRIVER = 0x00200,
  90. /// <summary>
  91. /// Hardware already in use by a Net
  92. /// </summary>
  93. PCAN_ERROR_HWINUSE = 0x00400,
  94. /// <summary>
  95. /// A Client is already connected to the Net
  96. /// </summary>
  97. PCAN_ERROR_NETINUSE = 0x00800,
  98. /// <summary>
  99. /// Hardware handle is invalid
  100. /// </summary>
  101. PCAN_ERROR_ILLHW = 0x01400,
  102. /// <summary>
  103. /// Net handle is invalid
  104. /// </summary>
  105. PCAN_ERROR_ILLNET = 0x01800,
  106. /// <summary>
  107. /// Client handle is invalid
  108. /// </summary>
  109. PCAN_ERROR_ILLCLIENT = 0x01C00,
  110. /// <summary>
  111. /// Mask for all handle errors
  112. /// </summary>
  113. PCAN_ERROR_ILLHANDLE = (PCAN_ERROR_ILLHW | PCAN_ERROR_ILLNET | PCAN_ERROR_ILLCLIENT),
  114. /// <summary>
  115. /// Resource (FIFO, Client, timeout) cannot be created
  116. /// </summary>
  117. PCAN_ERROR_RESOURCE = 0x02000,
  118. /// <summary>
  119. /// Invalid parameter
  120. /// </summary>
  121. PCAN_ERROR_ILLPARAMTYPE = 0x04000,
  122. /// <summary>
  123. /// Invalid parameter value
  124. /// </summary>
  125. PCAN_ERROR_ILLPARAMVAL = 0x08000,
  126. /// <summary>
  127. /// Unknown error
  128. /// </summary>
  129. PCAN_ERROR_UNKNOWN = 0x10000,
  130. /// <summary>
  131. /// Invalid data, function, or action.
  132. /// </summary>
  133. PCAN_ERROR_ILLDATA = 0x20000,
  134. /// <summary>
  135. /// Driver object state is wrong for the attempted operation
  136. /// </summary>
  137. PCAN_ERROR_ILLMODE = 0x80000,
  138. /// <summary>
  139. /// An operation was successfully carried out, however, irregularities were registered
  140. /// </summary>
  141. PCAN_ERROR_CAUTION = 0x2000000,
  142. /// <summary>
  143. /// Channel is not initialized
  144. /// <remarks>Value was changed from 0x40000 to 0x4000000</remarks>
  145. /// </summary>
  146. PCAN_ERROR_INITIALIZE = 0x4000000,
  147. /// <summary>
  148. /// Invalid operation
  149. /// <remarks>Value was changed from 0x80000 to 0x8000000</remarks>
  150. /// </summary>
  151. PCAN_ERROR_ILLOPERATION = 0x8000000,
  152. }
  153. /// <summary>
  154. /// Represents a PCAN device
  155. /// </summary>
  156. public enum TPCANDevice : byte
  157. {
  158. /// <summary>
  159. /// Undefined, unknown or not selected PCAN device value
  160. /// </summary>
  161. PCAN_NONE = 0,
  162. /// <summary>
  163. /// PCAN Non-PnP devices. NOT USED WITHIN PCAN-Basic API
  164. /// </summary>
  165. PCAN_PEAKCAN = 1,
  166. /// <summary>
  167. /// PCAN-ISA, PCAN-PC/104, and PCAN-PC/104-Plus
  168. /// </summary>
  169. PCAN_ISA = 2,
  170. /// <summary>
  171. /// PCAN-Dongle
  172. /// </summary>
  173. PCAN_DNG = 3,
  174. /// <summary>
  175. /// PCAN-PCI, PCAN-cPCI, PCAN-miniPCI, and PCAN-PCI Express
  176. /// </summary>
  177. PCAN_PCI = 4,
  178. /// <summary>
  179. /// PCAN-USB and PCAN-USB Pro
  180. /// </summary>
  181. PCAN_USB = 5,
  182. /// <summary>
  183. /// PCAN-PC Card
  184. /// </summary>
  185. PCAN_PCC = 6,
  186. /// <summary>
  187. /// PCAN Virtual hardware. NOT USED WITHIN PCAN-Basic API
  188. /// </summary>
  189. PCAN_VIRTUAL = 7,
  190. /// <summary>
  191. /// PCAN Gateway devices
  192. /// </summary>
  193. PCAN_LAN = 8
  194. }
  195. /// <summary>
  196. /// Represents a PCAN parameter to be read or set
  197. /// </summary>
  198. public enum TPCANParameter : byte
  199. {
  200. /// <summary>
  201. /// Device identifier parameter
  202. /// </summary>
  203. PCAN_DEVICE_ID = 1,
  204. /// <summary>
  205. /// DEPRECATED parameter. Use PCAN_DEVICE_ID instead
  206. /// </summary>
  207. [Obsolete]
  208. PCAN_DEVICE_NUMBER = PCAN_DEVICE_ID,
  209. /// <summary>
  210. /// 5-Volt power parameter
  211. /// </summary>
  212. PCAN_5VOLTS_POWER = 2,
  213. /// <summary>
  214. /// PCAN receive event handler parameter
  215. /// </summary>
  216. PCAN_RECEIVE_EVENT = 3,
  217. /// <summary>
  218. /// PCAN message filter parameter
  219. /// </summary>
  220. PCAN_MESSAGE_FILTER = 4,
  221. /// <summary>
  222. /// PCAN-Basic API version parameter
  223. /// </summary>
  224. PCAN_API_VERSION = 5,
  225. /// <summary>
  226. /// PCAN device channel version parameter
  227. /// </summary>
  228. PCAN_CHANNEL_VERSION = 6,
  229. /// <summary>
  230. /// PCAN Reset-On-Busoff parameter
  231. /// </summary>
  232. PCAN_BUSOFF_AUTORESET = 7,
  233. /// <summary>
  234. /// PCAN Listen-Only parameter
  235. /// </summary>
  236. PCAN_LISTEN_ONLY = 8,
  237. /// <summary>
  238. /// Directory path for log files
  239. /// </summary>
  240. PCAN_LOG_LOCATION = 9,
  241. /// <summary>
  242. /// Debug-Log activation status
  243. /// </summary>
  244. PCAN_LOG_STATUS = 10,
  245. /// <summary>
  246. /// Configuration of the debugged information (LOG_FUNCTION_***)
  247. /// </summary>
  248. PCAN_LOG_CONFIGURE = 11,
  249. /// <summary>
  250. /// Custom insertion of text into the log file
  251. /// </summary>
  252. PCAN_LOG_TEXT = 12,
  253. /// <summary>
  254. /// Availability status of a PCAN-Channel
  255. /// </summary>
  256. PCAN_CHANNEL_CONDITION = 13,
  257. /// <summary>
  258. /// PCAN hardware name parameter
  259. /// </summary>
  260. PCAN_HARDWARE_NAME = 14,
  261. /// <summary>
  262. /// Message reception status of a PCAN-Channel
  263. /// </summary>
  264. PCAN_RECEIVE_STATUS = 15,
  265. /// <summary>
  266. /// CAN-Controller number of a PCAN-Channel
  267. /// </summary>
  268. PCAN_CONTROLLER_NUMBER = 16,
  269. /// <summary>
  270. /// Directory path for PCAN trace files
  271. /// </summary>
  272. PCAN_TRACE_LOCATION = 17,
  273. /// <summary>
  274. /// CAN tracing activation status
  275. /// </summary>
  276. PCAN_TRACE_STATUS = 18,
  277. /// <summary>
  278. /// Configuration of the maximum file size of a CAN trace
  279. /// </summary>
  280. PCAN_TRACE_SIZE = 19,
  281. /// <summary>
  282. /// Configuration of the trace file storing mode (TRACE_FILE_***)
  283. /// </summary>
  284. PCAN_TRACE_CONFIGURE = 20,
  285. /// <summary>
  286. /// Physical identification of a USB based PCAN-Channel by blinking its associated LED
  287. /// </summary>
  288. PCAN_CHANNEL_IDENTIFYING = 21,
  289. /// <summary>
  290. /// Capabilities of a PCAN device (FEATURE_***)
  291. /// </summary>
  292. PCAN_CHANNEL_FEATURES = 22,
  293. /// <summary>
  294. /// Using of an existing bit rate (PCAN-View connected to a channel)
  295. /// </summary>
  296. PCAN_BITRATE_ADAPTING = 23,
  297. /// <summary>
  298. /// Configured bit rate as Btr0Btr1 value
  299. /// </summary>
  300. PCAN_BITRATE_INFO = 24,
  301. /// <summary>
  302. /// Configured bit rate as TPCANBitrateFD string
  303. /// </summary>
  304. PCAN_BITRATE_INFO_FD = 25,
  305. /// <summary>
  306. /// Configured nominal CAN Bus speed as Bits per seconds
  307. /// </summary>
  308. PCAN_BUSSPEED_NOMINAL = 26,
  309. /// <summary>
  310. /// Configured CAN data speed as Bits per seconds
  311. /// </summary>
  312. PCAN_BUSSPEED_DATA = 27,
  313. /// <summary>
  314. /// Remote address of a LAN channel as string in IPv4 format
  315. /// </summary>
  316. PCAN_IP_ADDRESS = 28,
  317. /// <summary>
  318. /// Status of the Virtual PCAN-Gateway Service
  319. /// </summary>
  320. PCAN_LAN_SERVICE_STATUS = 29,
  321. /// <summary>
  322. /// Status messages reception status within a PCAN-Channel
  323. /// </summary>
  324. PCAN_ALLOW_STATUS_FRAMES = 30,
  325. /// <summary>
  326. /// RTR messages reception status within a PCAN-Channel
  327. /// </summary>
  328. PCAN_ALLOW_RTR_FRAMES = 31,
  329. /// <summary>
  330. /// Error messages reception status within a PCAN-Channel
  331. /// </summary>
  332. PCAN_ALLOW_ERROR_FRAMES = 32,
  333. /// <summary>
  334. /// Delay, in microseconds, between sending frames
  335. /// </summary>
  336. PCAN_INTERFRAME_DELAY = 33,
  337. /// <summary>
  338. /// Filter over code and mask patterns for 11-Bit messages
  339. /// </summary>
  340. PCAN_ACCEPTANCE_FILTER_11BIT = 34,
  341. /// <summary>
  342. /// Filter over code and mask patterns for 29-Bit messages
  343. /// </summary>
  344. PCAN_ACCEPTANCE_FILTER_29BIT = 35,
  345. /// <summary>
  346. /// Output mode of 32 digital I/O pin of a PCAN-USB Chip. 1: Output-Active 0 : Output Inactive
  347. /// </summary>
  348. PCAN_IO_DIGITAL_CONFIGURATION = 36,
  349. /// <summary>
  350. /// Value assigned to a 32 digital I/O pins of a PCAN-USB Chip
  351. /// </summary>
  352. PCAN_IO_DIGITAL_VALUE = 37,
  353. /// <summary>
  354. /// Value assigned to a 32 digital I/O pins of a PCAN-USB Chip - Multiple digital I/O pins to 1 = High
  355. /// </summary>
  356. PCAN_IO_DIGITAL_SET = 38,
  357. /// <summary>
  358. /// Clear multiple digital I/O pins to 0
  359. /// </summary>
  360. PCAN_IO_DIGITAL_CLEAR = 39,
  361. /// <summary>
  362. /// Get value of a single analog input pin
  363. /// </summary>
  364. PCAN_IO_ANALOG_VALUE = 40,
  365. /// <summary>
  366. /// Get the version of the firmware used by the device associated with a PCAN-Channel
  367. /// </summary>
  368. PCAN_FIRMWARE_VERSION = 41,
  369. /// <summary>
  370. /// Get the amount of PCAN channels attached to a system
  371. /// </summary>
  372. PCAN_ATTACHED_CHANNELS_COUNT = 42,
  373. /// <summary>
  374. /// Get information about PCAN channels attached to a system
  375. /// </summary>
  376. PCAN_ATTACHED_CHANNELS = 43,
  377. }
  378. /// <summary>
  379. /// Represents the type of a PCAN message
  380. /// </summary>
  381. [Flags]
  382. public enum TPCANMessageType : byte
  383. {
  384. /// <summary>
  385. /// The PCAN message is a CAN Standard Frame (11-bit identifier)
  386. /// </summary>
  387. PCAN_MESSAGE_STANDARD = 0x00,
  388. /// <summary>
  389. /// The PCAN message is a CAN Remote-Transfer-Request Frame
  390. /// </summary>
  391. PCAN_MESSAGE_RTR = 0x01,
  392. /// <summary>
  393. /// The PCAN message is a CAN Extended Frame (29-bit identifier)
  394. /// </summary>
  395. PCAN_MESSAGE_EXTENDED = 0x02,
  396. /// <summary>
  397. /// The PCAN message represents a FD frame in terms of CiA Specs
  398. /// </summary>
  399. PCAN_MESSAGE_FD = 0x04,
  400. /// <summary>
  401. /// The PCAN message represents a FD bit rate switch (CAN data at a higher bit rate)
  402. /// </summary>
  403. PCAN_MESSAGE_BRS = 0x08,
  404. /// <summary>
  405. /// The PCAN message represents a FD error state indicator(CAN FD transmitter was error active)
  406. /// </summary>
  407. PCAN_MESSAGE_ESI = 0x10,
  408. /// <summary>
  409. /// The PCAN message represents an error frame
  410. /// </summary>
  411. PCAN_MESSAGE_ERRFRAME = 0x40,
  412. /// <summary>
  413. /// The PCAN message represents a PCAN status message
  414. /// </summary>
  415. PCAN_MESSAGE_STATUS = 0x80,
  416. }
  417. /// <summary>
  418. /// Represents a PCAN filter mode
  419. /// </summary>
  420. public enum TPCANMode : byte
  421. {
  422. /// <summary>
  423. /// Mode is Standard (11-bit identifier)
  424. /// </summary>
  425. PCAN_MODE_STANDARD = TPCANMessageType.PCAN_MESSAGE_STANDARD,
  426. /// <summary>
  427. /// Mode is Extended (29-bit identifier)
  428. /// </summary>
  429. PCAN_MODE_EXTENDED = TPCANMessageType.PCAN_MESSAGE_EXTENDED,
  430. }
  431. /// <summary>
  432. /// Represents a PCAN Baud rate register value
  433. /// </summary>
  434. public enum TPCANBaudrate : ushort
  435. {
  436. /// <summary>
  437. /// 1 MBit/s
  438. /// </summary>
  439. PCAN_BAUD_1M = 0x0014,
  440. /// <summary>
  441. /// 800 KBit/s
  442. /// </summary>
  443. PCAN_BAUD_800K = 0x0016,
  444. /// <summary>
  445. /// 500 kBit/s
  446. /// </summary>
  447. PCAN_BAUD_500K = 0x001C,
  448. /// <summary>
  449. /// 250 kBit/s
  450. /// </summary>
  451. PCAN_BAUD_250K = 0x011C,
  452. /// <summary>
  453. /// 125 kBit/s
  454. /// </summary>
  455. PCAN_BAUD_125K = 0x031C,
  456. /// <summary>
  457. /// 100 kBit/s
  458. /// </summary>
  459. PCAN_BAUD_100K = 0x432F,
  460. /// <summary>
  461. /// 95,238 KBit/s
  462. /// </summary>
  463. PCAN_BAUD_95K = 0xC34E,
  464. /// <summary>
  465. /// 83,333 KBit/s
  466. /// </summary>
  467. PCAN_BAUD_83K = 0x852B,
  468. /// <summary>
  469. /// 50 kBit/s
  470. /// </summary>
  471. PCAN_BAUD_50K = 0x472F,
  472. /// <summary>
  473. /// 47,619 KBit/s
  474. /// </summary>
  475. PCAN_BAUD_47K = 0x1414,
  476. /// <summary>
  477. /// 33,333 KBit/s
  478. /// </summary>
  479. PCAN_BAUD_33K = 0x8B2F,
  480. /// <summary>
  481. /// 20 kBit/s
  482. /// </summary>
  483. PCAN_BAUD_20K = 0x532F,
  484. /// <summary>
  485. /// 10 kBit/s
  486. /// </summary>
  487. PCAN_BAUD_10K = 0x672F,
  488. /// <summary>
  489. /// 5 kBit/s
  490. /// </summary>
  491. PCAN_BAUD_5K = 0x7F7F,
  492. }
  493. /// <summary>
  494. /// Represents the type of PCAN (Non-PnP) hardware to be initialized
  495. /// </summary>
  496. public enum TPCANType : byte
  497. {
  498. /// <summary>
  499. /// PCAN-ISA 82C200
  500. /// </summary>
  501. PCAN_TYPE_ISA = 0x01,
  502. /// <summary>
  503. /// PCAN-ISA SJA1000
  504. /// </summary>
  505. PCAN_TYPE_ISA_SJA = 0x09,
  506. /// <summary>
  507. /// PHYTEC ISA
  508. /// </summary>
  509. PCAN_TYPE_ISA_PHYTEC = 0x04,
  510. /// <summary>
  511. /// PCAN-Dongle 82C200
  512. /// </summary>
  513. PCAN_TYPE_DNG = 0x02,
  514. /// <summary>
  515. /// PCAN-Dongle EPP 82C200
  516. /// </summary>
  517. PCAN_TYPE_DNG_EPP = 0x03,
  518. /// <summary>
  519. /// PCAN-Dongle SJA1000
  520. /// </summary>
  521. PCAN_TYPE_DNG_SJA = 0x05,
  522. /// <summary>
  523. /// PCAN-Dongle EPP SJA1000
  524. /// </summary>
  525. PCAN_TYPE_DNG_SJA_EPP = 0x06,
  526. }
  527. #endregion
  528. #region Structures
  529. /// <summary>
  530. /// Represents a PCAN message
  531. /// </summary>
  532. public struct TPCANMsg
  533. {
  534. /// <summary>
  535. /// 11/29-bit message identifier
  536. /// </summary>
  537. public uint ID;
  538. /// <summary>
  539. /// Type of the message
  540. /// </summary>
  541. [MarshalAs(UnmanagedType.U1)]
  542. public TPCANMessageType MSGTYPE;
  543. /// <summary>
  544. /// Data Length Code of the message (0..8)
  545. /// </summary>
  546. public byte LEN;
  547. /// <summary>
  548. /// Data of the message (DATA[0]..DATA[7])
  549. /// </summary>
  550. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  551. public byte[] DATA;
  552. }
  553. /// <summary>
  554. /// Represents a timestamp of a received PCAN message.
  555. /// Total Microseconds = micros + 1000 * millis + 0x100000000 * 1000 * millis_overflow
  556. /// </summary>
  557. public struct TPCANTimestamp
  558. {
  559. /// <summary>
  560. /// Base-value: milliseconds: 0.. 2^32-1
  561. /// </summary>
  562. public uint millis;
  563. /// <summary>
  564. /// Roll-arounds of millis
  565. /// </summary>
  566. public ushort millis_overflow;
  567. /// <summary>
  568. /// Microseconds: 0..999
  569. /// </summary>
  570. public ushort micros;
  571. }
  572. /// <summary>
  573. /// Represents a PCAN message from a FD capable hardware
  574. /// </summary>
  575. public struct TPCANMsgFD
  576. {
  577. /// <summary>
  578. /// 11/29-bit message identifier
  579. /// </summary>
  580. public uint ID;
  581. /// <summary>
  582. /// Type of the message
  583. /// </summary>
  584. [MarshalAs(UnmanagedType.U1)]
  585. public TPCANMessageType MSGTYPE;
  586. /// <summary>
  587. /// Data Length Code of the message (0..15)
  588. /// </summary>
  589. public byte DLC;
  590. /// <summary>
  591. /// Data of the message (DATA[0]..DATA[63])
  592. /// </summary>
  593. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  594. public byte[] DATA;
  595. }
  596. /// <summary>
  597. /// Describes an available PCAN channel
  598. /// </summary>
  599. public struct TPCANChannelInformation
  600. {
  601. /// <summary>
  602. /// PCAN channel handle
  603. /// </summary>
  604. [MarshalAs(UnmanagedType.U2)]
  605. public TPCANHandle channel_handle;
  606. /// <summary>
  607. /// Kind of PCAN device
  608. /// </summary>
  609. [MarshalAs(UnmanagedType.U1)]
  610. public TPCANDevice device_type;
  611. /// <summary>
  612. /// CAN-Controller number
  613. /// </summary>
  614. public byte controller_number;
  615. /// <summary>
  616. /// Device capabilities flag (see FEATURE_*)
  617. /// </summary>
  618. public uint device_features;
  619. /// <summary>
  620. /// Device name
  621. /// </summary>
  622. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = PCANBasic.MAX_LENGTH_HARDWARE_NAME)]
  623. public string device_name;
  624. /// <summary>
  625. /// Device number
  626. /// </summary>
  627. public uint device_id;
  628. /// <summary>
  629. /// Availability status of a PCAN-Channel
  630. /// </summary>
  631. public uint channel_condition;
  632. }
  633. #endregion
  634. #region PCANBasic class
  635. /// <summary>
  636. /// PCAN-Basic API class implementation
  637. /// </summary>
  638. public static class PCANBasic
  639. {
  640. #region PCAN-BUS Handles Definition
  641. /// <summary>
  642. /// Undefined/default value for a PCAN bus
  643. /// </summary>
  644. public const TPCANHandle PCAN_NONEBUS = 0x00;
  645. /// <summary>
  646. /// PCAN-ISA interface, channel 1
  647. /// </summary>
  648. public const TPCANHandle PCAN_ISABUS1 = 0x21;
  649. /// <summary>
  650. /// PCAN-ISA interface, channel 2
  651. /// </summary>
  652. public const TPCANHandle PCAN_ISABUS2 = 0x22;
  653. /// <summary>
  654. /// PCAN-ISA interface, channel 3
  655. /// </summary>
  656. public const TPCANHandle PCAN_ISABUS3 = 0x23;
  657. /// <summary>
  658. /// PCAN-ISA interface, channel 4
  659. /// </summary>
  660. public const TPCANHandle PCAN_ISABUS4 = 0x24;
  661. /// <summary>
  662. /// PCAN-ISA interface, channel 5
  663. /// </summary>
  664. public const TPCANHandle PCAN_ISABUS5 = 0x25;
  665. /// <summary>
  666. /// PCAN-ISA interface, channel 6
  667. /// </summary>
  668. public const TPCANHandle PCAN_ISABUS6 = 0x26;
  669. /// <summary>
  670. /// PCAN-ISA interface, channel 7
  671. /// </summary>
  672. public const TPCANHandle PCAN_ISABUS7 = 0x27;
  673. /// <summary>
  674. /// PCAN-ISA interface, channel 8
  675. /// </summary>
  676. public const TPCANHandle PCAN_ISABUS8 = 0x28;
  677. /// <summary>
  678. /// PPCAN-Dongle/LPT interface, channel 1
  679. /// </summary>
  680. public const TPCANHandle PCAN_DNGBUS1 = 0x31;
  681. /// <summary>
  682. /// PCAN-PCI interface, channel 1
  683. /// </summary>
  684. public const TPCANHandle PCAN_PCIBUS1 = 0x41;
  685. /// <summary>
  686. /// PCAN-PCI interface, channel 2
  687. /// </summary>
  688. public const TPCANHandle PCAN_PCIBUS2 = 0x42;
  689. /// <summary>
  690. /// PCAN-PCI interface, channel 3
  691. /// </summary>
  692. public const TPCANHandle PCAN_PCIBUS3 = 0x43;
  693. /// <summary>
  694. /// PCAN-PCI interface, channel 4
  695. /// </summary>
  696. public const TPCANHandle PCAN_PCIBUS4 = 0x44;
  697. /// <summary>
  698. /// PCAN-PCI interface, channel 5
  699. /// </summary>
  700. public const TPCANHandle PCAN_PCIBUS5 = 0x45;
  701. /// <summary>
  702. /// PCAN-PCI interface, channel 6
  703. /// </summary>
  704. public const TPCANHandle PCAN_PCIBUS6 = 0x46;
  705. /// <summary>
  706. /// PCAN-PCI interface, channel 7
  707. /// </summary>
  708. public const TPCANHandle PCAN_PCIBUS7 = 0x47;
  709. /// <summary>
  710. /// PCAN-PCI interface, channel 8
  711. /// </summary>
  712. public const TPCANHandle PCAN_PCIBUS8 = 0x48;
  713. /// <summary>
  714. /// PCAN-PCI interface, channel 9
  715. /// </summary>
  716. public const TPCANHandle PCAN_PCIBUS9 = 0x409;
  717. /// <summary>
  718. /// PCAN-PCI interface, channel 10
  719. /// </summary>
  720. public const TPCANHandle PCAN_PCIBUS10 = 0x40A;
  721. /// <summary>
  722. /// PCAN-PCI interface, channel 11
  723. /// </summary>
  724. public const TPCANHandle PCAN_PCIBUS11 = 0x40B;
  725. /// <summary>
  726. /// PCAN-PCI interface, channel 12
  727. /// </summary>
  728. public const TPCANHandle PCAN_PCIBUS12 = 0x40C;
  729. /// <summary>
  730. /// PCAN-PCI interface, channel 13
  731. /// </summary>
  732. public const TPCANHandle PCAN_PCIBUS13 = 0x40D;
  733. /// <summary>
  734. /// PCAN-PCI interface, channel 14
  735. /// </summary>
  736. public const TPCANHandle PCAN_PCIBUS14 = 0x40E;
  737. /// <summary>
  738. /// PCAN-PCI interface, channel 15
  739. /// </summary>
  740. public const TPCANHandle PCAN_PCIBUS15 = 0x40F;
  741. /// <summary>
  742. /// PCAN-PCI interface, channel 16
  743. /// </summary>
  744. public const TPCANHandle PCAN_PCIBUS16 = 0x410;
  745. /// <summary>
  746. /// PCAN-USB interface, channel 1
  747. /// </summary>
  748. public const TPCANHandle PCAN_USBBUS1 = 0x51;
  749. /// <summary>
  750. /// PCAN-USB interface, channel 2
  751. /// </summary>
  752. public const TPCANHandle PCAN_USBBUS2 = 0x52;
  753. /// <summary>
  754. /// PCAN-USB interface, channel 3
  755. /// </summary>
  756. public const TPCANHandle PCAN_USBBUS3 = 0x53;
  757. /// <summary>
  758. /// PCAN-USB interface, channel 4
  759. /// </summary>
  760. public const TPCANHandle PCAN_USBBUS4 = 0x54;
  761. /// <summary>
  762. /// PCAN-USB interface, channel 5
  763. /// </summary>
  764. public const TPCANHandle PCAN_USBBUS5 = 0x55;
  765. /// <summary>
  766. /// PCAN-USB interface, channel 6
  767. /// </summary>
  768. public const TPCANHandle PCAN_USBBUS6 = 0x56;
  769. /// <summary>
  770. /// PCAN-USB interface, channel 7
  771. /// </summary>
  772. public const TPCANHandle PCAN_USBBUS7 = 0x57;
  773. /// <summary>
  774. /// PCAN-USB interface, channel 8
  775. /// </summary>
  776. public const TPCANHandle PCAN_USBBUS8 = 0x58;
  777. /// <summary>
  778. /// PCAN-USB interface, channel 9
  779. /// </summary>
  780. public const TPCANHandle PCAN_USBBUS9 = 0x509;
  781. /// <summary>
  782. /// PCAN-USB interface, channel 10
  783. /// </summary>
  784. public const TPCANHandle PCAN_USBBUS10 = 0x50A;
  785. /// <summary>
  786. /// PCAN-USB interface, channel 11
  787. /// </summary>
  788. public const TPCANHandle PCAN_USBBUS11 = 0x50B;
  789. /// <summary>
  790. /// PCAN-USB interface, channel 12
  791. /// </summary>
  792. public const TPCANHandle PCAN_USBBUS12 = 0x50C;
  793. /// <summary>
  794. /// PCAN-USB interface, channel 13
  795. /// </summary>
  796. public const TPCANHandle PCAN_USBBUS13 = 0x50D;
  797. /// <summary>
  798. /// PCAN-USB interface, channel 14
  799. /// </summary>
  800. public const TPCANHandle PCAN_USBBUS14 = 0x50E;
  801. /// <summary>
  802. /// PCAN-USB interface, channel 15
  803. /// </summary>
  804. public const TPCANHandle PCAN_USBBUS15 = 0x50F;
  805. /// <summary>
  806. /// PCAN-USB interface, channel 16
  807. /// </summary>
  808. public const TPCANHandle PCAN_USBBUS16 = 0x510;
  809. /// <summary>
  810. /// PCAN-PC Card interface, channel 1
  811. /// </summary>
  812. public const TPCANHandle PCAN_PCCBUS1 = 0x61;
  813. /// <summary>
  814. /// PCAN-PC Card interface, channel 2
  815. /// </summary>
  816. public const TPCANHandle PCAN_PCCBUS2 = 0x62;
  817. /// <summary>
  818. /// PCAN-LAN interface, channel 1
  819. /// </summary>
  820. public const TPCANHandle PCAN_LANBUS1 = 0x801;
  821. /// <summary>
  822. /// PCAN-LAN interface, channel 2
  823. /// </summary>
  824. public const TPCANHandle PCAN_LANBUS2 = 0x802;
  825. /// <summary>
  826. /// PCAN-LAN interface, channel 3
  827. /// </summary>
  828. public const TPCANHandle PCAN_LANBUS3 = 0x803;
  829. /// <summary>
  830. /// PCAN-LAN interface, channel 4
  831. /// </summary>
  832. public const TPCANHandle PCAN_LANBUS4 = 0x804;
  833. /// <summary>
  834. /// PCAN-LAN interface, channel 5
  835. /// </summary>
  836. public const TPCANHandle PCAN_LANBUS5 = 0x805;
  837. /// <summary>
  838. /// PCAN-LAN interface, channel 6
  839. /// </summary>
  840. public const TPCANHandle PCAN_LANBUS6 = 0x806;
  841. /// <summary>
  842. /// PCAN-LAN interface, channel 7
  843. /// </summary>
  844. public const TPCANHandle PCAN_LANBUS7 = 0x807;
  845. /// <summary>
  846. /// PCAN-LAN interface, channel 8
  847. /// </summary>
  848. public const TPCANHandle PCAN_LANBUS8 = 0x808;
  849. /// <summary>
  850. /// PCAN-LAN interface, channel 9
  851. /// </summary>
  852. public const TPCANHandle PCAN_LANBUS9 = 0x809;
  853. /// <summary>
  854. /// PCAN-LAN interface, channel 10
  855. /// </summary>
  856. public const TPCANHandle PCAN_LANBUS10 = 0x80A;
  857. /// <summary>
  858. /// PCAN-LAN interface, channel 11
  859. /// </summary>
  860. public const TPCANHandle PCAN_LANBUS11 = 0x80B;
  861. /// <summary>
  862. /// PCAN-LAN interface, channel 12
  863. /// </summary>
  864. public const TPCANHandle PCAN_LANBUS12 = 0x80C;
  865. /// <summary>
  866. /// PCAN-LAN interface, channel 13
  867. /// </summary>
  868. public const TPCANHandle PCAN_LANBUS13 = 0x80D;
  869. /// <summary>
  870. /// PCAN-LAN interface, channel 14
  871. /// </summary>
  872. public const TPCANHandle PCAN_LANBUS14 = 0x80E;
  873. /// <summary>
  874. /// PCAN-LAN interface, channel 15
  875. /// </summary>
  876. public const TPCANHandle PCAN_LANBUS15 = 0x80F;
  877. /// <summary>
  878. /// PCAN-LAN interface, channel 16
  879. /// </summary>
  880. public const TPCANHandle PCAN_LANBUS16 = 0x810;
  881. #endregion
  882. #region FD Bit rate parameters
  883. /// <summary>
  884. /// Clock frequency in Herz (80000000, 60000000, 40000000, 30000000, 24000000, 20000000)
  885. /// </summary>
  886. public const string PCAN_BR_CLOCK = "f_clock";
  887. /// <summary>
  888. /// Clock frequency in Megaherz (80, 60, 40, 30, 24, 20)
  889. /// </summary>
  890. public const string PCAN_BR_CLOCK_MHZ = "f_clock_mhz";
  891. /// <summary>
  892. /// Clock prescaler for nominal time quantum
  893. /// </summary>
  894. public const string PCAN_BR_NOM_BRP = "nom_brp";
  895. /// <summary>
  896. /// TSEG1 segment for nominal bit rate in time quanta
  897. /// </summary>
  898. public const string PCAN_BR_NOM_TSEG1 = "nom_tseg1";
  899. /// <summary>
  900. /// TSEG2 segment for nominal bit rate in time quanta
  901. /// </summary>
  902. public const string PCAN_BR_NOM_TSEG2 = "nom_tseg2";
  903. /// <summary>
  904. /// Synchronization Jump Width for nominal bit rate in time quanta
  905. /// </summary>
  906. public const string PCAN_BR_NOM_SJW = "nom_sjw";
  907. /// <summary>
  908. /// Sample point for nominal bit rate
  909. /// </summary>
  910. public const string PCAN_BR_NOM_SAMPLE = "nom_sam";
  911. /// <summary>
  912. /// Clock prescaler for highspeed data time quantum
  913. /// </summary>
  914. public const string PCAN_BR_DATA_BRP = "data_brp";
  915. /// <summary>
  916. /// TSEG1 segment for fast data bit rate in time quanta
  917. /// </summary>
  918. public const string PCAN_BR_DATA_TSEG1 = "data_tseg1";
  919. /// <summary>
  920. /// TSEG2 segment for fast data bit rate in time quanta
  921. /// </summary>
  922. public const string PCAN_BR_DATA_TSEG2 = "data_tseg2";
  923. /// <summary>
  924. /// Synchronization Jump Width for highspeed data bit rate in time quanta
  925. /// </summary>
  926. public const string PCAN_BR_DATA_SJW = "data_sjw";
  927. /// <summary>
  928. /// Secondary sample point delay for highspeed data bit rate in cyles
  929. /// </summary>
  930. public const string PCAN_BR_DATA_SAMPLE = "data_ssp_offset";
  931. #endregion
  932. #region Parameter values definition
  933. /// <summary>
  934. /// The PCAN parameter is not set (inactive)
  935. /// </summary>
  936. public const int PCAN_PARAMETER_OFF = 0;
  937. /// <summary>
  938. /// The PCAN parameter is set (active)
  939. /// </summary>
  940. public const int PCAN_PARAMETER_ON = 1;
  941. /// <summary>
  942. /// The PCAN filter is closed. No messages will be received
  943. /// </summary>
  944. public const int PCAN_FILTER_CLOSE = 0;
  945. /// <summary>
  946. /// The PCAN filter is fully opened. All messages will be received
  947. /// </summary>
  948. public const int PCAN_FILTER_OPEN = 1;
  949. /// <summary>
  950. /// The PCAN filter is custom configured. Only registered
  951. /// messages will be received
  952. /// </summary>
  953. public const int PCAN_FILTER_CUSTOM = 2;
  954. /// <summary>
  955. /// The PCAN-Channel handle is illegal, or its associated hardware is not available
  956. /// </summary>
  957. public const int PCAN_CHANNEL_UNAVAILABLE = 0;
  958. /// <summary>
  959. /// The PCAN-Channel handle is available to be connected (PnP Hardware: it means furthermore that the hardware is plugged-in)
  960. /// </summary>
  961. public const int PCAN_CHANNEL_AVAILABLE = 1;
  962. /// <summary>
  963. /// The PCAN-Channel handle is valid, and is already being used
  964. /// </summary>
  965. public const int PCAN_CHANNEL_OCCUPIED = 2;
  966. /// <summary>
  967. /// The PCAN-Channel handle is already being used by a PCAN-View application, but is available to connect
  968. /// </summary>
  969. public const int PCAN_CHANNEL_PCANVIEW = (PCAN_CHANNEL_AVAILABLE | PCAN_CHANNEL_OCCUPIED);
  970. /// <summary>
  971. /// Logs system exceptions / errors
  972. /// </summary>
  973. public const int LOG_FUNCTION_DEFAULT = 0x00;
  974. /// <summary>
  975. /// Logs the entries to the PCAN-Basic API functions
  976. /// </summary>
  977. public const int LOG_FUNCTION_ENTRY = 0x01;
  978. /// <summary>
  979. /// Logs the parameters passed to the PCAN-Basic API functions
  980. /// </summary>
  981. public const int LOG_FUNCTION_PARAMETERS = 0x02;
  982. /// <summary>
  983. /// Logs the exits from the PCAN-Basic API functions
  984. /// </summary>
  985. public const int LOG_FUNCTION_LEAVE = 0x04;
  986. /// <summary>
  987. /// Logs the CAN messages passed to the CAN_Write function
  988. /// </summary>
  989. public const int LOG_FUNCTION_WRITE = 0x08;
  990. /// <summary>
  991. /// Logs the CAN messages received within the CAN_Read function
  992. /// </summary>
  993. public const int LOG_FUNCTION_READ = 0x10;
  994. /// <summary>
  995. /// Logs all possible information within the PCAN-Basic API functions
  996. /// </summary>
  997. public const int LOG_FUNCTION_ALL = 0xFFFF;
  998. /// <summary>
  999. /// A single file is written until it size reaches PAN_TRACE_SIZE
  1000. /// </summary>
  1001. public const int TRACE_FILE_SINGLE = 0x00;
  1002. /// <summary>
  1003. /// Traced data is distributed in several files with size PAN_TRACE_SIZE
  1004. /// </summary>
  1005. public const int TRACE_FILE_SEGMENTED = 0x01;
  1006. /// <summary>
  1007. /// Includes the date into the name of the trace file
  1008. /// </summary>
  1009. public const int TRACE_FILE_DATE = 0x02;
  1010. /// <summary>
  1011. /// Includes the start time into the name of the trace file
  1012. /// </summary>
  1013. public const int TRACE_FILE_TIME = 0x04;
  1014. /// <summary>
  1015. /// Causes the overwriting of available traces (same name)
  1016. /// </summary>
  1017. public const int TRACE_FILE_OVERWRITE = 0x80;
  1018. /// <summary>
  1019. /// Device supports flexible data-rate (CAN-FD)
  1020. /// </summary>
  1021. public const int FEATURE_FD_CAPABLE = 0x01;
  1022. /// <summary>
  1023. /// Device supports a delay between sending frames (FPGA based USB devices)
  1024. /// </summary>
  1025. public const int FEATURE_DELAY_CAPABLE = 0x02;
  1026. /// <summary>
  1027. /// Device supports I/O functionality for electronic circuits (USB-Chip devices)
  1028. /// </summary>
  1029. public const int FEATURE_IO_CAPABLE = 0x04;
  1030. /// <summary>
  1031. /// The service is not running
  1032. /// </summary>
  1033. public const int SERVICE_STATUS_STOPPED = 0x01;
  1034. /// <summary>
  1035. /// The service is running
  1036. /// </summary>
  1037. public const int SERVICE_STATUS_RUNNING = 0x04;
  1038. #endregion
  1039. #region Other Constants
  1040. /// <summary>
  1041. /// Maximum length of the name of a device: 32 characters + terminator
  1042. /// </summary>
  1043. public const int MAX_LENGTH_HARDWARE_NAME = 33;
  1044. /// <summary>
  1045. /// Maximum length of a version string: 17 characters + terminator
  1046. /// </summary>
  1047. public const int MAX_LENGTH_VERSION_STRING = 18;
  1048. #endregion
  1049. #region PCANBasic API Implementation
  1050. /// <summary>
  1051. /// Initializes a PCAN Channel
  1052. /// </summary>
  1053. /// <param name="Channel">The handle of a PCAN Channel</param>
  1054. /// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
  1055. /// <param name="HwType">Non-PnP: The type of hardware and operation mode</param>
  1056. /// <param name="IOPort">Non-PnP: The I/O address for the parallel port</param>
  1057. /// <param name="Interrupt">Non-PnP: Interrupt number of the parallel por</param>
  1058. /// <returns>A TPCANStatus error code</returns>
  1059. [DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")]
  1060. public static extern TPCANStatus Initialize(
  1061. [MarshalAs(UnmanagedType.U2)]
  1062. TPCANHandle Channel,
  1063. [MarshalAs(UnmanagedType.U2)]
  1064. TPCANBaudrate Btr0Btr1,
  1065. [MarshalAs(UnmanagedType.U1)]
  1066. TPCANType HwType,
  1067. UInt32 IOPort,
  1068. UInt16 Interrupt);
  1069. /// <summary>
  1070. /// Initializes a PCAN Channel
  1071. /// </summary>
  1072. /// <param name="Channel">The handle of a PCAN Channel</param>
  1073. /// <param name="Btr0Btr1">The speed for the communication (BTR0BTR1 code)</param>
  1074. /// <returns>A TPCANStatus error code</returns>
  1075. public static TPCANStatus Initialize(
  1076. TPCANHandle Channel,
  1077. TPCANBaudrate Btr0Btr1)
  1078. {
  1079. return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0);
  1080. }
  1081. /// <summary>
  1082. /// Initializes a FD capable PCAN Channel
  1083. /// </summary>
  1084. /// <param name="Channel">The handle of a FD capable PCAN Channel</param>
  1085. /// <param name="BitrateFD">The speed for the communication (FD bit rate string)</param>
  1086. /// <remarks> See PCAN_BR_* values
  1087. /// Bit rate string must follow the following construction rules:
  1088. /// * parameters and values must be separated by '='
  1089. /// * Couples of Parameter/value must be separated by ','
  1090. /// * Following Parameter must be filled out: f_clock, data_brp, data_sjw, data_tseg1, data_tseg2,
  1091. /// nom_brp, nom_sjw, nom_tseg1, nom_tseg2.
  1092. /// * Following Parameters are optional (not used yet): data_ssp_offset, nom_sam</remarks>
  1093. /// <example>f_clock=80000000,nom_brp=10,nom_tseg1=5,nom_tseg2=2,nom_sjw=1,data_brp=4,data_tseg1=7,data_tseg2=2,data_sjw=1</example>
  1094. /// <returns>A TPCANStatus error code</returns>
  1095. [DllImport("PCANBasic.dll", EntryPoint = "CAN_InitializeFD")]
  1096. public static extern TPCANStatus InitializeFD(
  1097. [MarshalAs(UnmanagedType.U2)]
  1098. TPCANHandle Channel,
  1099. TPCANBitrateFD BitrateFD);
  1100. /// <summary>
  1101. /// Uninitializes one or all PCAN Channels initialized by CAN_Initialize
  1102. /// </summary>
  1103. /// <remarks>Giving the TPCANHandle value "PCAN_NONEBUS",
  1104. /// uninitialize all initialized channels</remarks>
  1105. /// <param name="Channel">The handle of a PCAN Channel</param>
  1106. /// <returns>A TPCANStatus error code</returns>
  1107. [DllImport("PCANBasic.dll", EntryPoint = "CAN_Uninitialize")]
  1108. public static extern TPCANStatus Uninitialize(
  1109. [MarshalAs(UnmanagedType.U2)]
  1110. TPCANHandle Channel);
  1111. /// <summary>
  1112. /// Resets the receive and transmit queues of the PCAN Channel
  1113. /// </summary>
  1114. /// <remarks>A reset of the CAN controller is not performed</remarks>
  1115. /// <param name="Channel">The handle of a PCAN Channel</param>
  1116. /// <returns>A TPCANStatus error code</returns>
  1117. [DllImport("PCANBasic.dll", EntryPoint = "CAN_Reset")]
  1118. public static extern TPCANStatus Reset(
  1119. [MarshalAs(UnmanagedType.U2)]
  1120. TPCANHandle Channel);
  1121. /// <summary>
  1122. /// Gets the current status of a PCAN Channel
  1123. /// </summary>
  1124. /// <param name="Channel">The handle of a PCAN Channel</param>
  1125. /// <returns>A TPCANStatus error code</returns>
  1126. [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetStatus")]
  1127. public static extern TPCANStatus GetStatus(
  1128. [MarshalAs(UnmanagedType.U2)]
  1129. TPCANHandle Channel);
  1130. /// <summary>
  1131. /// Reads a CAN message from the receive queue of a PCAN Channel
  1132. /// </summary>
  1133. /// <param name="Channel">The handle of a PCAN Channel</param>
  1134. /// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
  1135. /// <param name="TimestampBuffer">A TPCANTimestamp structure buffer to get
  1136. /// the reception time of the message</param>
  1137. /// <returns>A TPCANStatus error code</returns>
  1138. [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
  1139. public static extern TPCANStatus Read(
  1140. [MarshalAs(UnmanagedType.U2)]
  1141. TPCANHandle Channel,
  1142. out TPCANMsg MessageBuffer,
  1143. out TPCANTimestamp TimestampBuffer);
  1144. [DllImport("PCANBasic.dll", EntryPoint = "CAN_Read")]
  1145. private static extern TPCANStatus Read(
  1146. [MarshalAs(UnmanagedType.U2)]
  1147. TPCANHandle Channel,
  1148. out TPCANMsg MessageBuffer,
  1149. IntPtr bufferPointer);
  1150. /// <summary>
  1151. /// Reads a CAN message from the receive queue of a PCAN Channel
  1152. /// </summary>
  1153. /// <param name="Channel">The handle of a PCAN Channel</param>
  1154. /// <param name="MessageBuffer">A TPCANMsg structure buffer to store the CAN message</param>
  1155. /// <returns>A TPCANStatus error code</returns>
  1156. public static TPCANStatus Read(
  1157. TPCANHandle Channel,
  1158. out TPCANMsg MessageBuffer)
  1159. {
  1160. return Read(Channel, out MessageBuffer, IntPtr.Zero);
  1161. }
  1162. /// <summary>
  1163. /// Reads a CAN message from the receive queue of a FD capable PCAN Channel
  1164. /// </summary>
  1165. /// <param name="Channel">The handle of a FD capable PCAN Channel</param>
  1166. /// <param name="MessageBuffer">A TPCANMsgFD structure buffer to store the CAN message</param>
  1167. /// <param name="TimestampBuffer">A TPCANTimestampFD buffer to get the
  1168. /// reception time of the message</param>
  1169. /// <returns>A TPCANStatus error code</returns>
  1170. [DllImport("PCANBasic.dll", EntryPoint = "CAN_ReadFD")]
  1171. public static extern TPCANStatus ReadFD(
  1172. [MarshalAs(UnmanagedType.U2)]
  1173. TPCANHandle Channel,
  1174. out TPCANMsgFD MessageBuffer,
  1175. out TPCANTimestampFD TimestampBuffer);
  1176. [DllImport("PCANBasic.dll", EntryPoint = "CAN_ReadFD")]
  1177. private static extern TPCANStatus ReadFD(
  1178. [MarshalAs(UnmanagedType.U2)]
  1179. TPCANHandle Channel,
  1180. out TPCANMsgFD MessageBuffer,
  1181. IntPtr TimestampBuffer);
  1182. /// <summary>
  1183. /// Reads a CAN message from the receive queue of a FD capable PCAN Channel
  1184. /// </summary>
  1185. /// <param name="Channel">The handle of a FD capable PCAN Channel</param>
  1186. /// <param name="MessageBuffer">A TPCANMsgFD structure buffer to store the CAN message</param>
  1187. /// <returns>A TPCANStatus error code</returns>
  1188. public static TPCANStatus ReadFD(
  1189. TPCANHandle Channel,
  1190. out TPCANMsgFD MessageBuffer)
  1191. {
  1192. return ReadFD(Channel, out MessageBuffer, IntPtr.Zero);
  1193. }
  1194. /// <summary>
  1195. /// Transmits a CAN message
  1196. /// </summary>
  1197. /// <param name="Channel">The handle of a PCAN Channel</param>
  1198. /// <param name="MessageBuffer">A TPCANMsg buffer with the message to be sent</param>
  1199. /// <returns>A TPCANStatus error code</returns>
  1200. [DllImport("PCANBasic.dll", EntryPoint = "CAN_Write")]
  1201. public static extern TPCANStatus Write(
  1202. [MarshalAs(UnmanagedType.U2)]
  1203. TPCANHandle Channel,
  1204. ref TPCANMsg MessageBuffer);
  1205. /// <summary>
  1206. /// Transmits a CAN message over a FD capable PCAN Channel
  1207. /// </summary>
  1208. /// <param name="Channel">The handle of a FD capable PCAN Channel</param>
  1209. /// <param name="MessageBuffer">A TPCANMsgFD buffer with the message to be sent</param>
  1210. /// <returns>A TPCANStatus error code</returns>
  1211. [DllImport("PCANBasic.dll", EntryPoint = "CAN_WriteFD")]
  1212. public static extern TPCANStatus WriteFD(
  1213. [MarshalAs(UnmanagedType.U2)]
  1214. TPCANHandle Channel,
  1215. ref TPCANMsgFD MessageBuffer);
  1216. /// <summary>
  1217. /// Configures the reception filter
  1218. /// </summary>
  1219. /// <remarks>The message filter will be expanded with every call to
  1220. /// this function. If it is desired to reset the filter, please use
  1221. /// the 'SetValue' function</remarks>
  1222. /// <param name="Channel">The handle of a PCAN Channel</param>
  1223. /// <param name="FromID">The lowest CAN ID to be received</param>
  1224. /// <param name="ToID">The highest CAN ID to be received</param>
  1225. /// <param name="Mode">Message type, Standard (11-bit identifier) or
  1226. /// Extended (29-bit identifier)</param>
  1227. /// <returns>A TPCANStatus error code</returns>
  1228. [DllImport("PCANBasic.dll", EntryPoint = "CAN_FilterMessages")]
  1229. public static extern TPCANStatus FilterMessages(
  1230. [MarshalAs(UnmanagedType.U2)]
  1231. TPCANHandle Channel,
  1232. UInt32 FromID,
  1233. UInt32 ToID,
  1234. [MarshalAs(UnmanagedType.U1)]
  1235. TPCANMode Mode);
  1236. /// <summary>
  1237. /// Retrieves a PCAN Channel value
  1238. /// </summary>
  1239. /// <remarks>Parameters can be present or not according with the kind
  1240. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1241. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1242. /// <param name="Channel">The handle of a PCAN Channel</param>
  1243. /// <param name="Parameter">The TPCANParameter parameter to get</param>
  1244. /// <param name="StringBuffer">Buffer for the parameter value</param>
  1245. /// <param name="BufferLength">Size in bytes of the buffer</param>
  1246. /// <returns>A TPCANStatus error code</returns>
  1247. [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
  1248. public static extern TPCANStatus GetValue(
  1249. [MarshalAs(UnmanagedType.U2)]
  1250. TPCANHandle Channel,
  1251. [MarshalAs(UnmanagedType.U1)]
  1252. TPCANParameter Parameter,
  1253. StringBuilder StringBuffer,
  1254. UInt32 BufferLength);
  1255. /// <summary>
  1256. /// Retrieves a PCAN Channel value
  1257. /// </summary>
  1258. /// <remarks>Parameters can be present or not according with the kind
  1259. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1260. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1261. /// <param name="Channel">The handle of a PCAN Channel</param>
  1262. /// <param name="Parameter">The TPCANParameter parameter to get</param>
  1263. /// <param name="NumericBuffer">Buffer for the parameter value</param>
  1264. /// <param name="BufferLength">Size in bytes of the buffer</param>
  1265. /// <returns>A TPCANStatus error code</returns>
  1266. [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
  1267. public static extern TPCANStatus GetValue(
  1268. [MarshalAs(UnmanagedType.U2)]
  1269. TPCANHandle Channel,
  1270. [MarshalAs(UnmanagedType.U1)]
  1271. TPCANParameter Parameter,
  1272. out UInt32 NumericBuffer,
  1273. UInt32 BufferLength);
  1274. /// <summary>
  1275. /// Retrieves a PCAN Channel value
  1276. /// </summary>
  1277. /// <remarks>Parameters can be present or not according with the kind
  1278. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1279. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1280. /// <param name="Channel">The handle of a PCAN Channel</param>
  1281. /// <param name="Parameter">The TPCANParameter parameter to get</param>
  1282. /// <param name="NumericBuffer">Buffer for the parameter value</param>
  1283. /// <param name="BufferLength">Size in bytes of the buffer</param>
  1284. /// <returns>A TPCANStatus error code</returns>
  1285. [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
  1286. public static extern TPCANStatus GetValue(
  1287. [MarshalAs(UnmanagedType.U2)]
  1288. TPCANHandle Channel,
  1289. [MarshalAs(UnmanagedType.U1)]
  1290. TPCANParameter Parameter,
  1291. out UInt64 NumericBuffer,
  1292. UInt32 BufferLength);
  1293. [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetValue")]
  1294. private static extern TPCANStatus GetValue(
  1295. [MarshalAs(UnmanagedType.U2)]
  1296. TPCANHandle Channel,
  1297. [MarshalAs(UnmanagedType.U1)]
  1298. TPCANParameter Parameter,
  1299. [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]
  1300. [In, Out]TPCANChannelInformation[] ChannelsBuffer,
  1301. UInt32 BufferLength);
  1302. /// <summary>
  1303. /// Retrieves a PCAN Channel value
  1304. /// </summary>
  1305. /// <remarks>Parameters can be present or not according with the kind
  1306. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1307. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1308. /// <param name="Channel">The handle of a PCAN Channel</param>
  1309. /// <param name="Parameter">The TPCANParameter parameter to get</param>
  1310. /// <param name="ChannelsBuffer">Buffer for the parameter value</param>
  1311. /// <returns>A TPCANStatus error code</returns>
  1312. public static TPCANStatus GetValue(
  1313. TPCANHandle Channel,
  1314. TPCANParameter Parameter,
  1315. TPCANChannelInformation[] ChannelsBuffer)
  1316. {
  1317. if (ChannelsBuffer == null)
  1318. return TPCANStatus.PCAN_ERROR_ILLPARAMVAL;
  1319. return GetValue(Channel, Parameter, ChannelsBuffer, (uint)(ChannelsBuffer.Length * Marshal.SizeOf(typeof(TPCANChannelInformation))));
  1320. }
  1321. /// <summary>
  1322. /// Configures or sets a PCAN Channel value
  1323. /// </summary>
  1324. /// <remarks>Parameters can be present or not according with the kind
  1325. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1326. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1327. /// <param name="Channel">The handle of a PCAN Channel</param>
  1328. /// <param name="Parameter">The TPCANParameter parameter to set</param>
  1329. /// <param name="NumericBuffer">Buffer with the value to be set</param>
  1330. /// <param name="BufferLength">Size in bytes of the buffer</param>
  1331. /// <returns>A TPCANStatus error code</returns>
  1332. [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
  1333. public static extern TPCANStatus SetValue(
  1334. [MarshalAs(UnmanagedType.U2)]
  1335. TPCANHandle Channel,
  1336. [MarshalAs(UnmanagedType.U1)]
  1337. TPCANParameter Parameter,
  1338. ref UInt32 NumericBuffer,
  1339. UInt32 BufferLength);
  1340. /// <summary>
  1341. /// Configures or sets a PCAN Channel value
  1342. /// </summary>
  1343. /// <remarks>Parameters can be present or not according with the kind
  1344. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1345. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1346. /// <param name="Channel">The handle of a PCAN Channel</param>
  1347. /// <param name="Parameter">The TPCANParameter parameter to set</param>
  1348. /// <param name="NumericBuffer">Buffer with the value to be set</param>
  1349. /// <param name="BufferLength">Size in bytes of the buffer</param>
  1350. /// <returns>A TPCANStatus error code</returns>
  1351. [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
  1352. public static extern TPCANStatus SetValue(
  1353. [MarshalAs(UnmanagedType.U2)]
  1354. TPCANHandle Channel,
  1355. [MarshalAs(UnmanagedType.U1)]
  1356. TPCANParameter Parameter,
  1357. ref UInt64 NumericBuffer,
  1358. UInt32 BufferLength);
  1359. /// <summary>
  1360. /// Configures or sets a PCAN Channel value
  1361. /// </summary>
  1362. /// <remarks>Parameters can be present or not according with the kind
  1363. /// of Hardware (PCAN Channel) being used. If a parameter is not available,
  1364. /// a PCAN_ERROR_ILLPARAMTYPE error will be returned</remarks>
  1365. /// <param name="Channel">The handle of a PCAN Channel</param>
  1366. /// <param name="Parameter"></param>
  1367. /// <param name="StringBuffer">Buffer with the value to be set</param>
  1368. /// <param name="BufferLength">Size in bytes of the buffer</param>
  1369. /// <returns>A TPCANStatus error code</returns>
  1370. [DllImport("PCANBasic.dll", EntryPoint = "CAN_SetValue")]
  1371. public static extern TPCANStatus SetValue(
  1372. [MarshalAs(UnmanagedType.U2)]
  1373. TPCANHandle Channel,
  1374. [MarshalAs(UnmanagedType.U1)]
  1375. TPCANParameter Parameter,
  1376. [MarshalAs(UnmanagedType.LPStr,SizeParamIndex=3)]
  1377. string StringBuffer,
  1378. UInt32 BufferLength);
  1379. /// <summary>
  1380. /// Returns a descriptive text of a given TPCANStatus error
  1381. /// code, in any desired language
  1382. /// </summary>
  1383. /// <remarks>The current languages available for translation are:
  1384. /// Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
  1385. /// Italian (0x10) and French (0x0C)</remarks>
  1386. /// <param name="Error">A TPCANStatus error code</param>
  1387. /// <param name="Language">Indicates a 'Primary language ID'</param>
  1388. /// <param name="StringBuffer">Buffer for the text (must be at least 256 in length)</param>
  1389. /// <returns>A TPCANStatus error code</returns>
  1390. [DllImport("PCANBasic.dll", EntryPoint = "CAN_GetErrorText")]
  1391. public static extern TPCANStatus GetErrorText(
  1392. [MarshalAs(UnmanagedType.U4)]
  1393. TPCANStatus Error,
  1394. UInt16 Language,
  1395. StringBuilder StringBuffer);
  1396. #endregion
  1397. }
  1398. #endregion
  1399. }