CanLibraryClass.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CanTest
  8. {
  9. /// <summary>
  10. /// CAN波特率枚举
  11. /// </summary>
  12. public enum CanBaudRate
  13. {
  14. BaudRate_1M = 0, // 1Mbps
  15. BaudRate_500K = 1, // 500kbps
  16. BaudRate_250K = 2, // 250kbps
  17. BaudRate_125K = 3 // 125kbps
  18. }
  19. unsafe public struct VCI_BOARD_INFO//使用不安全代码
  20. {
  21. public UInt16 hw_Version;
  22. public UInt16 fw_Version;
  23. public UInt16 dr_Version;
  24. public UInt16 in_Version;
  25. public UInt16 irq_Num;
  26. public byte can_Num;
  27. public fixed byte str_Serial_Num[20];
  28. public fixed byte str_hw_Type[40];
  29. public fixed byte Reserved[8];
  30. }
  31. /////////////////////////////////////////////////////
  32. //2.定义CAN信息帧的数据类型。
  33. unsafe public struct VCI_CAN_OBJ //使用不安全代码
  34. {
  35. public uint ID;
  36. public uint TimeStamp; //时间标识
  37. public byte TimeFlag; //是否使用时间标识
  38. public byte SendType; //发送标志。保留,未用
  39. public byte RemoteFlag; //是否是远程帧
  40. public byte ExternFlag; //是否是扩展帧
  41. public byte DataLen; //数据长度
  42. public fixed byte Data[8]; //数据
  43. public fixed byte Reserved[3];//保留位
  44. }
  45. //3.定义初始化CAN的数据类型
  46. public struct VCI_INIT_CONFIG
  47. {
  48. public UInt32 AccCode;
  49. public UInt32 AccMask;
  50. public UInt32 Reserved;
  51. public byte Filter; //0或1接收所有帧。2标准帧滤波,3是扩展帧滤波。
  52. public byte Timing0; //波特率参数,具体配置,请查看二次开发库函数说明书。
  53. public byte Timing1;
  54. public byte Mode; //模式,0表示正常模式,1表示只听模式,2自测模式
  55. }
  56. /*------------其他数据结构描述---------------------------------*/
  57. //4.USB-CAN总线适配器板卡信息的数据类型1,该类型为VCI_FindUsbDevice函数的返回参数。
  58. public struct VCI_BOARD_INFO1
  59. {
  60. public UInt16 hw_Version;
  61. public UInt16 fw_Version;
  62. public UInt16 dr_Version;
  63. public UInt16 in_Version;
  64. public UInt16 irq_Num;
  65. public byte can_Num;
  66. public byte Reserved;
  67. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] str_Serial_Num;
  68. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  69. public byte[] str_hw_Type;
  70. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  71. public byte[] str_Usb_Serial;
  72. }
  73. /*------------数据结构描述完成---------------------------------*/
  74. public struct CHGDESIPANDPORT
  75. {
  76. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
  77. public byte[] szpwd;
  78. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  79. public byte[] szdesip;
  80. public Int32 desport;
  81. public void Init()
  82. {
  83. szpwd = new byte[10];
  84. szdesip = new byte[20];
  85. }
  86. }
  87. public static class CanLibraryClass
  88. {
  89. /*------------兼容ZLG的函数描述---------------------------------*/
  90. [DllImport("controlcan.dll")]
  91. public static extern UInt32 VCI_OpenDevice(UInt32 DeviceType, UInt32 DeviceInd, UInt32 Reserved);
  92. [DllImport("controlcan.dll")]
  93. public static extern UInt32 VCI_CloseDevice(UInt32 DeviceType, UInt32 DeviceInd);
  94. [DllImport("controlcan.dll")]
  95. public static extern UInt32 VCI_InitCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_INIT_CONFIG pInitConfig);
  96. [DllImport("controlcan.dll")]
  97. public static extern UInt32 VCI_ReadBoardInfo(UInt32 DeviceType, UInt32 DeviceInd, ref VCI_BOARD_INFO pInfo);
  98. [DllImport("controlcan.dll")]
  99. public static extern UInt32 VCI_GetReceiveNum(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
  100. [DllImport("controlcan.dll")]
  101. public static extern UInt32 VCI_ClearBuffer(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
  102. [DllImport("controlcan.dll")]
  103. public static extern UInt32 VCI_StartCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
  104. [DllImport("controlcan.dll")]
  105. public static extern UInt32 VCI_ResetCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
  106. [DllImport("controlcan.dll")]
  107. public static extern UInt32 VCI_Transmit(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pSend, UInt32 Len);
  108. [DllImport("controlcan.dll")]
  109. public static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pReceive, UInt32 Len, Int32 WaitTime);
  110. /*------------其他函数描述---------------------------------*/
  111. [DllImport("controlcan.dll")]
  112. public static extern UInt32 VCI_ConnectDevice(UInt32 DevType, UInt32 DevIndex);
  113. [DllImport("controlcan.dll")]
  114. public static extern UInt32 VCI_UsbDeviceReset(UInt32 DevType, UInt32 DevIndex, UInt32 Reserved);
  115. [DllImport("controlcan.dll")]
  116. public static extern UInt32 VCI_FindUsbDevice2(ref VCI_BOARD_INFO pInfo);
  117. }
  118. }