资源简介
C++ 串口类 串口通信 RS232 不需要使用微软件的串口控件
代码片段和文件信息
#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;
}
CSerial::~CSerial()
{
Close();
}
BOOL CSerial::Open( int nPort int nBaud )
{
if( m_bOpened ) return( TRUE );
char szPort[15];
char szComParams[50];
DCB dcb;
wsprintf( szPort “COM%d“ nPort );
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 );
wsprintf( szComParams “COM%d:%dn81“ nPort nBaud );
m_OverlappedRead.hEvent = CreateEvent( NULL TRUE FALSE NULL );
m_OverlappedWrite.hEvent = CreateEvent( NULL TRUE FALSE NULL );
dcb.DCBlength = sizeof( DCB );
GetCommState( m_hIDComDev &dcb );
dcb.BaudRate = nBaud;
dcb.ByteSize = 8;
unsigned char ucSet;
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
if( !SetCommState( m_hIDComDev &dcb ) ||
!SetupComm( m_hIDComDev 10000 10000 ) ||
m_OverlappedRead.hEvent == NULL ||
m_OverlappedWrite.hEvent == NULL )
{
DWORD dwError = GetLastError();
if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
CloseHandle( m_hIDComDev );
return( FALSE );
}
m_bOpened = TRUE;
return( m_bOpened );
}
BOOL CSerial::Close( void )
{
if( !m_bOpened || m_hIDComDev == NULL )
return( TRUE );
if( m_OverlappedRead.hEvent != NULL )
CloseHandle( m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL )
CloseHandle( m_OverlappedWrite.hEvent );
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( !bWriteS
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 974 2011-05-12 16:50 Serial.h
文件 4823 2011-05-12 16:16 Serial.cpp
----------- --------- ---------- ----- ----
5797 2
相关资源
- c++ 写的电梯调度程序
- ADO DATA控件
- Canny 边沿提取算子 C++代码实现
- 拼写纠错 拼写检查 c++
- C++从YUV420序列中获得指定一帧数据(
- 将png格式图片转换成YUV格式,C++
- c++教程网的linux网络编程视频
- SPOOLing算法模拟 C++实现
- 操作系统3种页面置换算法 C++实现
- VC++ 四种进程间通信的完整
- 学生社团管理程序 c++ 课程设计
- C++写的家庭小记账本
- 多机调度问题C++语言解决的源代码
- C++ 停车场管理 数据结构 课程设计
- 书店管理系统 C++
- 八数码游戏程序人工智能,c++
- 一个简单的vs c++ socket通讯程序
- 程序流程图生成器支持C/C++
- C++代码转换成C#工具
- 飞行棋c++源码初学者学习用
- MFC小学生速算练习系统VS2017
- xcs 基于遗传算法的自动学习分类器系
- 基于VS2008和IVF11的C/C++和Fortran混合编程
- 使用标准C++库实现的WAV音频文件读写
- C++ 带接口类动态链接库开发
- C++控制播放/插入PPT等操作
- BCH(168)编解码,c++实现
- C++版本的FFT傅立叶变换程序源代码
- 随机产生大素数的C++程序
- Lua源码和Lua在C++的使用
评论
共有 条评论