USB设备通讯的问题(写数据),已附上源码
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_GetInputReport(int hidDeviceObject, byte[] buffer, int bufferLength);
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_SetOutputReport(int hidDeviceObject, byte[] buffer, int bufferLength);
[DllImport("hid.dll")]
private unsafe static extern Boolean HidD_GetSerialNumberString(int hidDeviceObject, IntPtr buffer, int bufferLength);
[DllImport("user32.dll", SetLastError = true)]
public unsafe static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, Int32 Flags);
[DllImport("hid.dll")]
public static extern bool HidD_FlushQueue(int HidDeviceObject);
[DllImport("hid.dll")]
public unsafe static extern void HidD_GetHidGuid(ref Guid HidGuid);
[DllImport("setupapi.dll", SetLastError = true)]
public unsafe static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public unsafe static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
[StructLayout(LayoutKind.Sequential)]
public unsafe class DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
public short dbcc_name;
}
public unsafe struct SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid interfaceClassGuid;
public int flags;
public int reserved;
}
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
private unsafe static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData,
int deviceInterfaceDetailDataSize, ref int requiredSize, SP_DEVINFO_DATA deviceInfoData);
[StructLayout(LayoutKind.Sequential)]
public unsafe class SP_DEVINFO_DATA
{
public int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
public Guid classGuid = Guid.Empty; // temp
public int devInst = 0; // dumy
public int reserved = 0;
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]
internal unsafe struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
internal int cbSize;
internal short devicePath;
}
public enum DIGCF
{
DIGCF_DEFAULT = 0x1,
DIGCF_PRESENT = 0x2,
DIGCF_ALLCLASSES = 0x4,
DIGCF_PROFILE = 0x8,
DIGCF_DEVICEINTERFACE = 0x10
}
[DllImport("Kernel32.dll", SetLastError = true)]
private unsafe static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
ref uint lpNumberOfBytesWritten,
IntPtr lpOverlapped
);
//獲取設備文件
[DllImport("kernel32.dll", SetLastError = true)]
private unsafe static extern int CreateFile(
string lpFileName, // file name
uint dwDesiredAccess, // access mode
uint dwShareMode, // share mode
uint lpSecurityAttributes, // SD
uint dwCreationDisposition, // how to create
uint dwFlagsAndAttributes, // file attributes
uint hTemplateFile // handle to template file
);
[DllImport("kernel32.dll")]
public unsafe static extern int CloseHandle(IntPtr hObject);
[DllImport("setupapi.dll", SetLastError = true)]
internal unsafe static extern IntPtr SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("hid.dll", SetLastError = true)]
public unsafe static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
[DllImport("hid.dll", SetLastError = true)]
public unsafe static extern int HidD_GetPreparsedData(int HidHandle, ref int preparsedDataPointer);
[DllImport("hid.dll", SetLastError = true)]
private unsafe static extern int HidP_GetCaps(int pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS);
public struct HIDD_ATTRIBUTES //HID屬性
{
public Int32 Size;
public Int32 VendorID;
public Int32 ProductID;
public Int32 VersionNumber;
}
[StructLayout(LayoutKind.Sequential)]
public struct HIDP_CAPS
{
public UInt16 Usage;
public UInt16 UsagePage;
public UInt16 InputReportByteLength;
public UInt16 OutputReportByteLength;
public UInt16 FeatureReportByteLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
public UInt16[] Reserved;
public UInt16 NumberLinkCollectionNodes;
public UInt16 NumberInputButtonCaps;
public UInt16 NumberInputValueCaps;
public UInt16 NumberInputDataIndices;
public UInt16 NumberOutputButtonCaps;
public UInt16 NumberOutputValueCaps;
public UInt16 NumberOutputDatadices;
public UInt16 NumberFeatureButtonCaps;
public UInt16 NumberFeatureValueCaps;
public UInt16 NumberFeatureDataIndices;
}