|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
//注意:使用这些函数需要1.5.30及以上的固件才支持
|
|
|
|
|
|
namespace USB2XXX
|
|
|
|
|
|
{
|
|
|
|
|
|
class USB2ELINS
|
|
|
|
|
|
{
|
|
|
|
|
|
//定义函数返回错误代码
|
|
|
|
|
|
public const int ELINS_SUCCESS = (0); //函数执行成功
|
|
|
|
|
|
public const int ELINS_ERR_NOT_SUPPORT = (-1); //适配器不支持该函数
|
|
|
|
|
|
public const int ELINS_ERR_USB_WRITE_FAIL = (-2); //USB写数据失败
|
|
|
|
|
|
public const int ELINS_ERR_USB_READ_FAIL = (-3); //USB读数据失败
|
|
|
|
|
|
public const int ELINS_ERR_CMD_FAIL = (-4); //命令执行失败
|
|
|
|
|
|
public const int ELINS_ERR_PARAMETER = (-5); //参数错误
|
|
|
|
|
|
//命令值宏定义
|
|
|
|
|
|
public const byte ELINS_CMD_SDW = 2; //Single Device Write
|
|
|
|
|
|
public const byte ELINS_CMD_SDR = 3; //Single Device Read
|
|
|
|
|
|
public const byte ELINS_CMD_BW = 4; //Broadcast Write
|
|
|
|
|
|
public const byte ELINS_CMD_EBW = 5; //Enhance Broadcast Write
|
|
|
|
|
|
public const byte ELINS_CMD_ESDW = 6; //Enhance Single Device Write
|
|
|
|
|
|
public const byte ELINS_CMD_ESDR = 7; //Enhance Single Device Read
|
|
|
|
|
|
//消息状态定义
|
|
|
|
|
|
public const byte ELINS_STATUS_NACK = 0x01;//写无应答(非广播帧)
|
|
|
|
|
|
public const byte ELINS_STATUS_RNRES = 0x02;//读数据无应答
|
|
|
|
|
|
public const byte ELINS_STATUS_ECRC = 0x04;//CRC校验错误
|
|
|
|
|
|
public const byte ELINS_STATUS_FERR = 0x08;//帧异常,实际字节数跟规定的不匹配
|
|
|
|
|
|
public const byte ELINS_STATUS_DLEN = 0x10;//实际数据字节数跟帧头字节数不匹配
|
|
|
|
|
|
|
|
|
|
|
|
//ELINS帧类型定义
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
|
|
|
|
public struct ELINS_MSG
|
|
|
|
|
|
{
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte DataLen;//Data域中有效数据字节数
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte BreakBits; //发送同步间隔宽度
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte Status; //当前帧状态指示,比如帧数据异常可以在这里显示
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte Flags; ///<bit[0..1]表示通道号
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte SYNC; ///<固定为0x55
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte TimeStampHigh; //时间戳高位
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
|
|
|
|
public short MsgSendTimes; //当前帧发送次数
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U4)]
|
|
|
|
|
|
public uint TimeStamp; //时间戳低位,单位为10us
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte CmdCode; ///<命令
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public byte DevID;
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
|
|
|
|
public short RegAddr;
|
|
|
|
|
|
[MarshalAs(UnmanagedType.U2)]
|
|
|
|
|
|
public short Crc16;
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64, ArraySubType = UnmanagedType.U1)]
|
|
|
|
|
|
public byte[] Data; //数据
|
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U1)]
|
|
|
|
|
|
public byte[] ACKValue; //应答值
|
|
|
|
|
|
}
|
|
|
|
|
|
[DllImport("USB2XXX.dll")]
|
|
|
|
|
|
public static extern int ELINS_Init(int DevHandle, byte Index, uint BaudRate, byte MasterMode, byte ResEnable, byte Ver);
|
|
|
|
|
|
[DllImport("USB2XXX.dll")]
|
|
|
|
|
|
public static extern int ELINS_MasterStartSch(int DevHandle, byte Index, ELINS_MSG[] pMsg, uint MsgLen, uint SendTimes, byte ReadBackFlag);
|
|
|
|
|
|
[DllImport("USB2XXX.dll")]
|
|
|
|
|
|
public static extern int ELINS_GetMsg(int DevHandle, byte Index, IntPtr pMsg, uint BufferSize);
|
|
|
|
|
|
[DllImport("USB2XXX.dll")]
|
|
|
|
|
|
public static extern int ELINS_MasterStopSch(int DevHandle, byte Index);
|
|
|
|
|
|
[DllImport("USB2XXX.dll")]
|
|
|
|
|
|
public static extern int ELINS_Stop(int DevHandle, byte Index);
|
|
|
|
|
|
[DllImport("USB2XXX.dll")]
|
|
|
|
|
|
public static extern int ELINS_GetStartTime(int DevHandle, byte Index);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|