您的位置:首页 > 房产 > 建筑 > 邢台做网站的价格究竟多少钱?_工业软件开发流程_百度广告推广怎么做_做网络推广需要多少钱

邢台做网站的价格究竟多少钱?_工业软件开发流程_百度广告推广怎么做_做网络推广需要多少钱

2024/12/23 7:46:14 来源:https://blog.csdn.net/m0_53292003/article/details/144589566  浏览:    关键词:邢台做网站的价格究竟多少钱?_工业软件开发流程_百度广告推广怎么做_做网络推广需要多少钱
邢台做网站的价格究竟多少钱?_工业软件开发流程_百度广告推广怎么做_做网络推广需要多少钱

头文件

#include "InsScpi.h"
#include <QObject>
#include <QSerialPort>class TestSerial : public QObject
{Q_OBJECT
public:explicit TestSerial(QObject *parent = nullptr);//打开设备bool    openDevice(const QString &portName);//关闭设备void    closeDevice();//发送数据bool    write(const char* cmd, qint64 maxSize);//-- 获取温度 //-- 返回字符串格式://-- "22.3408    22.7402    24.3184    24.6043"QString getTemp(bool& ok, int msec = 500);signals:void    sig_finishReadSerial();     //完成串口读取信号private:void    init();                     //初始化函数private:QSerialPort*    m_serialPort    = nullptr;  //串口QString         m_portName      = "COM1";   //串口号//串口描述信息using QtSP = QSerialPort;QtSP::BaudRate   m_baudRate  = QtSP::Baud115200;    //波特率QtSP::DataBits   m_dataBits  = QtSP::Data8;         //数据位长度QtSP::Parity     m_parity    = QtSP::NoParity;      //校验位QtSP::StopBits   m_stopBits  = QtSP::OneStop;       //停止位
};

实现


#include <QDebug>
#include <QEventLoop>
#include <QTimer>TestSerial::TestSerial( QObject *parent): QObject{parent}
{(void)init();
}void TestSerial::init()
{//[1] 实例化串口对象if (!m_serialPort){m_serialPort = new QSerialPort();}
}bool TestSerial::openDevice(const QString &portName)
{//[1] 判断串口对象是否实例化if (!m_serialPort){qDebug() << "serial obj is null";return false;}//[2] 判断串口是否已经打开if (m_serialPort->isOpen()){qDebug() << "device is already open";return false;}//[3] 设置串口信息m_portName = portName;m_serialPort->setPortName(m_portName);m_serialPort->setBaudRate(m_baudRate, QSerialPort::AllDirections);m_serialPort->setDataBits(m_dataBits);m_serialPort->setParity(m_parity);m_serialPort->setStopBits(m_stopBits);m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//[4] 打开串口return m_serialPort->open(QIODevice::ReadWrite);
}void TestSerial::closeDevice()
{//[1] 判断串口对象是否实例化if (!m_serialPort){qDebug() << "serial obj is null";return;}//[2] 关闭串口m_serialPort->close();
}bool TestSerial::write(const char *cmd, qint64 maxSize)
{//[1] 判断串口对象是否实例化if (!m_serialPort){qDebug() << "serial obj is null";return false;}//[2] 判断串口是否打开if (!m_serialPort->isOpen()){qDebug() << "serial is not open";return false;}//[3] 发送数据if(maxSize != m_serialPort->write(cmd, maxSize)){qDebug() << "send serial data failed";return false;}return true;
}//-- 获取温度 
//-- 返回字符串格式:
//-- "22.3408    22.7402    24.3184    24.6043"
QString TestSerial::getTemp(bool& ok, int msec)
{static QString ret;ret.clear();if (!m_serialPort){qDebug() << "serial obj is null";return "";}QEventLoop loop;QObject::connect(this, &TestSerial::sig_finishReadSerial, &loop, &QEventLoop::quit);QTimer::singleShot(msec, this, [&]{ emit sig_finishReadSerial(); });QByteArray cmdBa = "getTemp()";m_serialPort->clear();if(0 != cmdBa.size()) {if(cmdBa.size() == m_serialPort->write(cmdBa)) ok = true;else ok = false;}auto async = QObject::connect(m_serialPort, &QSerialPort::readyRead, [&]() {auto rcvData = m_serialPort->readAll();ret = QString::fromLatin1(rcvData);emit sig_finishReadSerial();});loop.exec();QObject::disconnect(async);return ret;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com