#include "rtuport.h" RtuPort::RtuPort() { m_serialPort = new QSerialPort(); //设置参数 m_serialPort->setPortName(GlobalInfo::this_()->config().portName); m_serialPort->setBaudRate(GlobalInfo::this_()->config().portBaudRate.toInt(),QSerialPort::AllDirections);//设置默认波特率和读写方向 m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位 m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制 m_serialPort->setParity(QSerialPort::NoParity); //无校验位 m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位 connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(receiveInfo())); ret = QByteArray::fromHex("010300000006C5C8"); portTimer = new QTimer(this); connect(portTimer,&QTimer::timeout,this,&RtuPort::portStart); portTimer->start(1000); } void RtuPort::portStart() { m_serialPort->open(QIODevice::ReadWrite); if(!m_serialPort->isOpen()){ qDebug()<<"串口未打开"; return; } qDebug("串口已开启"); portTimer->stop(); timer = new QTimer(this); connect(timer,&QTimer::timeout,this,&RtuPort::portWrite); timer->start(200); } void RtuPort::portWrite() { // qDebug()<<"测试"; // if(hexData.isEmpty()){ // qDebug()<<"null"; // timer->stop(); // portTimer->start(1000); // } m_serialPort->write(ret); } void RtuPort::receiveInfo() { QByteArray info = m_serialPort->readAll(); QByteArray hexData = info.toHex().data(); // qDebug()<<"receiveInfo测试:"<