windows串口编程C++ 下载本文

程序分为两个部分。 /* MySerialPort.h

封装了串口通讯的windows API。 支持异步操作串口。 */

#pragma once

#include

namespace jeff_class {

class CMySerialPort {

public:

CMySerialPort(); ~CMySerialPort();

BOOL Open ( int nPort = 2, int nBaud = 9600 ); BOOL Close ( void );

int ReadData ( void *, int );

int SendData ( const char *, int );

int ReadDataWaiting ( void ); // 查询缓冲区内是否有未读取的数据

BOOL IsOpened ( void ) { return ( m_bOpened ); }

protected:

BOOL WriteCommByte ( unsigned char ); // 内部实现,向串口写数据

HANDLE m_hIDComDev;

OVERLAPPED m_OverlappedRead, m_OverlappedWrite; BOOL m_bOpened; }; }

/ ****MySerialPort.cpp***********/

#include \#include

// 不使用读超时

#define TIMEOUT_READ_INTERVAL 0xFFFFFFFF

#define TIMEOUT_READ_TOTAL_MULTIPLIER 0 #define TIMEOUT_READ_TOTAL_CONSTANT 0 // 写超时为秒

#define TIMEOUT_WRITE_TOTAL_MULTIPLIER 0 #define TIMEOUT_WRITE_TOTAL_CONSTANT 5000

// 推荐的输入/输出缓冲区(注意:实际值由系统设置) #define BUFFER_INPUT_RECOMMEND 10000 #define BUFFER_OUTPUT_RECOMMEND 10000

// 异步读取/写入操作时等待事件的超时时间 #define TIMEOUT_READCOMM_EVENT 4000 #define TIMEOUT_WRITECOMM_EVENT 2000

// 一些通讯协议使用的宏

#define FC_DTRDSR 0x01 #define FC_RTSCTS 0x02 #define FC_XONXOFF 0x04 #define ASCII_BEL 0x07 #define ASCII_BS 0x08 #define ASCII_LF 0x0A #define ASCII_CR 0x0D #define ASCII_XON 0x11 #define ASCII_XOFF 0x13

jeff_class::CMySerialPort::CMySerialPort() {

memset ( &m_OverlappedRead, 0, sizeof ( OVERLAPPED ) ); memset ( &m_OverlappedWrite, 0, sizeof ( OVERLAPPED ) ); m_hIDComDev = NULL; m_bOpened = FALSE; }

jeff_class::CMySerialPort::~CMySerialPort() {

Close(); }

// 打开串口

BOOL jeff_class::CMySerialPort::Open ( int nPort, int nBaud ) {

if ( m_bOpened ) return TRUE;

TCHAR szPort[50];

TCHAR szComParams[50]; DCB dcb;

wsprintf ( szPort, _T ( \ ), nPort );

// API:建立文件,Windows中将串口设备当做文件对待 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 = TIMEOUT_READ_INTERVAL;

CommTimeOuts.ReadTotalTimeoutMultiplier = TIMEOUT_READ_TOTAL_MULTIPLIER; CommTimeOuts.ReadTotalTimeoutConstant = TIMEOUT_READ_TOTAL_CONSTANT; CommTimeOuts.WriteTotalTimeoutMultiplier= TIMEOUT_WRITE_TOTAL_MULTIPLIER;

CommTimeOuts.WriteTotalTimeoutConstant = TIMEOUT_WRITE_TOTAL_CONSTANT;

SetCommTimeouts ( m_hIDComDev, &CommTimeOuts );

wsprintf ( szComParams, _T ( \ ), 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, BUFFER_INPUT_RECOMMEND, BUFFER_OUTPUT_RECOMMEND ) ||

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 jeff_class::CMySerialPort::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 ); }