?? serport.cpp
字號:
/*
Module : SERPORT.CPP
Purpose: "C" style Implementation to GPS32
Created: PJN / 28-12-1997
History: None
Copyright (c) 1997 - 2001 by PJ Naughter. (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
///////////////////////////////// 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 + -