-
大小: 7KB文件类型: .rar金币: 2下载: 0 次发布日期: 2021-05-25
- 语言: C/C++
- 标签: serialport.h .cPP
资源简介
C++串口通信类,此资源是利用CserialPort类进行串口通信的源码

代码片段和文件信息
/*
** FILENAME CSerialPort.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
//
// Constructor
//
CSerialPort::CSerialPort()
{
m_hComm = NULL;
// initialize overlapped structure members to zero
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
// create events
m_ov.hEvent = NULL;
m_hWriteEvent = NULL;
m_hShutdownEvent = NULL;
m_szWriteBuffer = NULL;
m_bThreadAlive = FALSE;
}
//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
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 CSerialPort::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 < 11);
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 char[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=%
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3182 2011-08-08 16:13 CSerialPort.h
文件 17641 2011-08-08 16:12 SerialPort.cpp
----------- --------- ---------- ----- ----
20823 2
相关资源
- 漂亮的仪表盘(widget.cpp)
- 代码雨.cpp
- libevent(timeout.cpp)
- 密码学 重合指数计算(IC.cpp)
- 输出n的阶乘.cpp
- opencv2 3D标定.cpp
- c 操作sqlite数据库.cpp
- 扫雷游戏.cpp
- c++爱心表白(心.cpp)
- 算法表达式求值.cpp
- 银行家算法分配资源的模拟实现(m
- 常见排序算法比较.cpp
- 无向图M着色.cpp
- 客户消费积分管理系统.cpp
- 公司人员月薪级别信息管理系统.cpp
- 俄罗斯方块.cpp
- 跑跑卡丁车.cpp
- 扫雷游戏.cpp(较简单)
- 打飞机.cpp
- 吃豆人.cpp 代码
- opengl绘制汽车.cpp
- consts.cpp
- 银行管理系统.cpp
- BombGoldFlower.cpp
- 扫雷游戏.cpp C++ 课程设计
- 图书管理系统.cpp
- c++游戏歹徒逃亡.cpp
- 180912.cpp
- ZhajingHua.cpp
- puma560的运动学正解和逆解的C++源码
评论
共有 条评论