⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialport.cpp

📁 简单的串口调试
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!SetCommBreak(m_hComm))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::SetBreak, Failed in call to SetCommBreak, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::ClearBreak()
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!ClearCommBreak(m_hComm))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::ClearBreak, Failed in call to SetCommBreak, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::ClearError(DWORD& dwErrors)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!ClearCommError(m_hComm, &dwErrors, NULL))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::ClearError, Failed in call to ClearCommError, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::GetDefaultConfig(int nPort, COMMCONFIG& config)
{
  //Create the device name as a string
  CString sPort;
  sPort.Format(_T("COM%d"), nPort);

  DWORD dwSize = sizeof(COMMCONFIG);
  if (!GetDefaultCommConfig(sPort, &config, &dwSize))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetDefaultConfig, Failed in call to GetDefaultCommConfig, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::SetDefaultConfig(int nPort, COMMCONFIG& config)
{
  //Create the device name as a string
  CString sPort;
  sPort.Format(_T("COM%d"), nPort);

  DWORD dwSize = sizeof(COMMCONFIG);
  if (!SetDefaultCommConfig(sPort, &config, dwSize))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::SetDefaultConfig, Failed in call to SetDefaultCommConfig, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::GetStatus(COMSTAT& stat)
{
  //Validate our parameters
  ASSERT(IsOpen());

  DWORD dwErrors;
  if (!ClearCommError(m_hComm, &dwErrors, &stat))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetStatus, Failed in call to ClearCommError, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::GetState(DCB& dcb)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!GetCommState(m_hComm, &dcb))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetState, Failed in call to GetCommState, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::SetState(DCB& dcb)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!SetCommState(m_hComm, &dcb))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::SetState, Failed in call to SetCommState, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::Escape(DWORD dwFunc)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!EscapeCommFunction(m_hComm, dwFunc))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::Escape, Failed in call to EscapeCommFunction, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::ClearDTR()
{
  Escape(CLRDTR);
}

void CSerialPort::ClearRTS()
{
  Escape(CLRRTS);
}

void CSerialPort::SetDTR()
{
  Escape(SETDTR);
}

void CSerialPort::SetRTS()
{
  Escape(SETRTS);
}

void CSerialPort::SetXOFF()
{
  Escape(SETXOFF);
}

void CSerialPort::SetXON()
{
  Escape(SETXON);
}

void CSerialPort::GetProperties(COMMPROP& properties)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!GetCommProperties(m_hComm, &properties))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetProperties, Failed in call to GetCommProperties, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::GetModemStatus(DWORD& dwModemStatus)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!GetCommModemStatus(m_hComm, &dwModemStatus))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetModemStatus, Failed in call to GetCommModemStatus, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::SetMask(DWORD dwMask)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!SetCommMask(m_hComm, dwMask))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::SetMask, Failed in call to SetCommMask, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::GetMask(DWORD& dwMask)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!GetCommMask(m_hComm, &dwMask))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetMask, Failed in call to GetCommMask, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::Flush()
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!FlushFileBuffers(m_hComm))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::Flush, Failed in call to FlushFileBuffers, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::Purge(DWORD dwFlags)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!PurgeComm(m_hComm, dwFlags))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::Purge, Failed in call to PurgeComm, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::TerminateOutstandingWrites()
{
  Purge(PURGE_TXABORT);
}

void CSerialPort::TerminateOutstandingReads()
{
  Purge(PURGE_RXABORT);
}

void CSerialPort::ClearWriteBuffer()
{
  Purge(PURGE_TXCLEAR);
}

void CSerialPort::ClearReadBuffer()
{
  Purge(PURGE_RXCLEAR);
}

void CSerialPort::Setup(DWORD dwInQueue, DWORD dwOutQueue)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!SetupComm(m_hComm, dwInQueue, dwOutQueue))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::Setup, Failed in call to SetupComm, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::SetTimeouts(COMMTIMEOUTS& timeouts)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!SetCommTimeouts(m_hComm, &timeouts))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::SetTimeouts, Failed in call to SetCommTimeouts, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::GetTimeouts(COMMTIMEOUTS& timeouts)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!GetCommTimeouts(m_hComm, &timeouts))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::GetTimeouts, Failed in call to GetCommTimeouts, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

void CSerialPort::Set0Timeout()
{
  COMMTIMEOUTS Timeouts;
  memset(&Timeouts, 0, sizeof(Timeouts));
  Timeouts.ReadIntervalTimeout = MAXDWORD;
  SetTimeouts(Timeouts);
}

void CSerialPort::Set0WriteTimeout()
{
  COMMTIMEOUTS Timeouts;
  GetTimeouts(Timeouts);
  Timeouts.WriteTotalTimeoutMultiplier = 0;
  Timeouts.WriteTotalTimeoutConstant = 0;
  SetTimeouts(Timeouts);
}

void CSerialPort::Set0ReadTimeout()
{
  COMMTIMEOUTS Timeouts;
  GetTimeouts(Timeouts);
  Timeouts.ReadIntervalTimeout = MAXDWORD;
  Timeouts.ReadTotalTimeoutMultiplier = 0;
  Timeouts.ReadTotalTimeoutConstant = 0;
  SetTimeouts(Timeouts);
}

void CSerialPort::WaitEvent(DWORD& dwMask)
{
  //Validate our parameters
  ASSERT(IsOpen());

  if (!WaitCommEvent(m_hComm, &dwMask, NULL))
  {
    DWORD dwLastError = GetLastError();
    TRACE(_T("CSerialPort::WaitEvent, Failed in call to WaitCommEvent, Error:%d\n"), dwLastError);
    ThrowSerialException(dwLastError);
  }
}

BOOL CSerialPort::WaitEvent(DWORD& dwMask, OVERLAPPED& overlapped)
{
  //Validate our parameters
  ASSERT(IsOpen());
  ASSERT(overlapped.hEvent);

  BOOL bSuccess = WaitCommEvent(m_hComm, &dwMask, &overlapped);
  if (!bSuccess)
  {
    DWORD dwLastError = GetLastError();
    if (dwLastError != ERROR_IO_PENDING)
    {
      TRACE(_T("CSerialPort::WaitEvent, Failed in call to WaitCommEvent, Error:%d\n"), dwLastError);
      ThrowSerialException(dwLastError);
    }
  }

  return bSuccess;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -