using System;
using System.Runtime.InteropServices;
namespace USBCAN
{
public sealed class CAN_API
{
#region 数据类型定义
///
/// 接口卡类型定义
///
public enum PCIDeviceType
{
VCI_PCI5121 = 1,
VCI_PCI9810 = 2,
VCI_USBCAN1 = 3,
VCI_USBCAN2 = 4,
VCI_PCI9820 = 5,
VCI_CAN232 = 6,
VCI_PCI5110 = 7,
VCI_CANLITE = 8,
VCI_ISA9620 = 9,
VCI_ISA5420 = 10,
VCI_PC104CAN = 11,
VCI_CANETE = 12,
VCI_DNP9810 = 13,
VCI_PCI9840 = 14,
VCI_PCI9820I = 16
}
//函数调用返回状态值
///
/// 正常状态
///
public static readonly int STATUS_OK = 1;
///
/// 发生错误
///
public static readonly int STATUS_ERR = 0;
///
/// 错误类型
///
public enum ErrorType
{
// --------------- CAN错误码 -------------------
///
/// CAN错误码:CAN控制器内部FIFO溢出
///
ERR_CAN_OVERFLOW = 0x0001,
///
/// CAN错误码:CAN控制器错误报警
///
ERR_CAN_ERRALARM = 0x0002,
///
/// CAN错误码:CAN控制器消极错误
///
ERR_CAN_PASSIVE = 0x0004,
///
/// CAN错误码:CAN控制器仲裁丢失
///
ERR_CAN_LOSE = 0x0008,
///
/// CAN错误码:CAN控制器总线错误
///
ERR_CAN_BUSERR = 0x0010,
// --------------- 通用错误码 -------------------
///
/// 通用错误码:设备已经打开
///
ERR_DEVICEOPENED = 0x0100,
///
/// 通用错误码:打开设备错误
///
ERR_DEVICEOPEN = 0x0200,
///
/// 通用错误码:设备没有打开
///
ERR_DEVICENOTOPEN = 0x0400,
///
/// 通用错误码:缓冲区溢出
///
ERR_BUFFEROVERFLOW = 0x0800,
///
/// 通用错误码:此设备不存在
///
ERR_DEVICENOTEXIST = 0x1000,
///
/// 通用错误码:装载动态库失败
///
ERR_LOADKERNELDLL = 0x2000,
///
/// 通用错误码:执行命令失败错误码
///
ERR_CMDFAILED = 0x4000,
///
/// 通用错误码:内存不足
///
ERR_BUFFERCREATE = 0x8000
}
///
/// ZLGCAN系列接口卡信息
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct VCI_BOARD_INFO
{
public ushort hw_Version;
public ushort fw_Version;
public ushort dr_Version;
public ushort in_Version;
public ushort irq_Num;
public byte can_Num;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 20)]
public string str_Serial_Num;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 40)]
public string str_hw_Type;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = System.Runtime.InteropServices.UnmanagedType.U2)]
public ushort[] Reserved;
}
///
/// CAN信息帧
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
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;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = UnmanagedType.I1)]
public byte[] Data;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.I1)]
public byte[] Reserved;
}
///
/// CAN控制器状态
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct VCI_CAN_STATUS
{
public byte ErrInterrupt;
public byte regMode;
public byte regStatus;
public byte regALCapture;
public byte regECCapture;
public byte regEWLimit;
public byte regRECounter;
public byte regTECounter;
public uint Reserved;
}
///
/// 错误信息
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct VCI_ERR_INFO
{
public uint ErrCode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.I1)]
public byte[] Passive_ErrData;
public byte ArLost_ErrData;
}
///
/// CAN波特率枚举
///
public enum CAN_BaudRate
{
BaudRate_1000kbps = 0,
BaudRate_800kbps,
BaudRate_500kbps,
BaudRate_250kbps,
BaudRate_100kbps,
BaudRate_50kbps,
BaudRate_20kbps,
BaudRate_10kbps,
BaudRate_5kbps,
}
///
/// 波特率对应Timing0值
///
public static readonly byte[] VCI_INIT_CONFIG_Timing0 =
{
0x00,
0x00,
0x00,
0x01,
0x04,
0x09,
0x18,
0x31,
0xBF
};
///
/// 波特率对应Timing0值
///
public static readonly byte[] VCI_INIT_CONFIG_Timing1 =
{
0x14,
0x16,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0xFF
};
///
/// 初始化CAN的配置信息
///
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct VCI_INIT_CONFIG
{
///
/// 验收码
///
public uint AccCode;
///
/// 屏蔽码
///
public uint AccMask;
///
/// 预留,填0
///
public uint Reserved;
///
/// 滤波方式 01
///
public byte Filter;
///
/// 波特率对应值
/// 1000kbps :0x00
/// 800kbps :0x00
/// 500kbps :0x00
/// 250kbps :0x01
/// 100kbps :0x04
/// 50kbps :0x09
/// 20kbps :0x18
/// 10kbps :0x31
/// 5kbps :0xBF
///
public byte Timing0;
///
/// 波特率对应值
/// 1000kbps :0x14
/// 800kbps :0x16
/// 500kbps :0x1C
/// 250kbps :0x1C
/// 100kbps :0x1C
/// 50kbps :0x1C
/// 20kbps :0x1C
/// 10kbps :0x1C
/// 5kbps :0xFF
///
public byte Timing1;
///
/// 模式:0:正常|1:只听
///
public byte Mode;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CHGDESIPANDPORT
{
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 10)]
public string szpwd;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 20)]
public string szdesip;
public int desport;
}
#endregion
#region API函数
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_OpenDevice", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_OpenDevice(uint DeviceType, uint DeviceInd, uint Reserved);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_CloseDevice", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_CloseDevice(uint DeviceType, uint DeviceInd);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_ResetCAN", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_ResetCAN(uint DeviceType, uint DeviceInd, uint CANInd);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_InitCAN", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_InitCAN(uint DeviceType, uint DeviceInd, uint CANInd, ref VCI_INIT_CONFIG pInitConfig);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_Transmit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_Transmit(uint DeviceType, uint DeviceInd, uint CANInd, ref VCI_CAN_OBJ pSend, uint Len);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_Receive", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_Receive(uint DeviceType, uint DeviceInd, uint CANInd, IntPtr pReceive, uint Len, int WaitTime);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_GetReceiveNum", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_GetReceiveNum(uint DeviceType, uint DeviceInd, uint CANInd);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_ClearBuffer", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_ClearBuffer(uint DeviceType, uint DeviceInd, uint CANInd);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_ReadErrInfo", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_ReadErrInfo(uint DeviceType, uint DeviceInd, uint CANInd, ref VCI_ERR_INFO pErrInfo);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_StartCAN", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_StartCAN(uint DeviceType, uint DeviceInd, uint CANInd);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_SetReference", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_SetReference(uint DeviceType, uint DeviceInd, uint CANInd, uint RefType, object pData);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_GetReference", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_GetReference(uint DeviceType, uint DeviceInd, uint CANInd, uint RefType, object pData);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_ReadCANStatus", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_ReadCANStatus(uint DeviceType, uint DeviceInd, uint CANInd, ref VCI_CAN_STATUS pCANStatus);
[DllImport("ControlCAN.dll", SetLastError = true, EntryPoint = "VCI_ReadBoardInfo", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint VCI_ReadBoardInfo(uint DeviceType, uint DeviceInd, ref VCI_BOARD_INFO pInfo);
#endregion
}
}