private void pHandleOverlappedRead(int Bytes2Read) { this.muOverlapped.hEvent = Rs232.CreateEvent(0, 1, 0, (string)null); if (this.muOverlapped.hEvent == 0) { throw new ApplicationException("Error creating event for overlapped read."); } int num = 0; if (!this.mbWaitOnRead) { this.mabtRxBuf = new byte[checked (Bytes2Read - 1 + 1)]; if (Rs232.ReadFile(this.mhRS, this.mabtRxBuf, Bytes2Read, ref num, ref this.muOverlapped) == 0) { int lastError = Rs232.GetLastError(); if (lastError != 997) { throw new ArgumentException("Overlapped Read Error: " + this.pErr2Text(lastError)); } this.mbWaitOnRead = true; } //else if (this.DataReceivedEvent != null) // this.DataReceivedEvent(this, this.mabtRxBuf); } if (!this.mbWaitOnRead) { return; } switch (Rs232.WaitForSingleObject(this.muOverlapped.hEvent, this.miTimeout)) { case 0: if (Rs232.GetOverlappedResult(this.mhRS, ref this.muOverlapped, ref num, 0) == 0) { int lastError = Rs232.GetLastError(); if (lastError == 996) { throw new ApplicationException("Read operation incomplete"); } throw new ApplicationException("Read operation error " + lastError.ToString()); } //if (this.DataReceivedEvent != null) // this.DataReceivedEvent(this, this.mabtRxBuf); this.mbWaitOnRead = false; break; case 258: throw new Rs232.IOTimeoutException("Timeout error"); default: throw new ApplicationException("Overlapped read error"); } }
private bool pHandleOverlappedWrite(byte[] Buffer) { this.muOverlappedW.hEvent = Rs232.CreateEvent(0, 1, 0, (string)null); if (this.muOverlappedW.hEvent == 0) { throw new ApplicationException("Error creating event for overlapped write."); } Rs232.PurgeComm(this.mhRS, 12); this.mbWaitOnRead = true; int num = 0; bool flag = false; if (Rs232.WriteFile(this.mhRS, Buffer, Buffer.Length, ref num, ref this.muOverlappedW) == 0) { int lastError = Rs232.GetLastError(); if (lastError != 997) { throw new ArgumentException("Overlapped Read Error: " + this.pErr2Text(lastError)); } if (Rs232.WaitForSingleObject(this.muOverlappedW.hEvent, -1) == 0) { if (Rs232.GetOverlappedResult(this.mhRS, ref this.muOverlappedW, ref num, 0) == 0) { flag = true; } else { this.mbWaitOnRead = false; //if (this.TxCompletedEvent != null) // this.TxCompletedEvent(this); } } } else { flag = false; } Rs232.CloseHandle(this.muOverlappedW.hEvent); return(flag); }