-
大小: 13KB文件类型: .zip金币: 1下载: 0 次发布日期: 2021-06-25
- 语言: 其他
- 标签: CSerialPort
资源简介
**
** 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
**
**
************************************************************************************
** author: mrlong date:2007-12-25
**
** 改进
** 1) 增加ClosePort
** 2) 增加 writetoProt() 两个方法
** 3) 增加 SendData 与 RecvData 方法
**
************************************************************************************
** author:liquanhai date:2011-11-04
** 改进
** 1)增加 ClosePort中交出控制权,防止死锁问题
** 2) 增加 ReceiveChar中防止线程死锁
***************************************************************************************
** author: itas109 date:2014-01-10
** Blog:blog.csdn.net/itas109
**
** 改进
** 1) 解决COM10以上端口无法显示的问题
** 2) 扩展可选择端口,最大值MaxSerialPortNum可以自定义
** 3) 添加QueryKey()和Hkey2ComboBox两个方法,用于自动查询当前有效的串口号
**
博客:blog.csdn.net/itas109
Email:itas109@qq.com
代码片段和文件信息
/*
** 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
int m_nComArray[20];
//
// 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;
m_nWriteSize=1;///
m_bIsSuspened = FALSE;///
}
//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()///析构函数
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
// if the port is still opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// Close Handles
if(m_hShutdownEvent!=NULL)
CloseHandle( m_hShutdownEvent);
if(m_ov.hEvent!=NULL)
CloseHandle( m_ov.hEvent );
if(m_hWriteEvent!=NULL)
CloseHandle( m_hWriteEvent );
TRACE(“Thread ended\n“);
delete [] m_szWriteBuffer;
}
//
// Initialize the port. This can be port 1 to MaxSerialPortNum.
///初始化串口。只能是1-MaxSerialPortNum
//
BOOL CSerialPort::InitPort(CWnd* pPortOwner // the owner (CWnd) of the port (receives message)
UINT portnr // portnumber (1..MaxSerialPortNum)
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 < MaxSerialPortNum+1);///add by itas109 2014-01-09
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);
else
m_ov.hEvent = CreateEvent(NULL TRUE FALSE NULL);
if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
else
m_hWriteEvent = CreateEvent(NULL TRUE FALSE NULL);
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
else
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 =
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1253 2014-01-16 15:00 CSerialPort\README.txt
文件 29121 2014-01-13 21:11 CSerialPort\SerialPort.cpp
文件 5516 2014-01-16 14:33 CSerialPort\SerialPort.h
目录 0 2014-01-16 14:59 CSerialPort\
评论
共有 条评论