HidLibrary
HidLibrary copied to clipboard
HidLibrary cannot write data on Windows 10
Hello everyone, On my project I want to write data to PSoC device. I can connect and read datas but write. Here is my code:
private void sendData_Click(object sender, EventArgs e)
{
byte[] outData = new byte[_selectedDevice.Capabilities.OutputReportByteLength];
outData[0] = Convert.ToByte(reportID.Text);
outData[1] = Convert.ToByte(sData_1.Text);
outData[2] = Convert.ToByte(sData_2.Text);
outData[3] = Convert.ToByte(sData_3.Text);
outData[4] = Convert.ToByte(sData_4.Text);
outData[5] = Convert.ToByte(sData_5.Text);
outData[6] = Convert.ToByte(sData_6.Text);
outData[7] = Convert.ToByte(sData_7.Text);
outData[8] = Convert.ToByte(sData_8.Text);
_deviceList[Devices.SelectedIndex].Write(outData);
}
First byte is report ID and follows by 8 bytes of datas. On windows 7 I can write data with UsbLibrary.dll. But I need Windows 10 support.
I and you the opposite, I can write data to the device in WIN10 system, but WIN7 system can not write
I have the same problem on Win10, WriteReport method isn't executed until some IN report from the device comes. (DotNet Framework 4.6.1, HID generic device (custom defined))
I was able to solve problem with WriteReport on Windows 10 DotNet Framework 4.6.2 by setting different file handles for Read and Write operations:
HidDevice.cs
public IntPtr ReadHandle { get; private set; }
public IntPtr WriteHandle { get; private set; }
ReadHandle = OpenDeviceIO(_devicePath, readMode, NativeMethods.GENERIC_READ, shareMode);
WriteHandle = OpenDeviceIO(_devicePath, writeMode, NativeMethods.GENERIC_WRITE, shareMode);
IHidDevice.cs
IntPtr ReadHandle { get; }
IntPtr WriteHandle { get; }
instead of
HidDevice.cs
public IntPtr Handle { get; private set; }
Handle = OpenDeviceIO(_devicePath, readMode, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, shareMode);
IHidDevice.cs
IntPtr Handle { get; }
and of course i had to set corresponding handle for every method where it needed, for example:
public HidReport ReadReportSync(byte reportId)
{
byte[] cmdBuffer = new byte[Capabilities.InputReportByteLength];
cmdBuffer[0] = reportId;
// ReadHandle is used here instead of Handle
bool bSuccess = NativeMethods.HidD_GetInputReport(ReadHandle, cmdBuffer, cmdBuffer.Length);
HidDeviceData deviceData = new HidDeviceData(cmdBuffer, bSuccess ? HidDeviceData.ReadStatus.Success : HidDeviceData.ReadStatus.NoDataRead);
return new HidReport(Capabilities.InputReportByteLength, deviceData);
}