void usbHID_DataReceived(object sender, EventArgs e) { report myRP = (report)e; int i = 0; string str = null; while (i < myRP.reportBuff.Length) { if ((char)myRP.reportBuff[i] != '\0') { str += (char)myRP.reportBuff[i]; } i++; } status = str; Console.WriteLine(str); }
/// <summary> /// 异步读取结束,发出有数据到达事件 /// </summary> /// <param name="iResult">这里是输入报告的数组</param> private void ReadCompleted(IAsyncResult iResult) { byte[] readBuff = (byte[])(iResult.AsyncState); //MessageBox.Show(iResult.AsyncState.ToString()); try { hidDevice.EndRead(iResult);//读取结束,如果读取错误就会产生一个异常 byte[] reportData = new byte[readBuff.Length - 1]; for (int i = 1; i < readBuff.Length; i++) { reportData[i - 1] = readBuff[i]; } report e = new report(readBuff[0], reportData); OnDataReceived(e); //发出数据到达消息 BeginAsyncRead(); //启动下一次读操作 } catch (IOException) //读写错误,设备已经被移除 { EventArgs ex = new EventArgs(); OnDeviceRemoved(ex);//发出设备移除消息 CloseDevice(); } }