| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- //创芯科技CAN卡的引用库,需要安装对应驱动
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace CCDCount.DLL.CanBus
- {
- unsafe public struct VCI_BOARD_INFO//使用不安全代码
- {
- public UInt16 hw_Version;
- public UInt16 fw_Version;
- public UInt16 dr_Version;
- public UInt16 in_Version;
- public UInt16 irq_Num;
- public byte can_Num;
- public fixed byte str_Serial_Num[20];
- public fixed byte str_hw_Type[40];
- public fixed byte Reserved[8];
- }
- /////////////////////////////////////////////////////
- //2.定义CAN信息帧的数据类型。
- unsafe public struct VCI_CAN_OBJ //使用不安全代码
- {
- public uint ID;
- public uint TimeStamp; //时间标识
- public byte TimeFlag; //是否使用时间标识
- public byte SendType; //发送标志。保留,未用
- public byte RemoteFlag; //是否是远程帧
- public byte ExternFlag; //是否是扩展帧
- public byte DataLen; //数据长度
- public fixed byte Data[8]; //数据
- public fixed byte Reserved[3];//保留位
- }
- //3.定义初始化CAN的数据类型
- public struct VCI_INIT_CONFIG
- {
- public UInt32 AccCode;
- public UInt32 AccMask;
- public UInt32 Reserved;
- public byte Filter; //0或1接收所有帧。2标准帧滤波,3是扩展帧滤波。
- public byte Timing0; //波特率参数,具体配置,请查看二次开发库函数说明书。
- public byte Timing1;
- public byte Mode; //模式,0表示正常模式,1表示只听模式,2自测模式
- }
- /*------------其他数据结构描述---------------------------------*/
- //4.USB-CAN总线适配器板卡信息的数据类型1,该类型为VCI_FindUsbDevice函数的返回参数。
- public struct VCI_BOARD_INFO1
- {
- public UInt16 hw_Version;
- public UInt16 fw_Version;
- public UInt16 dr_Version;
- public UInt16 in_Version;
- public UInt16 irq_Num;
- public byte can_Num;
- public byte Reserved;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] str_Serial_Num;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
- public byte[] str_hw_Type;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
- public byte[] str_Usb_Serial;
- }
- /*------------数据结构描述完成---------------------------------*/
- public struct CHGDESIPANDPORT
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
- public byte[] szpwd;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
- public byte[] szdesip;
- public Int32 desport;
- public void Init()
- {
- szpwd = new byte[10];
- szdesip = new byte[20];
- }
- }
- public static class CanLibraryClass
- {
- /*------------兼容ZLG的函数描述---------------------------------*/
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_OpenDevice(UInt32 DeviceType, UInt32 DeviceInd, UInt32 Reserved);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_CloseDevice(UInt32 DeviceType, UInt32 DeviceInd);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_InitCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_INIT_CONFIG pInitConfig);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_ReadBoardInfo(UInt32 DeviceType, UInt32 DeviceInd, ref VCI_BOARD_INFO pInfo);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_GetReceiveNum(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_ClearBuffer(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_StartCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_ResetCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_Transmit(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pSend, UInt32 Len);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pReceive, UInt32 Len, Int32 WaitTime);
- /*------------其他函数描述---------------------------------*/
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_ConnectDevice(UInt32 DevType, UInt32 DevIndex);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_UsbDeviceReset(UInt32 DevType, UInt32 DevIndex, UInt32 Reserved);
- [DllImport("controlcan.dll")]
- public static extern UInt32 VCI_FindUsbDevice2(ref VCI_BOARD_INFO pInfo);
- }
- }
|