CanLibraryClass.cs 5.1 KB

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