//CSerialPortEx类是在Remon Spekreijse设计的CSerialPort类基础上设计的,并增加
//了对二进制数据传输和块读写方式的支持和其他功能函数。关于CSerialPort类的声明如下:
/*
** FILENAME SerialPort.cpp
**
** PURPOSE This class can read, write and watch one serial port.
** It sends messages to its owner when something happends on the port
** The class creates a thread for reading and writing so the main
** program is not blocked.
**
** CREATION DATE 15-09-1997
** LAST MODIFICATION 12-11-1997
**
** AUTHOR Remon Spekreijse
**
**
*/
#include "stdafx.h"
#include "SerialPort.h"
#include <assert.h>
//
// Constructor
//
CSerialPortEx::CSerialPortEx()
{
m_hComm = NULL;
// initialize overlapped structure members to zero
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
m_ov.hEvent = NULL;
// create events
m_hWriteEvent = NULL;
m_hShutdownEvent = NULL;
m_szWriteBuffer = NULL;
m_bThreadAlive = FALSE;
m_bBlockRead=FALSE;
}
//
// Delete dynamic memory
//
CSerialPortEx::~CSerialPortEx()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended\n");
delete [] m_szWriteBuffer;
}
//
// Initialize the port. This can be port 1 to 4.
//
BOOL CSerialPortEx::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL);
// if the thread is alive: Kill
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended\n");
}
// create events
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// initialize the event objects
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent;
// initialize critical section
InitializeCriticalSection(&m_csCommunicationSync);
// set buffersize for writing and save the owner
m_pOwner = pPortOwner;
if (m_szWriteBuffer != NULL)
delete [] m_szWriteBuffer;
m_szWriteBuffer =new BYTE[writebuffersize];
m_nPortNr = portnr;
m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents;
BOOL bResult = FALSE;
char *szPort = new char[50];
char *szBaud = new char[50];
// now it critical!
EnterCriticalSection(&m_csCommunicationSync);
// if the port is already opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// prepare port strings
sprintf(szPort, "COM%d", portnr);
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
// get a handle to the port
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices
if (m_hComm == INVALID_HANDLE_VALUE)
{
// port not found
delete [] szPort;
delete [] szBaud;
return FALSE;
}
// set the timeout values
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
// configure
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
if (BuildCommDCB(szBaud, &m_dcb))
{
if (SetCommState(m_hComm, &m_dcb))
; // normal operation... continue
else
ProcessErrorMessage("SetCommState()");
}
else
ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()");
delete [] szPort;
delete [] szBaud;
// flush the port
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
// release critical section
LeaveCriticalSection(&m_csCommunicationSync);
TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr);
return TRUE;
}
//
// The CommThread Function.
//
UINT CSerialPortEx::CommThread(LPVOID pParam)
{
// Cast the void pointer passed to the thread back to
// a pointer of CSerialPort class
CSerialPortEx *port = (CSerialPortEx*)pParam;
// Set the status variable in the dialog class to
// TRUE to indicate the thread is running.
port->m_bThreadAlive = TRUE;
// Misc. variables
DWORD BytesTransfered = 0;
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat;
BOOL bResult = TRUE;
// Clear comm buffers at startup
if (port->m_hComm) // check if the port is opened
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
// begin forever loop. This loop will run as long as the thread is alive.
for (;;)
{
// Make a call to WaitCommEvent(). This call will return immediatly
// because our port was created as an async port (FILE_FLAG_OVERLAPPED
// and an m_OverlappedStructerlapped structure specified). This call will cause the
// m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, which is part of the m_hEventArray to
// be placed in a non-signeled state if there are no bytes available to be read,
// or to a signeled state if there are bytes available. If this event handle
// is set to the non-signeled state, it will be set to signeled when a
// character arrives at the port.
// we do this for each port!
bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
if (!bResult)
{
// If WaitCommEvent() returns FALSE, process the last error to determin
// the reason..
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// This is a normal return value if there are no bytes
// to read at the port.
// Do nothing and continue
break;
}
case 87:
{
// Under Windows NT, this value is returned for some reason.
// I have not investigated why, but it is also a valid reply
// Also do nothing and continue.
break;
}
default:
{
// All other error codes indicate a serious error has
// occured. Process this error.
port->ProcessErrorMessage("WaitCommEvent()");
break;
}
}
}
else
{
// If WaitCommEvent() returns TRUE, check to be sure there are
// actually bytes in the buffer to read.
//
// If you are reading more than one byte at a time from the buffer
// (which this program does not do) you will have the situation occur
// where the first byte to arrive will cause the WaitForMultipleObjects()
// function to stop waiting. The WaitForMultipleObjects() function
// resets the event handle in m_OverlappedStruct.hEvent to the non-signelead state
// as it returns.
//
// If in the time
没有合适的资源?快使用搜索试试~ 我知道了~
GPS.rar_GPS matlab_Gps VC_VC GPS接收_VC 接收gps_vc g
共100个文件
obj:26个
h:14个
sbr:13个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 69 浏览量
2022-09-23
23:21:07
上传
评论
收藏 5.56MB RAR 举报
温馨提示
VC实现GPS数据接收的一个代码,比较简单,但是功能比较全面了
资源推荐
资源详情
资源评论
收起资源包目录
GPS.rar_GPS matlab_Gps VC_VC GPS接收_VC 接收gps_vc g (100个子文件)
GPS.APS 37KB
GPS.clw.bak 4KB
Toolbar.bmp 2KB
GPS.bsc 3.23MB
GPS.clw 4KB
SerialPort.cpp 20KB
msdgridctrl.cpp 14KB
rdc.cpp 12KB
MainFrm.cpp 8KB
GPS.cpp 4KB
GPSView.cpp 4KB
ConfigDlg.cpp 3KB
font.cpp 2KB
datetime.cpp 2KB
GPSDoc.cpp 2KB
GPSSet.cpp 2KB
picture.cpp 1KB
StdAfx.cpp 205B
Thumbs.db 5KB
GPS.dsp 6KB
GPS.dsw 619B
GPS.exe 180KB
GPS.exe 48KB
msdgridctrl.h 5KB
rdc.h 4KB
SerialPort.h 3KB
GPSView.h 2KB
MainFrm.h 2KB
Resource.h 2KB
GPSDoc.h 1KB
font.h 1KB
ConfigDlg.h 1KB
GPS.h 1KB
GPSSet.h 1KB
picture.h 1KB
StdAfx.h 1KB
datetime.h 325B
BuildLog.htm 4KB
GPS.ico 1KB
GPSDoc.ico 1KB
vc60.idb 233KB
vc60.idb 65KB
GPS.ilk 549KB
GPS.exe.embed.manifest 2KB
GPSData.mdb 148KB
GPSData.mdb 148KB
GPS.ncb 89KB
StdAfx.obj 105KB
msdgridctrl.obj 88KB
rdc.obj 71KB
msdgridctrl.obj 43KB
MainFrm.obj 39KB
rdc.obj 36KB
GPSView.obj 34KB
SerialPort.obj 25KB
GPS.obj 24KB
ConfigDlg.obj 24KB
MainFrm.obj 23KB
GPSView.obj 20KB
GPSDoc.obj 18KB
GPSSet.obj 17KB
GPS.obj 16KB
ConfigDlg.obj 14KB
font.obj 13KB
SerialPort.obj 12KB
datetime.obj 12KB
GPSDoc.obj 10KB
GPSSet.obj 10KB
picture.obj 6KB
font.obj 6KB
datetime.obj 5KB
picture.obj 2KB
StdAfx.obj 780B
GPS.opt 53KB
GPS.pch 6.83MB
GPS.pch 5.65MB
GPS.pdb 513KB
vc60.pdb 372KB
GPS.plg 3KB
GPS.rc 35KB
GPS.rc2 395B
GPS.res 14KB
GPS.res 14KB
StdAfx.sbr 1.38MB
MainFrm.sbr 24KB
GPS.sbr 22KB
msdgridctrl.sbr 20KB
GPSView.sbr 18KB
rdc.sbr 14KB
SerialPort.sbr 6KB
ConfigDlg.sbr 5KB
GPSDoc.sbr 5KB
GPSSet.sbr 4KB
font.sbr 3KB
datetime.sbr 2KB
picture.sbr 1KB
GPS.sln 870B
GPS.suo 9KB
GPS.vcproj.NIT-7C01CF3645A.pineapple.user 1KB
GPS.vcproj 12KB
共 100 条
- 1
资源评论
weixin_42651887
- 粉丝: 80
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功