设备在我的电脑上outputReportLength读取到的值为64,在win10电脑上读取的为0.无法发送数据到设备,什么原因呢
因为设备管理器中同一个PID vid的设备有多个,但是并不是所有的都支持out传输,到outlength为0时,应该继续查找其他设备。
public HID_RETURN OpenDevice(UInt16 vID, UInt16 pID, string serial)
{
if (deviceOpened == false)
{
//获取连接的HID列表
List<string> deviceList = new List<string>();
GetHidDeviceList(ref deviceList);
if (deviceList.Count == 0)
return HID_RETURN.NO_DEVICE_CONECTED;
for (int i = 0; i < deviceList.Count; i++)
{
if(deviceList[i].IndexOf("vid_045e&pid_0659")>-1)
{
deviceList.RemoveAt(i);
break;
}
}
for (int i = 0; i < deviceList.Count; i++)
{
//IntPtr device = CreateFile(deviceList[i], DESIREDACCESS.GENERIC_READ | DESIREDACCESS.GENERIC_WRITE, 0, 0, CREATIONDISPOSITION.OPEN_EXISTING, 0x40000000, 0);
device = CreateFile(deviceList[i], DESIREDACCESS.GENERIC_READ | DESIREDACCESS.GENERIC_WRITE, 0, 0, CREATIONDISPOSITION.OPEN_EXISTING, 0x40000000, 0);
if (device != INVALID_HANDLE_VALUE)
{
HIDD_ATTRIBUTES attributes;
IntPtr serialBuff = Marshal.AllocHGlobal(512);
HidD_GetAttributes(device, out attributes);
HidD_GetSerialNumberString(device, serialBuff, 512);
string deviceStr = Marshal.PtrToStringAuto(serialBuff);
Marshal.FreeHGlobal(serialBuff);
if (attributes.VendorID == vID && attributes.ProductID == pID)//&& deviceStr == serial)
{
IntPtr preparseData;
HIDP_CAPS caps;
HidD_GetPreparsedData(device, out preparseData);
HidP_GetCaps(preparseData, out caps);
HidD_FreePreparsedData(preparseData);
outputReportLength = caps.OutputReportByteLength;
inputReportLength = caps.InputReportByteLength;
featureReportLength = caps.FeatureReportByteLength;
if (outputReportLength == 0&& inputReportLength==0)
{
outputReportLength = 1;
inputReportLength = 1;
}
if(outputReportLength==0)
{
continue;
}
//if (featureReportLength==0)
//{
// featureReportLength = 1;
//}
hidDevice = new FileStream(new SafeFileHandle(device, false), FileAccess.ReadWrite, inputReportLength, true);
deviceOpened = true;
BeginAsyncRead();
return HID_RETURN.SUCCESS;
}
}
}
return HID_RETURN.DEVICE_NOT_FIND;
}
else
return HID_RETURN.DEVICE_OPENED;
}