Unsolved qextserial port in qt5.7.0
-
Dear Friends
I want to use qextserial port in qt5.7.0 vesrion i know the qextserial library for older qt versions.I have a source code that was previously written in qt4.8.7 but now i have upgraded my source code with newer version qt5.7.0 with help of the document Transition from Qt 4.x to Qt5 except qextserial
the source code was compiled and executed successfully without issue.
qextserial library is linked static include in the .pro file
So now my doubt is can i use qextserial lib in qt5.7.0?
Thanks and best regards
vivekyuvan -
Hi,
There’s no particular reason you can’t use it especially since it looks like it’s already working for you.
Just note that Qt 5 has now a dedicated module to handle serial ports.
-
Hi@SGaist
yes i know qt 5 has a qserial port . But my doubt is how to transit the qextserial code to qtserial portin my previous version code i know how to initialise and open the serial communication port
by this line
DataAcquisitionPort = new QextSerialPort("/dev/ttyUSB0", QextSerialPort::EventDriven); DataAcquisitionPort->setPortName("/dev/ttyUSB0"); DataAcquisitionPort->setBaudRate(BAUD115200); // To set the DataAcquisitionPort Baudrate to 115200 for Protocol DataAcquisitionPort->setFlowControl(FLOW_OFF); // To set the flow control to none DataAcquisitionPort->setParity(PAR_NONE); // To set the None pairity DataAcquisitionPort->setDataBits(DATA_8); // To set the bit length to 8 DataAcquisitionPort->setStopBits(STOP_1); // To set the stop bits to 1 DataAcquisitionPort->setTimeout(0);
But here in qt5.7.0 i don know how to initialise and open the communicaton port ttyUSB in qtserail port library
so can you show me an spinet or example code that will helpful
Thanks and regards
vivekyuvan -
@vivekyuvan have you taken a look at the docu yet?
http://doc.qt.io/qt-5/qserialport.htmlthe
QSerialport modul has all the functions you mention in the post
and the constuctor also excepts a string to the device
seems like you won't have to change much?
-
Hi@SGaist I have a problem while installing the qtserial port library here i pasted the error message
nu/qt5/QtCore -I.moc -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -o .obj/qserialport.o qserialport.cpp In file included from qserialport.cpp:47:0: qserialport_p.h:58:33: fatal error: private/qiodevice_p.h: No such file or directory compilation terminated. Makefile:402: recipe for target '.obj/qserialport.o' failed make[2]: *** [.obj/qserialport.o] Error 1 make[2]: Leaving directory '/home/vivek/qtserialport/src/serialport' Makefile:42: recipe for target 'sub-serialport-make_first' failed make[1]: *** [sub-serialport-make_first] Error 2 make[1]: Leaving directory '/home/vivek/qtserialport/src' Makefile:44: recipe for target 'sub-src-make_first' failed make: *** [sub-src-make_first] Error 2 vivek@vivek-Veriton-Series:~/qtserialport$ [0_1532751720829_gg](Uploading 100%)
-
Hi @vivekyuvan,
why do you need to build QtSerialPort yourself? It is already included in Qt since 5.1.
How did you install Qt 5.7? With the package manager or with the online installer?
BTW: You should better use Qt 5.9 LTS or 5.11 for new development.
Regards
-
hi@aha_1980 Yes indeed qtserial is already exists in qt5
But here I face an issue while calling the QSerial Header files with QT Creator Editor . My QT creator Doesn't includes the QSerial Header files
so that's why I did install qtserial port library manually. but now i got the header files in qt creator Editor with installing this command in terminal
$ sudo apt-get install libqt5serialport5-devis that ok correct me if i am wrong
Thanks & Regards
vivekyuvan -
$ sudo apt-get install libqt5serialport5-dev
that looks good, and should work.
Afterwards, you need to add
QT += serialport
in your.pro
file, as stated in the documentation -
@aha_1980 Thanks Now i am going to build my application hope it will build successfully.
-
@aha_1980 My code build and compiled successfully but my com port /dev/ttyUSB0 open was unsuccessful
Here is code snippet
void MainWindow::SerialPortInit() { DataAcquisitionPort = new QserialPort(); DataAcquisitionPort->setPortName("/dev/ttyUSB0"); DataAcquisitionPort->setBaudRate(QSerialPort::Baud115200); DataAcquisitionPort->setFlowControl(QSerialPort::NoFlowControl); DataAcquisitionPort->setDataBits(QSerialPort::Data8); DataAcquisitionPort->setParity(QSerialPort::NoParity); DataAcquisitionPort->setStopBits(QSerialPort::OneStop); qDebug() << "PORT Initialize completed"; bool res = false; res = DataAcquisitionPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Append | QIODevice::Text); connect(DataAcquisitionPort, SIGNAL(readyRead()), this, SLOT(Get_Data_from_Serial_port())); if(res) { qDebug() << "TRYING TO OPEN PORT" } else { qDebug("PROTOCOL PORT OPEN FAILED"); } }
this code cant open my port it always "protocol port open failed "
Help me out to resolve this issue
Thanks
-
@vivekyuvan said in qextserial port in qt5.7.0:
Hi, your code looks good so far, however this line:
bool res = false;
res = DataAcquisitionPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Append | QIODevice::Text);should be simplified to:
bool res = DataAcquisitionPort->open(QIODevice::ReadWrite);
I think open fails because you don't have the needed access rights.
Please send the output of
ls -l /dev/ttyUSB0
to verify.Usually the solution is to add your username to the group
dialout
Regards.
PS: It does not hurt to write "Please help me to solve this issue" ;)
-
@aha_1980 I have changed the line as like your simplified line. and again i got same error
vivek@vivek-Veriton-Series:~$ ls -l /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 Jul 28 13:25 /dev/ttyUSB0I have verified ttyUSB0 in terminal its looking fine
and i didn't understand the line " Usually the solution is to add your username to the group dialout
"
can you please show me how to add username to group dialout ?
-
Usually the solution is to add your username to the group dialout
In the terminal, input the following command:
sudo adduser vivek dialout
After that log out and in again or reboot the computer. Then it should work.
Regards
-
hi@aha_1980 thanks for reply. I have an another issue in my code that is " I can't get UART RAW data continuously " My code compiled successfully and when I run application it throws an error
ASSERT: "uint(i) < uint(size())" in file /usr/include/x86_64-linux-gnu/qt5/QtCore/qbytearray.h, line 464 The program has unexpectedly finished.
Serial port Initialize function
void MainWindow::SerialPortInit() { DataAcquisitionPort = new QSerialPort(); DataAcquisitionPort->setPortName("/dev/ttyUSB0"); DataAcquisitionPort->setBaudRate(QSerialPort::Baud115200); DataAcquisitionPort->setFlowControl(QSerialPort::NoFlowControl); // To set the flow control to none DataAcquisitionPort->setDataBits(QSerialPort::Data8); DataAcquisitionPort->setParity(QSerialPort::NoParity); DataAcquisitionPort->setStopBits(QSerialPort::OneStop); qDebug() << "PORT Initialize completed"; // bool res = false; // res = DataAcquisitionPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Append | QIODevice::Text); // To open the DataAcquisitionPort bool res = DataAcquisitionPort->open(QIODevice::ReadWrite); connect(DataAcquisitionPort, SIGNAL(readyRead()), this, SLOT(Get_Data_from_Serial_port())); if(res) { qDebug() << "TRYING TO OPEN PORT"; const char data[] = "PROTOCOL PORT OPEN SUCCESS"; qDebug() << data; DataAcquisitionPort->write(data, sizeof(data)); const char Protocol_init_data_1[] = { 0x80}; const char Protocol_init_data_2[] = { 0x04 }; const char Protocol_init_data_3[] = { 0x00 }; const char Protocol_init_data_4[] = { 0x80 }; const char Protocol_init_data_5[] = { 0x02 }; const char Protocol_init_data_6[] = { 0x00 }; DataAcquisitionPort->write(Protocol_init_data_1, sizeof(Protocol_init_data_1)); // Sleeper::msleep(100); QThread::msleep(100); DataAcquisitionPort->write(Protocol_init_data_2, sizeof(Protocol_init_data_2)); // Sleeper::msleep(100); QThread::msleep(100); DataAcquisitionPort->write(Protocol_init_data_3, sizeof(Protocol_init_data_3)); //Sleeper::msleep(100); QThread::msleep(100); DataAcquisitionPort->write(Protocol_init_data_4, sizeof(Protocol_init_data_4)); //Sleeper::msleep(100); QThread::msleep(100); DataAcquisitionPort->write(Protocol_init_data_5, sizeof(Protocol_init_data_5)); //Sleeper::msleep(100); QThread::msleep(100); DataAcquisitionPort->write(Protocol_init_data_6, sizeof(Protocol_init_data_6)); qDebug() << "PROTOCOL OPEN SUCCESS"; } else { qDebug("PROTOCOL PORT OPEN FAILED"); } }
this function for protocol raw data frame
void MainWindow::Get_Data_from_Serial_port() { QByteArray Protocol_Raw_Data; Protocol_Raw_Data = DataAcquisitionPort->readAll().toHex(); // Get the data and convert it to Hex fornat qDebug() << Protocol_Raw_Data; if((Protocol_Raw_Data.startsWith("02")) && (Protocol_Raw_Data.endsWith("03"))) // To check for the Complete Frame { P1.append(Protocol_Raw_Data); Protocol_Data_Organize(P1); P1.clear(); } else { P1.append(Protocol_Raw_Data); // If the frame is not completed, then append the data to Pi array } }
And this function for split the raw data and separate the into single frame
void MainWindow::Protocol_Data_Organize(QByteArray Data) { QList <QByteArray> Data_Frame; for(int pos = 0; pos < Data.size(); pos++) { if( (Data.at(pos-1) == '0' && Data.at(pos) == '3' && Data.at(pos+1) == '0' && Data.at(pos+2) == '2') || ((Data.at(pos) == '3') && pos == (Data.size()-1)) ) { Data.insert((pos+1), ';'); // Inserting a Splitter between the individual data frames } } Data_Frame << Data.split(';'); // Splits the Whole data frame into separate individual data frame and stores it in the List Protocol_Data_split(Data_Frame); }
In my previous version QT4.8.7 with qextserial library my application runs without issue but here i am facing this issue "The program has unexpectedly finished." and i cant get continuous UART Data . I don know what am missing please help me to resolve this issue
Thanks and Regard
vivekyuvan -
Hi @vivekyuvan said in qextserial port in qt5.7.0:
"The program has unexpectedly finished."
A typical case for using the debugger. Start a debugging session, on the crash you get a stack trace. Then inspect the different functions in the stack trace until you find the guilty one.
if( (Data.at(pos-1) == '0'
This already looks like a candidate when pos == 0.
-
Another thing I noticed: You are using signals&slots together with sleeping functions.
That is wrong - stop doing it, you putting yourself into trouble.