资源简介
C++串口读写操作封装类,简单易用,注释详细
代码片段和文件信息
// Serial.cpp
#include “stdafx.h“
#include “Serial.h“
CSerial::CSerial()
{
memset( &m_OverlappedRead 0 sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite 0 sizeof( OVERLAPPED ) );
m_hIDComDev = NULL;
m_bOpened = FALSE;
m_nCommNo = 0;
}
CSerial::~CSerial()
{
Close();
// TRACE(“Close Com%d\n“ m_nCommNo);
}
BOOL CSerial::Open( UINT nPort DWORD dwBaud BYTE byByteSize BYTE byParity BYTE byStopBits)
{
if( m_bOpened ) return TRUE;
char szPort[15];
m_nCommNo = nPort;
//wsprintf( szPort “COM%d“ m_nCommNo );
wsprintf( szPort “\\\\.\\COM%d“ m_nCommNo ); //这种格式才能打开大于9的串口!2003.12
m_hIDComDev = CreateFile( szPort GENERIC_READ | GENERIC_WRITE 0 NULL OPEN_EXISTING FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED NULL );
if( m_hIDComDev == NULL )
return FALSE;
memset( &m_OverlappedRead 0 sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite 0 sizeof( OVERLAPPED ) );
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts( m_hIDComDev &CommTimeOuts );
m_OverlappedRead.hEvent = CreateEvent( NULL TRUE FALSE NULL );
if (m_OverlappedRead.hEvent == NULL)
{
return FALSE;
}
m_OverlappedWrite.hEvent = CreateEvent( NULL TRUE FALSE NULL );
if (m_OverlappedWrite.hEvent == NULL)
{
return FALSE;
}
DCB dcb;
ZeroMemory(&dcb sizeof(DCB));
dcb.DCBlength = sizeof( DCB );
GetCommState( m_hIDComDev &dcb );
dcb.BaudRate = dwBaud;
dcb.ByteSize = byByteSize;
dcb.Parity = byParity;
dcb.StopBits = byStopBits;
//以下三行用于设置流控制为无
dcb.fOutX = FALSE;
dcb.fRtsControl = FALSE;
dcb.fOutxCtsFlow = FALSE;
if (!SetCommState( m_hIDComDev &dcb ))
{
DWORD dwError = GetLastError();
return FALSE;
}
if (!SetupComm( m_hIDComDev 2000 2000 ))
{
DWORD dwError = GetLastError();
return FALSE;
}
m_bOpened = TRUE;
SetCommMask(m_hIDComDevEV_RXCHAR|EV_TXEMPTY);
return m_bOpened;
}
BOOL CSerial::Close( void )
{
// if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );
if( m_OverlappedRead.hEvent)
{
CloseHandle( m_OverlappedRead.hEvent );
m_OverlappedRead.hEvent = NULL;
}
if( m_OverlappedWrite.hEvent)
{
CloseHandle( m_OverlappedWrite.hEvent );
m_OverlappedWrite.hEvent = NULL;
}
if (m_hIDComDev)
CloseHandle( m_hIDComDev );
m_bOpened = FALSE;
m_hIDComDev = NULL;
return TRUE;
}
BOOL CSerial::WriteCommByte( unsigned char ucByte )
{
BOOL bWriteStat;
DWORD dwBytesWritten;
bWriteStat = WriteFile( m_hIDComDev (LPSTR) &ucByte 1 &dwBytesWritten &m_OverlappedWrite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
if( WaitForSingleobject( m_OverlappedWrite.hEvent 1000 ) ) dwBytesWritten = 0;
else{
GetOverlappedResult( m_h
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6886 2006-03-08 10:12 Serial\Serial.cpp
文件 983 2006-03-06 10:56 Serial\Serial.h
目录 0 2014-04-30 20:53 Serial
----------- --------- ---------- ----- ----
7869 3
- 上一篇:基于遗传算法的最短路径的程序的开发C++
- 下一篇:RSA加解密c语言实现.zip
相关资源
- 基于遗传算法的最短路径的程序的开
- c++公司员工管理系统
- c++ 实现matlab smooth 函数
- C++ 课程设计 扫雷系统 报告+源代码
- C++文件加密系统
- 贝塞尔曲面 vc++ opengl
- alphabeta剪枝算法的C++实现下棋程序
- 操作系统的理发师问题解决文件打包
- C++11标准发布文档
- c++ MODBUS串行通信程序
- 成组链接法UNIXC++
- 操作系统调度算法c语言实现
- 维特比译码+卷积码编码程序C/C++实现
- 学分管理系统 c++
- C++五子棋源码有AI,先手禁手
- 双语版C++苏小红课后习题参考
- C++大作业之学生管理系统图表
- 基于Windows Socket的安全通信(C++实现)
- 蓝桥杯青少组C++赛前集训包.pdf
- 11个常用OpenCV+C++图像处理
- 吃豆人 c++ 源程序
- 求公式的主合取范式和主析取范式
- C++实现MATLAB的filter函数
- 实验室设备管理系统C++
- C++课程设计之简易英汉字典
- 传热学非稳态热传导问题网格划分数
- 学分管理系统c++课程设计
- 阿伦方差的C++ 版本
- 如何在VC++ 6.0创建RTX项目
- utf8编码解码的纯C++代码
评论
共有 条评论