• 4323阅读
  • 1回复

[求职]qt extserial 串口接受数据问题 [复制链接]

上一主题 下一主题
离线onelook
 

只看楼主 倒序阅读 楼主  发表于: 2013-03-08
运行extserial 自带的 event例子
我在例外一台电脑打开串口调试工具,输入 abcde 然后点发送,但是到了qt接收这里,每个字符都会触发一个事件
如果我要一次收到完整的 abced 应该怎么改?

#include "PortListener.h"
#include <QtDebug>

PortListener::PortListener(const QString &portName)
{
    qDebug() << "hi there";
    this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
    port->setBaudRate(BAUD115200);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    port->setTimeout(50);

    if (port->open(QIODevice::ReadWrite) == true) {
        connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
        connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
        if (!(port->lineStatus() & LS_DSR))
            qDebug() << "warning: device is not turned on";
        qDebug() << "listening for data on" << port->portName();
    }
    else {
        qDebug() << "device failed to open:" << port->errorString();
    }
}

void PortListener::onReadyRead()
{
    QByteArray bytes;
    int a = port->bytesAvailable();
    bytes.resize(a);
//    port->read(bytes.data(), bytes.size());
    bytes=port->readAll();
    qDebug() << "bytes read:" << bytes.size();
    qDebug() << "bytes:" << bytes;

}

void PortListener::onDsrChanged(bool status)
{
    if (status)
        qDebug() << "device was turned on";
    else
        qDebug() << "device was turned off";
}



离线pxiao_xiao

只看该作者 1楼 发表于: 2013-03-08
onReadyRead()
里加入如下过滤
if (port->bytesAvailable() > num)   // num为字符个数
{
       QByteArray temp = port->readAll();                 //读取数据            
}
快速回复
限100 字节
 
上一个 下一个