4

这是我在这个网站上的第一个问题!

我从一个 COM 端口读取数据时遇到了一些问题,我从另一个 COM 端口发送了一条完整的消息,当我用 Qt 接收它时,它总是被分成多个子消息。

void SerialPortReader::init()
{
    connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
}   

void SerialPortReader::readData()
{
//    m_serialPort->waitForReadyRead(200);
    QByteArray byteArray = m_serialPort->readAll();
    qDebug() << byteArray;

    if(byteArray.startsWith(SOF) && byteArray.endsWith(EOF_LS)
        && byteArray.size() >= MIN_SIZE_DATA) {
    decodeData(byteArray.constData());
    } else {
        qDebug() << "LIB SWCom : Unvalid trame !";
    }
}

发送的消息是 25 或 27 字节长,如果我使用 Putty 或超级终端来阅读它们,我没有问题。此外,如果我使用 2 个仿真串行端口 COM 进行通信,我没有这个问题......它只发生在 Qt 读取系统和 2 个物理 COM 端口......

我想我没有得到 readyRead 信号准确发出的时间......

我很困惑,提前感谢您的帮助!

4

3 回答 3

11

文档实际上对此非常清楚:

void QIODevice::readyRead() [信号]

每次有新数据可用于从设备读取时,都会发出一次此信号。它只会在新数据可用时再次发出,例如当新的网络数据有效负载到达您的网络套接字时,或者当新的数据块已附加到您的设备时。

readyRead() 不会递归发出;如果您重新进入事件循环或在连接到 readyRead() 信号的插槽内调用 waitForReadyRead(),则不会重新发送该信号(尽管 waitForReadyRead() 仍可能返回 true)。

实现从 QIODevice 派生的类的开发人员注意:当新数据到达时,您应该始终发出 readyRead() (不要仅仅因为缓冲区中还有数据要读取而发出它)。不要在其他条件下发出 readyRead()。

这意味着并不能真正保证有多少数据可供读取,只是有一些数据可用。

如果您希望读取的数据多于一次读取的数据,您可以选择超时值和/或 readyRead。这取决于您要达到的目标。

请参阅我不久前为此操作编写的命令行异步读取器示例:

#include "serialportreader.h"

#include <QCoreApplication>

QT_USE_NAMESPACE

SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent)
    : QObject(parent)
    , m_serialPort(serialPort)
    , m_standardOutput(stdout)
{
    connect(m_serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError)));
    connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));

    m_timer.start(5000);
}

SerialPortReader::~SerialPortReader()
{
}

void SerialPortReader::handleReadyRead()
{
    m_readData.append(m_serialPort->readAll());

    if (!m_timer.isActive())
        m_timer.start(5000);
}

void SerialPortReader::handleTimeout()
{
    if (m_readData.isEmpty()) {
        m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
    } else {
        m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
        m_standardOutput << m_readData << endl;
    }

    QCoreApplication::quit();
}

void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
    if (serialPortError == QSerialPort::ReadError) {
        m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
        QCoreApplication::exit(1);
    }
}

在这种情况下,命令行阅读器示例将获取一次性传递的任何数据,但它不保证长度或任何东西。

另外,请注意,您评论背后的同步 API 与您询问的异步 API 没有多大意义。我指的m_serialPort->waitForReadyRead(200);是这里。

于 2014-10-28T16:09:07.577 回答
1

只要有待处理的数据并且之前的 readyRead 执行完成,就会发出 readyRead 信号。

文档说:

每次有新数据可用于从设备读取时,都会发出一次此信号。它只会在新数据可用时再次发出,例如当新的网络数据有效负载到达您的网络套接字时,或者当新的数据块已附加到您的设备时。

readyRead() 不会递归发出;如果您重新进入事件循环或在连接到 readyRead() 信号的插槽内调用 waitForReadyRead(),则不会重新发送该信号(尽管 waitForReadyRead() 仍可能返回 true)。

如果这会导致您遇到任何问题,您可以使用 while 循环来检查 bytesAvailable()。

于 2014-10-28T16:02:39.967 回答
1

Use Thread concept here , using threads you can achieve this... run your portread in a seperate thread then it will be works fine, other wise the message will be cut. other method u can use Qprocess and open a new terminal using QProcess and send the port number and connectivity details to that terminal and run it from Qt then also u can archive this

i mean we can use therad and socket communication here look at this code

    QTcpSocket *SocketTest::getSocket() {
        return socket;
    }

    void SocketTest::Connect()

    {

        socket = new QTcpSocket(this);
        socket->connectToHost("127.0.0.1",22);
        if(socket->waitForConnected(3000))
        {

            qDebug()<<"connect";
            socket->waitForBytesWritten(3000);
            socket->waitForReadyRead(1000);
         Settings::sockdata=socket->readAll();
           qDebug()<<Settingssock::sockets;
           socket->close();

        }
        else
        {
             qDebug()<<" not connect";

        }
    }


............ call socket 

 SocketTest sock;
    sock.Connect();
    QTcpSocket * socket = sock.getSocket();
       QObject *ob= new QThread;
       mythread thread1(socket);
    thread = new mythread(socket);
    thread.run();
    thread->start();
   thread->wait();

this is a simple example of telnet connection, likewise u can specify which port you want connect and connected to which ip

于 2014-10-28T16:36:01.673 回答