-
大小: 13KB文件類型: .zip金幣: 2下載: 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? 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\
評論
共有 條評論