windows串口编程C++

更新时间:2023-07-22 19:46:41 阅读: 评论:0

程序分为两个部分。
/*    MySerialPort.h
    封装了串口通讯的windows API。
    支持异步操作串口。
*/
#pragma once
#include <windows.h>
namespace jeff_class
{
    class CMySerialPort
    {
    public:
        CMySerialPort();
习以为常        ~CMySerialPort();
        BOOL Open ( int nPort = 2, int nBaud = 9600 );
        BOOL Clo ( 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 "MySerialPort.h"
#include <tchar.h>
死亡教育// 不使用读超时
#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()
{
    memt ( &m_OverlappedRead, 0, sizeof ( OVERLAPPED ) );
    memt ( &m_OverlappedWrite, 0, sizeof ( OVERLAPPED ) );
    m_hIDComDev = NULL;
    m_bOpened = FALSE;
}
jeff_class::CMySerialPort::~CMySerialPort()
{
    Clo();
}
// 打开串口
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 ( "COM%d" ), 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 );
    memt ( &m_OverlappedRead, 0, sizeof有趣的课余生活 ( OVERLAPPED ) );
    memt ( &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 ( "COM%d:%d,n,8,1" ), 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 ) CloHandle ( m_OverlappedRead.hEvent );
        if ( m_OverlappedWrite.hEvent != NULL ) CloHandle ( m_OverlappedWrite.hEvent );
        CloHandle ( m_hIDComDev );
        return ( FALSE );
    }
    m_bOpened = TRUE;
    return ( m_bOpened );
}
// 关闭串口
BOOL jeff_class::CMySerialPort::Clo ( void )
{
    逍遥丸的功效if ( !m_bOpened || m_hIDComDev == NULL ) return ( TRUE );
    if ( m_OverlappedRead.hEvent != NULL ) CloHandle ( m_OverlappedRead.hEvent );

本文发布于:2023-07-22 19:46:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1111394.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:串口   读取   设备   缓冲区   设置   文件   写入   事件
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图