Qt的串口通信例子

本文介绍了如何在Qt应用中使用QSerialPort和QSerialPortInfo类来查找并自动连接合适的串口。通过遍历可用的串口,实现与USB-SERIALCH340设备的通信,并提供串口状态监控和错误处理功能。
摘要由CSDN通过智能技术生成

如何查看自己的计算机中串口?

需要的头文件

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

一个是进行串口的通讯
一个是可以获取到串口的信息

综合使用这两个类,我们可以让判断哪一个串口是我们的程序需要使用的串口,并自动做一个连接。

#pragma once
#include <Windows.h>
#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QtDebug>
#include <QMessageBox>
#include <QTimer>
#include <QTime>
#include <QCoreApplication>

DWORD FindProcess(LPCTSTR modname, BOOL bAddExe);
HWND  GetProcessMainWnd(DWORD  dwProcessId);

///
/// \brief The Switch2 class
///1.与光开关通讯
///2。启动dvs软件,定时给dvs发送信息
///

class Switch2 : public QObject
{
Q_OBJECT

	bool m_close_flag;
	QSerialPort m_serial;
	QTimer m_timer;
	int m_port;
    int m_current_channel;
	QByteArray m_buffer;

signals:
	void ok();
	void error();	
	void timeout();
	void status(int);

private slots:
	//
	void recv_serial_data()
	{
		m_buffer += m_serial.readAll();
		int pos = m_buffer.indexOf(">");
		if (pos == -1) return;

		QByteArray data = m_buffer.left(pos + 1);
		m_buffer = m_buffer.mid(pos + 1);

		qDebug() << u8"串口读取: " << data.toStdString().c_str();
		emit ok();
		emit status(m_current_channel);
	}

	//
	void recv_timeout()
	{
		emit timeout();
	}

public:
	//
	Switch2()
	{	
		m_close_flag = false;
		m_current_channel = 1;
		connect(&m_serial, SIGNAL(readyRead()), this, SLOT(recv_serial_data()));
	}

    void sleepTime(unsigned int msec)
    {
        QTime reachTime=QTime::currentTime().addMSecs(msec);
        while(QTime::currentTime()<reachTime)
        {
            QCoreApplication::processEvents(QEventLoop::AllEvents,100);
        }

    }

    //打开指定的“USB-SERIAL CH340”串口
    bool autoOpen()
    {
        QList<QSerialPortInfo> serial_list=QSerialPortInfo::availablePorts();
        int serialsize=serial_list.size();
        qDebug()<<serial_list.size();
        for(int i=0;i<serialsize;i++)
        {
            QString flag=serial_list.at(i).description();
//            qDebug()<<i<<flag<<serial_list.at(i).isBusy()<<serial_list.at(i).manufacturer()<<serial_list.at(i).systemLocation()
//                   <<serial_list.at(i).productIdentifier()<<serial_list.at(i).vendorIdentifier();
            if(flag=="USB-SERIAL CH340")
            {
                qDebug()<<serial_list.at(i).portName();
                //QString port_name = QString("COM") + QString::number(port + 1);
                m_serial.setPortName(serial_list.at(i).portName());
                m_serial.setBaudRate(QSerialPort::Baud9600);
                m_serial.setParity(QSerialPort::NoParity);
                m_serial.setDataBits(QSerialPort::Data8);
                m_serial.setStopBits(QSerialPort::OneStop);
                m_serial.setFlowControl(QSerialPort::NoFlowControl);

                // 打开串口
                if (m_serial.open(QIODevice::ReadWrite))
                {
                    qDebug() << "open auto serial port: "<<m_serial.portName()<<" success";
                    return true;
                }
                else
                {
                    qDebug() << "open auto serial port failed";
                    return false;
                }
            }
        }
        qDebug() << "open serial don't have auto port";
        return false;

    }

