public Int32 ReadCom(byte[] toSend, int uiCount) { bool bRes = false, bComEvent; UInt32 retlen; bComEvent = Win32Com.SetCommMask(_hPort, EV_RXCHAR); if (bComEvent) { bRes = ReadFile(_hPort, // handle of file to read toSend, // pointer to buffer that receives data (uint)uiCount, // number of bytes to read out retlen, // pointer to number of bytes read IntPtr.Zero // pointer to structure for data ); if (!(bRes)) { return(0); } if (retlen > 0) //If we have data { return((Int32)retlen); //return the length } else { return(0); //else no data has been read } } else { return(0); } }
private void ReceiveThread() { var buf = new Byte[1]; var sg = new AutoResetEvent(false); var ov = new OVERLAPPED(); var unmanagedOv = Marshal.AllocHGlobal(Marshal.SizeOf(ov)); ov.Offset = 0; ov.OffsetHigh = 0; ov.hEvent = sg.Handle; Marshal.StructureToPtr(ov, unmanagedOv, true); uint eventMask = 0; var uMask = Marshal.AllocHGlobal(Marshal.SizeOf(eventMask)); try { while (true) { if (!Win32Com.SetCommMask(_hPort, Win32Com.EV_RXCHAR | Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR | Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING | Win32Com.EV_ERR)) { throw new CommPortException("IO Error [001]"); } Marshal.WriteInt32(uMask, 0); if (!Win32Com.WaitCommEvent(_hPort, uMask, unmanagedOv)) { if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) { sg.WaitOne(); } else { throw new CommPortException("IO Error [002]"); } } eventMask = (uint)Marshal.ReadInt32(uMask); if ((eventMask & Win32Com.EV_ERR) != 0) { UInt32 errs; if (Win32Com.ClearCommError(_hPort, out errs, IntPtr.Zero)) { var s = new StringBuilder("UART Error: ", 40); if ((errs & Win32Com.CE_FRAME) != 0) { s = s.Append("Framing,"); } if ((errs & Win32Com.CE_IOE) != 0) { s = s.Append("IO,"); } if ((errs & Win32Com.CE_OVERRUN) != 0) { s = s.Append("Overrun,"); } if ((errs & Win32Com.CE_RXOVER) != 0) { s = s.Append("Receive Cverflow,"); } if ((errs & Win32Com.CE_RXPARITY) != 0) { s = s.Append("Parity,"); } if ((errs & Win32Com.CE_TXFULL) != 0) { s = s.Append("Transmit Overflow,"); } s.Length = s.Length - 1; throw new CommPortException(s.ToString()); } throw new CommPortException("IO Error [003]"); } if ((eventMask & Win32Com.EV_RXCHAR) != 0) { uint gotbytes; do { if (!Win32Com.ReadFile(_hPort, buf, 1, out gotbytes, unmanagedOv)) { if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) { Win32Com.CancelIo(_hPort); gotbytes = 0; } else { throw new CommPortException("IO Error [004]"); } } if (gotbytes == 1) { OnRxChar(buf[0]); } } while (gotbytes > 0); } if ((eventMask & Win32Com.EV_TXEMPTY) != 0) { OnTxDone(); } if ((eventMask & Win32Com.EV_BREAK) != 0) { OnBreak(); } uint i = 0; if ((eventMask & Win32Com.EV_CTS) != 0) { i |= Win32Com.MS_CTS_ON; } if ((eventMask & Win32Com.EV_DSR) != 0) { i |= Win32Com.MS_DSR_ON; } if ((eventMask & Win32Com.EV_RLSD) != 0) { i |= Win32Com.MS_RLSD_ON; } if ((eventMask & Win32Com.EV_RING) != 0) { i |= Win32Com.MS_RING_ON; } if (i != 0) { uint f; if (!Win32Com.GetCommModemStatus(_hPort, out f)) { throw new CommPortException("IO Error [005]"); } OnStatusChange(new ModemStatus(i), new ModemStatus(f)); } } } catch (Exception e) { if (uMask != IntPtr.Zero) { Marshal.FreeHGlobal(uMask); } if (unmanagedOv != IntPtr.Zero) { Marshal.FreeHGlobal(unmanagedOv); } if (!(e is ThreadAbortException)) { _rxException = e; OnRxException(e); } } }