2011-07-09 36 views
0

我想在C++中重寫一個用python編寫的應用程序。Qextserialport打破我的xml

它所做的只是打開串口並讀取一些xml。在python中,我使用pyserial讀取xml和beautifulsoup來檢索信息。輸出是這樣的。

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

現在我使用qextserialport從串口讀取和我得到的XML是這樣的。

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

我的問題是,我不能解析這樣的XML。我收到錯誤。

編輯:


Qextserialport從集合不是固定的字節的串行端口讀取數據。 那麼我如何將我的xml連接成一個字符串?我每隔4-5秒從串口獲得一個xml字符串。

這裏是我的代碼

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())); 
} 

和實際從串口

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

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

} 
+1

請上傳您使用的代碼重建XML。 – Mat

+0

這可能是問題所在。我必須重建它嗎? –

+0

用於從QextSerialPort中讀取的郵政編碼。也許你在那裏添加了不需要的'/ n'符號 – Raiv

回答

0

我的意思是這樣讀功能:

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(); 
    } 

} 
+0

不錯!我並不是不知道endsWith。 –