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

📄 serport.cpp

📁 在你的应用程序中添加对GPS(全球定位系统)的支持
💻 CPP
字号:
/*
Module : SERPORT.CPP
Purpose: "C" style Implementation to GPS32
Created: PJN / 28-12-1997
History: None

Copyright (c) 1997 by PJ Naughter.  
All rights reserved.

*/



/////////////////////////////////  Includes  //////////////////////////////////
#include "stdafx.h"
#include "serport.h"



//////////////////////////////////  Macros / Defines //////////////////////////
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif




////////////////////////////////// Implementation /////////////////////////////

CGpsSerialPort::CGpsSerialPort()
{
  m_hPort = INVALID_HANDLE_VALUE;
}

CGpsSerialPort::~CGpsSerialPort()
{
  Close();
}

BOOL CGpsSerialPort::Open(LPCGPSDEVINFO lpDevInfo)
{
  //open the specified comms port
  CString sPortNum;
  sPortNum.Format(_T("COM%d"), lpDevInfo->wCommPort);
  m_hPort = CreateFile(sPortNum, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  if (m_hPort == INVALID_HANDLE_VALUE)
    return FALSE;


  //set the baud rate, parity, stop bits and data bits
  DCB dcb;
  ZeroMemory(&dcb, sizeof(DCB));
  if (::GetCommState(m_hPort, &dcb) == FALSE)
  {
    TRACE(_T("CGpsSerialPort::Open failed in call to GetCommState\n"));
    Close();
    return FALSE;
  }

  dcb.BaudRate = lpDevInfo->dwCommBaudRate;
  switch (lpDevInfo->wCommParity)
  {
    case GpsParityNone: dcb.Parity = NOPARITY; break;
    case GpsParityOdd:  dcb.Parity = ODDPARITY; break;
    case GpsParityEven: dcb.Parity = EVENPARITY; break;
    default: ASSERT(FALSE); break;
  }
  switch (lpDevInfo->wCommStopBits)
  {
    case GpsStopBits1:       dcb.StopBits = ONESTOPBIT; break;
    case GpsStopBits1Point5: dcb.StopBits = ONE5STOPBITS; break;
    case GpsStopBits2:       dcb.StopBits = TWOSTOPBITS; break;
    default: ASSERT(FALSE); break;
  }
  dcb.ByteSize = (BYTE) lpDevInfo->wCommDataBits;
  dcb.fAbortOnError = FALSE; // Terminate Reads & Writes if there's an error
  dcb.fErrorChar    = TRUE;  // Replace any garbled bytes with ErrorChar
  dcb.ErrorChar     = ' ';   // Garbage bytes are spaces
  dcb.fBinary       = TRUE;  // Ignore EOF

  if (::SetCommState(m_hPort, &dcb) == FALSE)
  {
    TRACE(_T("CGpsSerialPort::Open failed in call to SetCommState\n"));
    Close();
    return FALSE;
  }


  //Setup return immediatly from any ReadFile calls
  COMMTIMEOUTS cti;
  ZeroMemory(&cti, sizeof(COMMTIMEOUTS));
  cti.ReadIntervalTimeout = MAXDWORD;
  if (::SetCommTimeouts(m_hPort, &cti) == FALSE)
  {
    TRACE(_T("CGpsSerialPort::Open failed in call to SetCommTimeouts\n"));
    Close();
    return FALSE;
  }

  return TRUE;
}

void CGpsSerialPort::Close()
{
  if (m_hPort != INVALID_HANDLE_VALUE)
  {
    if (!CloseHandle(m_hPort))
    {
      TRACE(_T("CGpsSerialPort::Close failed in call to CloseHandle\n"));
    }
    m_hPort = INVALID_HANDLE_VALUE;
  }
}

DWORD CGpsSerialPort::Read(LPBYTE psBuffer, UINT nBytes) const
{
  DWORD dwBytesRead;
  ReadFile(m_hPort, psBuffer, nBytes, &dwBytesRead, NULL);
  return dwBytesRead;
}

⌨️ 快捷键说明

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