// 从并口读取数据 public bool ReadPort(int NumBytes, ref byte[] commRead) { if (hComm == INVALID_HANDLE_VALUE) { return(false); } COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0; ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0) { PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT); } if (ComStat.cbInQue > 0) { OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesRead = 0; return(ReadFile(hComm, commRead, NumBytes, ref BytesRead, ref ovlCommPort)); } else { return(false); } }
public long ByteToRead() { if (hComm != INVALID_HANDLE_VALUE) { COMSTAT lSTAT = new COMSTAT(); uint lerror = 0; bool f = ClearCommError((IntPtr)hComm, ref lerror, ref lSTAT); return(lSTAT.cbInQue); } else { return(-1); } }
public void threadRead() { uint uBytesRead = 0; byte [] byData = new byte[100]; System.Text.ASCIIEncoding asciiEncoder = new System.Text.ASCIIEncoding(); try { PurgeComm(m_hCOMPort, PURGE_RXCLEAR); while (!m_bClose) //close thread { if (ReadFile(m_hCOMPort, byData, 20, ref uBytesRead, IntPtr.Zero)) { if (uBytesRead > 0 && m_bClose == false) { string sData = asciiEncoder.GetString(byData, 0, (int)uBytesRead); m_GpsTracker.COMCallback(m_iIndex, sData); try { if (m_GpsTracker.m_MessageMonitor != null) { m_GpsTracker.m_MessageMonitor.AddMessageCOMRaw(sData); } } catch (Exception) { m_GpsTracker.m_MessageMonitor = null; } } } else { Thread.Sleep(50); if (GetLastError() == ERROR_OPERATION_ABORTED) { uint uxErrors = 0; COMSTAT xstat = new COMSTAT(); ClearCommError(m_hCOMPort, ref uxErrors, ref xstat); PurgeComm(m_hCOMPort, PURGE_RXCLEAR); } } } //while } catch (Exception) { } }
// 往并口写数据 public bool WritePort(byte[] WriteBytes) { if (hComm == INVALID_HANDLE_VALUE) //如果并口未打开 { return(false); } COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0; ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0) { PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT); } OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesWritten = 0; // SET THE COMM TIMEOUTS. COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); GetCommTimeouts(hComm, ref ctoCommPort); ctoCommPort.ReadIntervalTimeout = Int32.MaxValue; ctoCommPort.ReadTotalTimeoutConstant = 0; ctoCommPort.ReadTotalTimeoutMultiplier = 0; ctoCommPort.WriteTotalTimeoutMultiplier = 10; ctoCommPort.WriteTotalTimeoutConstant = 3000; if (!SetCommTimeouts(hComm, ref ctoCommPort)) { return(false); } WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort); if (BytesWritten > 0) { return(true); } else { return(false); } }
// 往串口写数据 public bool WritePort(byte[] WriteBytes) { if (hComm == INVALID_HANDLE_VALUE) { return(false); } COMSTAT ComStat = new COMSTAT(); int dwErrorFlags = 0; ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (dwErrorFlags != 0) { PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT); } OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesWritten = 0; return(WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort)); }
public int Read(ref byte[] commRead, int NumBytes) { if (hComm != INVALID_HANDLE_VALUE) { COMSTAT ComStat = new COMSTAT(); uint dwErrorFlags = 0; ClearCommError(hComm, ref dwErrorFlags, ref ComStat); if (ComStat.cbInQue > 0 && NumBytes > 0) { int count = Math.Min(NumBytes, (int)(ComStat.cbInQue)); OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesRead = 0; if (!GetCommState(hComm, ref DCBlock)) { Logger.Error("GetCommState Error!"); } //Logger.Info("Com[{0}] Info: {1}, {2}, {3}, {4}", PortName, DCBlock.BaudRate, DCBlock.ByteSize, DCBlock.Parity, DCBlock.StopBits); bool ret = ReadFile(hComm, commRead, count, ref BytesRead, ref ovlCommPort); if (ret) { return(BytesRead); } else { return(0); } } else { return(0); } } else { return(-1); } }
public static extern bool ClearCommError(IntPtr hFile, out uint lpErrors, out COMSTAT cs);
internal static extern bool ClearCommError(IntPtr hFile, out uint lpErrors, out COMSTAT cs);
private static extern bool ClearCommError( int hFile, // handle to file ref int lpErrors, ref COMSTAT lpStat );
internal static extern bool ClearCommError( IntPtr hFile, ref int lpErrors, ref COMSTAT lpStat );
internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, out COMSTAT cs);
public static extern Boolean ClearCommError([In()] CreateFileWHandle hFile, ref IntPtr lpErrors, ref COMSTAT lpStat);
private static extern bool ClearCommError(uint hFile, ref uint lpErrors,ref COMSTAT lpStat);
public static extern bool ClearCommError(IntPtr handle, out uint errors, ref COMSTAT ver);
public static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, out COMSTAT cs);
public bool Open() { bool fRet = false; DCB dcbCommPort = new DCB(); COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); //ensure comport is closed Close(); // Open select Gps COM port m_hCOMPort = CreateFile("\\\\.\\COM" + m_iCOMPort.ToString().Trim(), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); if (m_hCOMPort != INVALID_HANDLE_VALUE) { uint uxErrors = 0; COMSTAT xstat = new COMSTAT(); ClearCommError(m_hCOMPort, ref uxErrors, ref xstat); PurgeComm(m_hCOMPort, PURGE_RXCLEAR); // SET THE COMM TIMEOUTS. GetCommTimeouts(m_hCOMPort, ref ctoCommPort); ctoCommPort.ReadIntervalTimeout = 0; //200ms read timeout ctoCommPort.ReadTotalTimeoutConstant = 100; ctoCommPort.ReadTotalTimeoutMultiplier = 0; SetCommTimeouts(m_hCOMPort, ref ctoCommPort); // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. dcbCommPort.DCBlength = (uint)Marshal.SizeOf(dcbCommPort); GetCommState(m_hCOMPort, ref dcbCommPort); dcbCommPort.DCBlength = (uint)Marshal.SizeOf(dcbCommPort); dcbCommPort.BaudRate = m_iBaudRate; dcbCommPort.Parity = m_byParity; dcbCommPort.ByteSize = m_byByteSize; dcbCommPort.StopBits = m_byStopBits; dcbCommPort.flags = 1; switch (m_iFlowControl) { case 0: //None dcbCommPort.flags = 0x1091; //use 1091 for no abort on error 50 break; case 1: //Hardware dcbCommPort.flags = 0x2095; //use 2095 for no abort on error 60 break; case 2: //Sw dcbCommPort.flags = 0x1391; //use 1391 for no abort on error 53 break; } dcbCommPort.XonLim = 0x50; dcbCommPort.XoffLim = 0xC8; dcbCommPort.XonChar = Convert.ToChar(0x11); dcbCommPort.XoffChar = Convert.ToChar(0x13); dcbCommPort.wReserved = 0; dcbCommPort.wReserved1 = 0; dcbCommPort.ErrorChar = Convert.ToChar(0); dcbCommPort.EvtChar = Convert.ToChar(0); dcbCommPort.EofChar = Convert.ToChar(0); if (SetCommState(m_hCOMPort, ref dcbCommPort)) { fRet = true; } } IsOpen = fRet; m_bClose = !fRet; return(fRet); }
private static extern bool ClearCommError(uint hFile, ref uint lpErrors, ref COMSTAT lpStat);
internal static extern bool ClearCommError( IntPtr hFile, ref uint lpErrors, ref COMSTAT lpStat);
public void threadRead() { uint uBytesRead=0; byte [] byData = new byte[100]; System.Text.ASCIIEncoding asciiEncoder = new System.Text.ASCIIEncoding(); try { PurgeComm(m_hCOMPort,PURGE_RXCLEAR); while (!m_bClose) //close thread { if (ReadFile(m_hCOMPort,byData,20,ref uBytesRead,IntPtr.Zero)) { if (uBytesRead>0 && m_bClose==false) { string sData = asciiEncoder.GetString(byData,0,(int)uBytesRead); m_GpsTracker.COMCallback(m_iIndex,sData); try { if(m_GpsTracker.m_MessageMonitor!=null) m_GpsTracker.m_MessageMonitor.AddMessageCOMRaw(sData); } catch (Exception) { m_GpsTracker.m_MessageMonitor=null; } } } else { Thread.Sleep(50); if (GetLastError()==ERROR_OPERATION_ABORTED) { uint uxErrors=0; COMSTAT xstat = new COMSTAT(); ClearCommError (m_hCOMPort,ref uxErrors, ref xstat); PurgeComm(m_hCOMPort,PURGE_RXCLEAR); } } } //while } catch(Exception) { } }
public bool Open() { bool fRet=false; DCB dcbCommPort = new DCB(); COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); //ensure comport is closed Close(); // Open select Gps COM port m_hCOMPort = CreateFile("\\\\.\\COM" + m_iCOMPort.ToString().Trim() ,GENERIC_READ,0, 0,OPEN_EXISTING,0,0); if(m_hCOMPort != INVALID_HANDLE_VALUE) { uint uxErrors=0; COMSTAT xstat = new COMSTAT(); ClearCommError (m_hCOMPort,ref uxErrors, ref xstat); PurgeComm(m_hCOMPort,PURGE_RXCLEAR); // SET THE COMM TIMEOUTS. GetCommTimeouts(m_hCOMPort,ref ctoCommPort); ctoCommPort.ReadIntervalTimeout=0; //200ms read timeout ctoCommPort.ReadTotalTimeoutConstant = 100; ctoCommPort.ReadTotalTimeoutMultiplier = 0; SetCommTimeouts(m_hCOMPort,ref ctoCommPort); // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. dcbCommPort.DCBlength=(uint)Marshal.SizeOf(dcbCommPort); GetCommState(m_hCOMPort, ref dcbCommPort); dcbCommPort.DCBlength=(uint)Marshal.SizeOf(dcbCommPort); dcbCommPort.BaudRate=m_iBaudRate; dcbCommPort.Parity=m_byParity; dcbCommPort.ByteSize=m_byByteSize; dcbCommPort.StopBits=m_byStopBits; dcbCommPort.flags=1; switch (m_iFlowControl) { case 0: //None dcbCommPort.flags=0x1091; //use 1091 for no abort on error 50 break; case 1: //Hardware dcbCommPort.flags=0x2095; //use 2095 for no abort on error 60 break; case 2: //Sw dcbCommPort.flags=0x1391; //use 1391 for no abort on error 53 break; } dcbCommPort.XonLim=0x50; dcbCommPort.XoffLim=0xC8; dcbCommPort.XonChar=Convert.ToChar(0x11); dcbCommPort.XoffChar=Convert.ToChar(0x13); dcbCommPort.wReserved=0; dcbCommPort.wReserved1=0; dcbCommPort.ErrorChar=Convert.ToChar(0); dcbCommPort.EvtChar=Convert.ToChar(0); dcbCommPort.EofChar=Convert.ToChar(0); if (SetCommState(m_hCOMPort, ref dcbCommPort)) fRet=true; } IsOpen=fRet; m_bClose=!fRet; return fRet; }
internal static partial bool ClearCommError( SafeFileHandle hFile, ref int lpErrors, ref COMSTAT lpStat);
internal static extern bool ClearCommError( SafeFileHandle hFile, // handle to comm device ref int lpErrors, ref COMSTAT lpStat );
// 장치의 오류 Flag를 지우거나 송수신된 데이타의 갯수를 확인한다. // lpErrors : // [1]CE_RXOVER: 수신버퍼 Overflow 발생 // [2]CE_OVERRUN: 수신버퍼 Overrun 에러발생 // [4]CE_RXPARITY: 수신 데이타 패리티비트 에러발생 // [8]CE_FRAME: 수신 Framing 에러발생 // lpStat->cbInQue : 수신버퍼에 입력된 데이타 갯수 // lpStat->cbOutQue: 송신버퍼에 남은 데이타 갯수 [DllImport("kernel32.dll")] public static extern uint AxsPortClearCommError(int nPortNo, out uint lpErrors, ref COMSTAT lpStat);
public static extern bool ClearCommError(int hFile, ref int lpErrors, ref COMSTAT lpStat);