    //自动识别和打开串口
    bool autoOpenSerial()
    {
        QList<QSerialPortInfo> serial_list=QSerialPortInfo::availablePorts();
        int serialsize=serial_list.size();
        qDebug()<<u8"串口数:"<<serial_list.size();
//做一个循环,让没一个串口都打开,然后发送一个与硬件匹配的消息,看硬件有没有给我们返回信息,就可以确定那一个串口是我们程序中要使用的串口了。
        //for(int i=0;i<serialsize;i++)
        for(int i=serialsize-1;i>=0;i--)
        {
            //qDebug()<<i;
            QString flag=serial_list.at(i).description();
            m_serial.setPortName(serial_list.at(i).portName());
            m_serial.setBaudRate(QSerialPort::Baud9600);
            m_serial.setParity(QSerialPort::NoParity);
            m_serial.setDataBits(QSerialPort::Data8);
            m_serial.setStopBits(QSerialPort::OneStop);
            m_serial.setFlowControl(QSerialPort::NoFlowControl);

            // 打开串口
            if (m_serial.open(QIODevice::ReadWrite))
            {
                QByteArray cmd = QByteArray::fromHex("A00102A3");
                m_serial.write(cmd);
                m_serial.write(cmd);
                m_serial.write(cmd);
                //延迟读取串口接收到的数据
                sleepTime(1000);
                if(m_buffer.size()>0)
                {
                    qDebug() << "open auto serial port: "<<m_serial.portName()<<" success";
                    m_buffer.clear();
                    m_buffer.clear();
                    return true;
                }
                else
                {
                    qDebug() << "open auto serial port: "<<m_serial.portName()<<" fail";
                    m_serial.close();
                }
            }
        }
        qDebug() << "open serial don't have auto port";
        return false;
    }

    //指定打开的串口号,如果没有打开,继续打开指定的“USB-SERIAL CH340”串口
	bool open(int port)
	{
        qDebug()<<"port:"<<port;
		m_close_flag = false;
		m_port = port;

        QString port_name = QString("COM") + QString::number(port + 1);
        m_serial.setPortName(port_name);
		m_serial.setBaudRate(QSerialPort::Baud9600);
		m_serial.setParity(QSerialPort::NoParity);
		m_serial.setDataBits(QSerialPort::Data8);
		m_serial.setStopBits(QSerialPort::OneStop);
		m_serial.setFlowControl(QSerialPort::NoFlowControl);

		// 打开串口
		if (m_serial.open(QIODevice::ReadWrite))
		{
            qDebug()<<m_serial.portName();
            qDebug() << "open serial port:"<<m_serial.portName()<< " success";
			return true;
		}
		else
		{
            qDebug() << "open serial port auto to open";
            return autoOpen();
		}
	}

	//
	void close()
	{
		m_close_flag = true;
		m_serial.close();
	}


	//
	void try_open_serial()
	{
		if (m_close_flag) return;
		if (m_serial.isOpen()) return;

		// 尝试重连
		if (m_serial.open(QIODevice::ReadWrite))
		{
			qDebug() << "open serial port success";
		}
		else
		{
			QTimer::singleShot(1000, this, SLOT(try_open_serial()));
		}
	}

    //切换通道,并发送消息给dvs
    void select(int channel)
    {
        //如果没有选择通道1或者通道2,就默认通道1
        QByteArray cmd = QByteArray::fromHex("A00102A3");
        if (channel == 1)
        {
            cmd = QByteArray::fromHex("A00102A3");
        }
        else if (channel == 2)
        {
            cmd = QByteArray::fromHex("A00103A4");
        }
        m_serial.write(cmd);
        m_serial.write(cmd);
        m_serial.write(cmd);

		qDebug() << u8"串口发送:" << cmd.toHex();
        m_current_channel = channel;
        sendToDvs(channel);

		QTimer::singleShot(1000, this, SLOT(recv_timeout()));
	}

	int currentChannel()
	{
		return m_current_channel;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值