1,221
社区成员
发帖
与我相关
我的任务
分享
this.MyDeviceManagement.WriteReportToDevice(0, outdatas);
this.MyDeviceManagement.ReadReportFromDevice(0, ref inputdatas, 100);
private byte[] ReadAndWriteToDevice()
{
byte[] outdatas = new byte[16];
outdatas[0] = 0x55;
outdatas[1] = 0x2;
outdatas[2] = 0x1;
outdatas[3] = 0x00;
byte[] inputdatas = new byte[192];
byte[] inputs = new byte[8];
inputs[0] = 0xa1;
if (inputs != null && inputs.Length > 0)
{
outdatas = inputs;
}
//this.txtBytesReceived.Clear();
Application.DoEvents();
this.MyDeviceManagement.WriteReportToDevice(0, outdatas);
this.MyDeviceManagement.ReadReportFromDevice(0, ref inputdatas, 100);
//MyDeviceManagement.ReadReportFromDevice1();
Application.DoEvents();
return inputdatas;
}
2.开了线程,写之前加入取消读属性,然后再调用第1步的写,读,程序会停在读的位置
private void button2_Click(object sender, EventArgs e)
{
if (t1.ThreadState == ThreadState.Running)
{
MyDeviceManagement.CancelRead = true;
while (true)
{
System.Threading.Thread.Sleep(5);
if (t1.ThreadState == ThreadState.Stopped)
{
break;
}
}
}
MyDeviceManagement.CancelRead = false;
this.ReadAndWriteToDevice();
t1 = new Thread(MyDeviceManagement.ReadReportFromDevice1);
t1.Start();
}
这是子线程中判断如果取消了,则中断线程
while (true)
{
if (_cancelRead == true)
{
System.Threading.Thread.CurrentThread.Abort();
}
if (this._isStop)
break;
if (!_transferInProgress)
{
break;
}
System.Threading.Thread.Sleep(5);
}
不知道为什么调用Write后,不调用Read,指令出不去?
public class DeviceInformation
{
public Boolean DeviceIsDetected;
public String myDevicePathName;
public HIDD_ATTRIBUTES DeviceAttributes;
public HIDP_CAPS Capabilities;
public IntPtr DeviceNotificationHandle;
public int NumberOfInputBuffers;
public Boolean ExclusiveAccess;
public FileStream FileStreamDeviceData;
public SafeFileHandle HidHandle;
public String HidUsage;
}
private void ReOpenDeviceFileStreamHandler(DeviceInformation findDevice)
{
//关闭句柄并用读/写模式重新打开
findDevice.HidHandle.Close();
findDevice.HidHandle = FileIO.CreateFile(findDevice.myDevicePathName, FileIO.GENERIC_READ | FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);
Debug.WriteLine("在函数ReOpenDeviceFileStreamHandler中" + this.MyDebugging.ResultOfAPICall("CreateFile"));
if (findDevice.HidHandle.IsInvalid)
{
findDevice.ExclusiveAccess = true;
Debug.WriteLine("此设备是一个系统" + findDevice.HidUsage + "." + "对于此设备,Windows 2000 和 Windows XP 才能使用 Input 和 Output reports功能.");
}
else
{
if (findDevice.Capabilities.InputReportByteLength > 0)
{
// Set the size of the Input report buffer.
Byte[] inputReportBuffer = null;
inputReportBuffer = new Byte[findDevice.Capabilities.InputReportByteLength];
findDevice.FileStreamDeviceData = new FileStream(findDevice.HidHandle, FileAccess.Read | FileAccess.Write, inputReportBuffer.Length, false);
}
// Flush any waiting reports in the input buffer. (optional)
_hidObject.FlushQueue(findDevice.HidHandle);
}
}
写数据:
if (device.FileStreamDeviceData.CanWrite)
{
device.FileStreamDeviceData.Write(outputReportBuffer, 0, outputReportBuffer.Length);
success = true;
}
读数据:
ar=device.FileStreamDeviceData.BeginRead(inputReportBuffer, 0, inputReportBuffer.Length, new AsyncCallback(GetInputReportData), inputReportBuffer);
如果现在单调用写指令,然后调用读指令,能正常送出指令和接收返回的指令,但如果不送指令,就收不到下位机的数据。
如果用子线程循环调用读,能正常读出下位机发出的所有数据,但又不能送出指令。