You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
4.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
//注意使用这些函数需要1.8.4及以上的固件才支持
namespace USB2XXX
{
class USB2MELIBU
{
//定义函数返回错误代码
public const int MELIBU_SUCCESS = (0); //函数执行成功
public const int MELIBU_ERR_NOT_SUPPORT = (-1); //适配器不支持该函数
public const int MELIBU_ERR_USB_WRITE_FAIL = (-2); //USB写数据失败
public const int MELIBU_ERR_USB_READ_FAIL = (-3); //USB读数据失败
public const int MELIBU_ERR_CMD_FAIL = (-4); //命令执行失败
public const int MELIBU_ERR_PARAMETER = (-5); //参数错误
//命令值宏定义
public const byte MELIBU_TX = 0; //主机发送数据给从机
public const byte MELIBU_RX = 1; //主机向从机读数据
//消息状态定义
public const byte MELIBU_STATUS_NACK = 0x01;//写无应答(非广播帧)
public const byte MELIBU_STATUS_RNRES = 0x02;//读数据无应答
public const byte MELIBU_STATUS_ECRC = 0x04;//CRC校验错误
public const byte MELIBU_STATUS_FERR = 0x08;//帧异常,实际字节数跟规定的不匹配
public const byte MELIBU_STATUS_DLEN = 0x10;//实际数据字节数跟帧头字节数不匹配
public const byte MELIBU_STATUS_PERR = 0x20;//P0 P1错误
//ELINS帧类型定义
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MELIBU_MSG
{
[MarshalAs(UnmanagedType.U1)]
public byte DataLen; //Data域中有效数据字节数
[MarshalAs(UnmanagedType.U1)]
public byte BreakBits; //发送同步间隔宽度一般为13
[MarshalAs(UnmanagedType.U1)]
public byte Status; //当前帧状态指示,接收时有效,指示帧异常状态
[MarshalAs(UnmanagedType.U1)]
public byte ACKValue; //应答值,发送数据时存储对方应答数据
[MarshalAs(UnmanagedType.U1)]
public byte MsgMode; //0-普通模式1-扩展模式
[MarshalAs(UnmanagedType.U1)]
public byte TimeStampHigh; //时间戳高位
[MarshalAs(UnmanagedType.U2)]
public short MsgSendTimes; //当前帧发送次数
[MarshalAs(UnmanagedType.U4)]
public uint TimeStamp; //接收帧时为时间戳低位单位为10us发送数据时为跟下一帧的间隔时间单位为微秒(us)
[MarshalAs(UnmanagedType.U2)]
public ushort ID; //ID值可以通过调用 @ref MeLiBu_GetID 函数获取
[MarshalAs(UnmanagedType.U2)]
public ushort Crc16; //发送数据时不用填底层会自动计算接收时代表接收到的CRC值
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128, ArraySubType = UnmanagedType.U1)]
public byte[] Data; //数据存储数组里面包含的有效数据字节数由DataLen决定
}
[DllImport("USB2XXX.dll")]
public static extern int MeLiBu_Init(int DevHandle, byte Index, uint BaudRate, byte MasterMode, byte ResEnable);
[DllImport("USB2XXX.dll")]
public static extern int MeLiBu_MasterStartSch(int DevHandle, byte Index, MELIBU_MSG[] pMsg, uint MsgLen, uint SendTimes, byte ReadBackFlag);
[DllImport("USB2XXX.dll")]
public static extern int MeLiBu_GetMsg(int DevHandle, byte Index, IntPtr pMsg, uint BufferSize);
[DllImport("USB2XXX.dll")]
public static extern ushort MeLiBu_GetID(byte MsgMode, byte SlaveAddr, byte RT, byte F, byte dlc,byte InstrExt);
[DllImport("USB2XXX.dll")]
public static extern int MeLiBu_MasterStopSch(int DevHandle, byte Index);
[DllImport("USB2XXX.dll")]
public static extern int MeLiBu_Stop(int DevHandle, byte Index);
[DllImport("USB2XXX.dll")]
public static extern int MeLiBu_GetStartTime(int DevHandle, byte Index);
}
}