/// <summary> /// 写数据到USB端口的文件流 /// </summary> /// <param name="Data">需要写入的数据</param> /// <returns>成功则返回OK,异常则返回DataSendFail</returns> private static COMMUNICATERESULT WriteData(byte[] Data) { try { SaveRecDataList.Clear(); //m_oFile.Flush(); Thread.Sleep(100); m_oFile.Write(Data, 0, Data.Length); } catch (Exception ee) { // The device was removed! HidExist = false; int jj = Marshal.GetLastWin32Error(); return(COMMUNICATERESULT.DataSendFail); } return(COMMUNICATERESULT.OK); }
/// <summary> /// Callback for above. Care with this as it will be called on the background thread from the async read /// </summary> /// <param name="iResult">Async result parameter</param> protected static void ReadCompleted(IAsyncResult iResult) { byte[] arrBuff = (byte[])iResult.AsyncState; // retrieve the read buffer byte[] tempbyte = new byte[8]; for (int i = 0; i < tempbyte.Length; i++) { tempbyte[i] = arrBuff[i + 1]; } SaveRecDataList.AddRange(tempbyte); try { m_oFile.EndRead(iResult); // call end read : this throws any exceptions that happened during the read BeginAsyncRead(); // when all that is done, kick off another read for the next report } catch (IOException ex) // if we got an IO exception, the device was removed { HidExist = false; } }