private void InternalClose() { Win32Com.CancelIo(this.hPort); if (this.rxThread != null) { this.rxThread.Abort(); this.rxThread.Join(100); this.rxThread = null; } Win32Com.CloseHandle(this.hPort); if (this.ptrUWO != IntPtr.Zero) { Marshal.FreeHGlobal(this.ptrUWO); } this.stateRTS = 2; this.stateDTR = 2; this.stateBRK = 2; this.online = false; }
private void ReceiveThread() { uint num; uint num1 = 0; uint num2 = 0; byte[] numArray = new byte[1]; bool flag = true; AutoResetEvent autoResetEvent = new AutoResetEvent(false); Win32Com.OVERLAPPED handle = new Win32Com.OVERLAPPED(); uint num3 = 0; IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(handle)); IntPtr intPtr1 = Marshal.AllocHGlobal(Marshal.SizeOf(num3)); handle.Offset = 0; handle.OffsetHigh = 0; handle.hEvent = autoResetEvent.Handle; Marshal.StructureToPtr(handle, intPtr, true); try { while (Win32Com.SetCommMask(this.hPort, 0x1fd)) { Marshal.WriteInt32(intPtr1, 0); if (flag) { this.startEvent.Set(); flag = false; } if (!Win32Com.WaitCommEvent(this.hPort, intPtr1, intPtr)) { if ((long)Marshal.GetLastWin32Error() != (long)0x3e5) { throw new CommPortException("IO Error [002]"); } else { autoResetEvent.WaitOne(); } } num3 = (uint)Marshal.ReadInt32(intPtr1); if ((num3 & 128) != 0) { if (!Win32Com.ClearCommError(this.hPort, out num1, IntPtr.Zero)) { throw new CommPortException("IO Error [003]"); } else { int num4 = 0; StringBuilder stringBuilder = new StringBuilder("UART Error: ", 40); if ((num1 & 8) != 0) { stringBuilder = stringBuilder.Append("Framing,"); num4++; } if ((num1 & 0x400) != 0) { stringBuilder = stringBuilder.Append("IO,"); num4++; } if ((num1 & 2) != 0) { stringBuilder = stringBuilder.Append("Overrun,"); num4++; } if ((num1 & 1) != 0) { stringBuilder = stringBuilder.Append("Receive Cverflow,"); num4++; } if ((num1 & 4) != 0) { stringBuilder = stringBuilder.Append("Parity,"); num4++; } if ((num1 & 0x100) != 0) { stringBuilder = stringBuilder.Append("Transmit Overflow,"); num4++; } if (num4 <= 0) { if (num1 != 16) { throw new CommPortException("IO Error [003]"); } else { num3 = num3 | 64; } } else { stringBuilder.Length = stringBuilder.Length - 1; throw new CommPortException(stringBuilder.ToString()); } } } if ((num3 & 1) != 0) { do { num = 0; if (Win32Com.ReadFile(this.hPort, numArray, 1, out num, intPtr)) { if (num != 1) { continue; } this.OnRxChar(numArray[0]); } else { Marshal.GetLastWin32Error(); throw new CommPortException("IO Error [004]"); } }while (num > 0); } if ((num3 & 4) != 0) { lock (this.empty) { this.empty[0] = true; } this.OnTxDone(); } if ((num3 & 64) != 0) { this.OnBreak(); } uint num5 = 0; if ((num3 & 8) != 0) { num5 = num5 | 16; } if ((num3 & 16) != 0) { num5 = num5 | 32; } if ((num3 & 32) != 0) { num5 = num5 | 128; } if ((num3 & 0x100) != 0) { num5 = num5 | 64; } if (num5 == 0) { continue; } if (Win32Com.GetCommModemStatus(this.hPort, out num2)) { this.OnStatusChange(new CommBase.ModemStatus(num5), new CommBase.ModemStatus(num2)); } else { throw new CommPortException("IO Error [005]"); } } throw new CommPortException("IO Error [001]"); } catch (Exception exception1) { Exception exception = exception1; Win32Com.CancelIo(this.hPort); if (intPtr1 != IntPtr.Zero) { Marshal.FreeHGlobal(intPtr1); } if (intPtr != IntPtr.Zero) { Marshal.FreeHGlobal(intPtr); } if (exception as ThreadAbortException == null) { this.rxException = exception; this.OnRxException(exception); } } }