串口操作类Word格式文档下载.docx
- 文档编号:8598417
- 上传时间:2023-05-12
- 格式:DOCX
- 页数:12
- 大小:19.20KB
串口操作类Word格式文档下载.docx
《串口操作类Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《串口操作类Word格式文档下载.docx(12页珍藏版)》请在冰点文库上搜索。
/*停止位*/
);
//关闭串口
voidClosePort();
//同步写入数据
BOOLWriteSyncPort(constBYTE*buf,DWORDbufLen);
//设置串口读取、写入超时
BOOLSetSeriesTimeouts(COMMTIMEOUTSCommTimeOuts);
//得到串口是否打开
BOOLGetComOpened();
private:
//串口读线程函数
static
DWORDWINAPIReadThreadFunc(LPVOIDlparam);
//关闭读线程
voidCloseReadThread();
//已打开的串口句柄
HANDLE
m_hComm;
//读线程句柄
HANDLEm_hReadThread;
//读线程ID标识
DWORDm_dwReadThreadID;
//读线程退出事件
HANDLEm_hReadCloseEvent;
BOOLm_bOpened;
//串口是否打开
void*m_pOwner;
//指定父对象指针
ONSERIESREADm_OnSeriesRead;
//串口读取回调函数
};
//构造函数
CCESeries:
:
CCESeries()
//初始化内部变量
m_hComm=INVALID_HANDLE_VALUE;
m_OnSeriesRead=NULL;
m_bOpened=0;
}
//析构函数
~CCESeries()
if(m_bOpened)
ClosePort();
DWORDCCESeries:
ReadThreadFunc(LPVOIDlparam)
CCESeries*ceSeries=(CCESeries*)lparam;
DWORD
evtMask;
BYTE*readBuf=NULL;
//读取的字节
DWORDactualReadLen=0;
//实际读取的字节数
DWORDwillReadLen;
DWORDdwReadErrors;
COMSTAT
cmState;
//清空缓冲,并检查串口是否打开。
ASSERT(ceSeries->
m_hComm!
=INVALID_HANDLE_VALUE);
//清空串口
PurgeComm(ceSeries->
m_hComm,PURGE_RXCLEAR|PURGE_TXCLEAR);
SetCommMask(ceSeries->
m_hComm,EV_RXCHAR|EV_CTS|EV_DSR);
while(TRUE)
{
if(WaitCommEvent(ceSeries->
m_hComm,&
evtMask,0))
//表示串口收到字符
if(evtMask&
EV_RXCHAR)
ClearCommError(ceSeries->
dwReadErrors,&
cmState);
willReadLen=cmState.cbInQue;
if(willReadLen<
=0)
continue;
//分配内存
readBuf=newBYTE[willReadLen];
ZeroMemory(readBuf,willReadLen);
//读取串口数据
ReadFile(ceSeries->
m_hComm,readBuf,willReadLen,&
actualReadLen,0);
//如果读取的数据大于,
if(actualReadLen>
0)
//触发读取回调函数
if(ceSeries->
m_OnSeriesRead)
ceSeries->
m_OnSeriesRead(ceSeries->
m_pOwner,readBuf,actualReadLen);
//释放内存
delete[]readBuf;
readBuf=NULL;
//如果收到读线程退出信号,则退出线程
if(WaitForSingleObject(ceSeries->
m_hReadCloseEvent,500)==WAIT_OBJECT_0)
break;
return0;
voidCCESeries:
CloseReadThread()
SetEvent(m_hReadCloseEvent);
//设置所有事件无效无效
SetCommMask(m_hComm,0);
//清空所有将要读的数据
PurgeComm(m_hComm,
PURGE_RXCLEAR);
//等待秒,如果读线程没有退出,则强制退出
if(WaitForSingleObject(m_hReadThread,4000)==WAIT_TIMEOUT)
TerminateThread(m_hReadThread,0);
m_hReadThread=NULL;
/*
*函数介绍:
打开串口
*入口参数:
pPortOwner
:
使用此串口类的窗体句柄
portNo
串口号
baud
波特率
parity
奇偶校验
databits
数据位
stopbits
停止位
*出口参数:
(无)
*返回值:
TRUE:
成功打开串口;
FALSE:
打开串口失败
*/
BOOLCCESeries:
OpenPort(void*pOwner,
UINTportNo
UINTbaud
UINTparity
/*奇偶校验*/
UINTdatabits
UINTstopbits
)
DCBcommParam;
TCHARszPort[15];
ASSERT(pOwner!
=NULL);
m_pOwner=pOwner;
//已经打开的话,直接返回
if(m_hComm!
=INVALID_HANDLE_VALUE)
returnTRUE;
//设置串口名
wsprintf(szPort,L"
COM%d:
"
portNo);
m_hComm=CreateFile(
szPort,
GENERIC_READ|GENERIC_WRITE,
//允许读和写
0,
//独占方式(共享模式)
NULL,
OPEN_EXISTING,
//打开而不是创建(创建方式)
0,
NULL
if(m_hComm==INVALID_HANDLE_VALUE)
//无效句柄,返回。
TRACE(_T("
CreateFile返回无效句柄\n"
));
returnFALSE;
//得到打开串口的当前属性参数,修改后再重新设置串口。
if(!
GetCommState(m_hComm,&
commParam))
CloseHandle(m_hComm);
//设置串口参数
commParam.BaudRate=baud;
//设置波特率
commParam.fBinary=TRUE;
//设置二进制模式,此处必须设置TRUE
commParam.fParity=TRUE;
//支持奇偶校验
commParam.ByteSize=databits;
//数据位,范围:
4-8
commParam.Parity=parity;
//校验模式
commParam.StopBits=stopbits;
//停止位
commParam.fOutxCtsFlow=FALSE;
//NoCTSoutputflowcontrol
commParam.fOutxDsrFlow=FALSE;
//NoDSRoutputflowcontrol
commParam.fDtrControl=DTR_CONTROL_ENABLE;
//DTRflowcontroltype
commParam.fDsrSensitivity=FALSE;
//DSRsensitivity
commParam.fTXContinueOnXoff=TRUE;
//XOFFcontinuesTx
commParam.fOutX=FALSE;
//NoXON/XOFFoutflowcontrol
commParam.fInX=FALSE;
//NoXON/XOFFinflowcontrol
commParam.fErrorChar=FALSE;
//Disableerrorreplacement
commParam.fNull=FALSE;
//Disablenullstripping
commParam.fRtsControl=RTS_CONTROL_ENABLE;
//RTSflowcontrol
commParam.fAbortOnError=FALSE;
//当串口发生错误,并不终止串口读写
SetCommState(m_hComm,&
SetCommStateerror"
//设置串口读写时间
COMMTIMEOUTSCommTimeOuts;
GetCommTimeouts(m_hComm,&
CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=10;
CommTimeOuts.WriteTotalTimeoutConstant=1000;
if(!
SetCommTimeouts(m_hComm,&
CommTimeOuts))
TRACE(_T("
SetCommTimeouts返回错误"
));
//指定端口监测的事件集
SetCommMask(m_hComm,EV_RXCHAR);
//分配串口设备缓冲区
SetupComm(m_hComm,512,512);
//初始化缓冲区中的信息
PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
CStringstrEvent;
strEvent.Format(L"
Com_ReadCloseEvent%d"
portNo);
m_hReadCloseEvent=CreateEvent(NULL,TRUE,FALSE,strEvent);
//创建串口读数据监听线程
m_hReadThread=CreateThread(NULL,0,ReadThreadFunc,this,0,&
m_dwReadThreadID);
串口打开成功"
m_bOpened=TRUE;
关闭串口
ClosePort()
//表示串口还没有打开
return;
CloseReadThread();
//关闭事件
CloseHandle(m_hReadCloseEvent);
m_bOpened=FALSE;
往串口写入数据
buf:
待写入数据缓冲区
bufLen:
待写入缓冲区长度
设置成功;
设置失败
WriteSyncPort(constBYTE*buf,DWORDbufLen)
DWORDdwNumBytesWritten;
DWORDdwHaveNumWritten=0;
//已经写入多少
intiInc=0;
//如果次写入不成功,返回FALSE
ASSERT(m_hComm!
=INVALID_HANDLE_VALUE);
do
if(WriteFile(m_hComm,
//串口句柄
buf+dwHaveNumWritten,
//被写数据缓冲区
bufLen-dwHaveNumWritten,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 串口 操作