CanLibraryClass.cs 5.0 KB

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