Search code examples
qtxml-parsingqextserialport

Qextserialport breaks my xml


I am trying to rewrite in c++ an application written in python.

All it does is to open a serial port and read some xml. In python i was using pyserial to read the xml and beautifulsoup to retrieve information. The output was like this.

<file><property>xx32</property></file>

Now i am using qextserialport to read from the serial port and the xml i get is something like this.

<
fil
e>
<prope
rty>xx32
</prop
erty>
</
file>

My problem is that i cant parse an xml like this. I get errors.

EDIT:


Qextserialport reads data from the serial port in set of bytes that are not fixed. So how do i concatenate my xml into one string? I get an xml string every 4-5 seconds from the serial port.

here is my code

this->port = new QextSerialPort(com_port,QextSerialPort::EventDriven);
port->setBaudRate(BAUD57600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->setTimeout(0);

if (port->open(QIODevice::ReadOnly) == true)
{
    //qDebug()<< "hello";
    connect(port,SIGNAL(readyRead()),this,SLOT(onReadyRead()));
}

and the function that actually reads from the serial port

void CurrentCost::onReadyRead()
{
    QByteArray bytes;
    bytes = port->readAll();

    //qDebug() << "bytes read:" << bytes.size();
    //qDebug() << "bytes:" << bytes;
    ui->textBrowser->append(bytes);

}

Solution

  • I mean something like this:

    class CurrentCost...{
    
    private:
        QByteArray xmlData;
    private slots:
       void onReadyRead();
    
    };
    
    void CurrentCost::onReadyRead()
    {
        xmlData.append(port->readAll());
    
        if(xmlData.endsWith(/*what your port sending then xml is over&*/))
        {
            ui->textBrowser->append(xmlData);
            xmlData.clear();
        }
    
    }