public int Read(int Bytes2Read) { if (Bytes2Read == 0) { Bytes2Read = this.miBufferSize; } if (this.mhRS == -1) { throw new ApplicationException("Please initialize and open port before using this method"); } int num1 = 0; try { if (this.meMode == Rs232.Mode.Overlapped) { this.pHandleOverlappedRead(Bytes2Read); } else { this.mabtRxBuf = new byte[checked (Bytes2Read - 1 + 1)]; int mhRs = this.mhRS; byte[] mabtRxBuf = this.mabtRxBuf; int nNumberOfBytesToRead = Bytes2Read; int num2 = 0; // ISSUE: explicit reference operation // ISSUE: variable of a reference type int lpNumberOfBytesRead = @num2; Rs232.OVERLAPPED overlapped = (Rs232.OVERLAPPED)((object)null ?? Activator.CreateInstance(typeof(Rs232.OVERLAPPED))); // ISSUE: explicit reference operation // ISSUE: variable of a reference type Rs232.OVERLAPPED lpOverlapped = @overlapped; int num3 = Rs232.ReadFile(mhRs, mabtRxBuf, nNumberOfBytesToRead, ref lpNumberOfBytesRead, ref lpOverlapped); if (num3 == 0) { throw new ApplicationException("ReadFile error " + num3.ToString()); } if (num2 < Bytes2Read) { throw new Rs232.IOTimeoutException("Timeout error"); } this.mbWaitOnRead = true; num1 = num2; } } catch (Exception ex) { ProjectData.SetProjectError(ex); Exception innerException = ex; throw new ApplicationException("Read Error: " + innerException.Message, innerException); } return(num1); }
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"); } }