Troubles with SerialPort
-
Hello,
I'm new using qt and I have some troubles with QSerialPort. I'm trying to connect Pixhawk to my computer with the USB port and make a little code to create a txt file that contains the information that send the Pixhawk. This is my code:myserialport.cpp
#include "myserialport.h" #include <QObject> #include <QtSerialPort/QSerialPort> #include <QDebug> #include<QFile> #include <QTextStream> #include<QString> MySerialPort::MySerialPort() { serial = new QSerialPort(this); data= ""; connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); } void MySerialPort::openSerialPort() { serial->setPortName("COM4"); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->open(QIODevice::ReadOnly); } void MySerialPort::closeSerialPort() { if(serial->isOpen()){ serial->close(); } } void MySerialPort::readData() { /*data = serial->readAll(); data2 += QString::fromStdString(data.toStdString()); qDebug()<<data<<"\n la longitud es: "<<data2.length(); qDebug()<<data2.value(data2.length()-1); qDebug()<<"Proximo mensaje"; //data3 = data; //qDebug()<<data3<<"Next";*/ data.append(serial->readAll()); QByteArray data1 = serial->readAll(); data2 += QString::fromStdString(data1.toStdString()); // data3 = data3 + QString::fromStdString(data.toStdString()); //data.clear(); dataAux = QByteArray::fromHex(data); for(int i = 0; i<dataAux.size(); i++){ data3=data3+dataAux.data()[i]; } if(data2.length()>=10){ closeSerialPort(); qDebug()<<data; qDebug()<<"Los datos Auxiliares"; qDebug()<<dataAux; qDebug()<<" "; qDebug()<<" "; qDebug()<<"EL QString es "<<data3; QFile arch ("C:/Users/Alvaro/Desktop/DatosLecturaDron.txt"); QTextStream out(&arch); arch.open(QIODevice::ReadWrite| QIODevice::Truncate| QIODevice::Text); out<<data3; arch.close(); }else{ data.clear(); dataAux.clear(); } }
myserialport.h
#ifndef MYSERIALPORT_H #define MYSERIALPORT_H #include <QObject> #include <QtSerialPort/QSerialPort> class MySerialPort: public QObject { Q_OBJECT public: MySerialPort(); public slots: void openSerialPort(); void closeSerialPort(); void readData(); private: QSerialPort *serial; QByteArray data; QStringList data2; QString data3; QByteArray dataAux; }; #endif // MYSERIALPORT_H
main.cpp
#include <QCoreApplication> #include <QtSerialPort/QtSerialPort> #include <myserialport.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MySerialPort isSerialPort; isSerialPort.openSerialPort(); return a.exec(); }
My issue is that i can read the messages with the QByteArray and show it in the console. However I cannot write the txt file correctly.
For example, this is one message from the pixhawk:And this what i get in the txt file:
Can Anyone help me?
PD. sorry for my english, it's not my native language. -
@Dooham said in Troubles with SerialPort:
The pixhawk sends messages in form of string of number in hexadecimal.
that does not clarify much, can your data be any value between 0x00 (0 in dec) and 0xFF(255 in dec) ? or is it limited between 0x30(45 in dec) and 0x39(57 in dec) ( representing the string character 0 -9) and 0x41(65 in dec) and 0x5A(90 in dec) (representing the string character A - Z)?
-
Do you mean those HEX'es are numeric only? You could QByteArray:split this array to separate strings containing your HEX of each number and convert it as written here
-
@Dooham Than a simple QString from QByteArray won't do it.
but this should work:
void MySerialPort::readData() { QByteArray data = serial->readAll(); QString str = QString(data.toHex(' ')); // The space is for readability, will separate each value by a space QFile f (myFile.txt) if(f.open(QIODevice::WriteOnly){ QTextStream out(&f); out << str; f.close(); } }