problem with writing on serial port
-
@nanor said in problem with writing on serial port:
serial = new QSerialPort(this);
Remove "this".
If it is still crashing then please use the debugger to see where exactly and post the stack trace after crash. -
@nanor Please post the stack trace (as text). It's in the middle of your screen-shot, in "Debugger" section.
-
@nanor Please post what is in level 2 and 3, else I'm out of this thread as I do not want to guess what is wrong...
It is your job to find out where your app is crashing. -
@nanor Please post what is in level 2 and 3, else I'm out of this thread as I do not want to guess what is wrong...
It is your job to find out where your app is crashing.@jsulm Sorry again. I have another question. inside the while loop, I have to constantly read data from the port and store them inside an array adn then debug them . I have written the following line. but nothing is printed
while(1) { QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; } } -
@jsulm Sorry again. I have another question. inside the while loop, I have to constantly read data from the port and store them inside an array adn then debug them . I have written the following line. but nothing is printed
while(1) { QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; } } -
@nanor
Absolutely not! Since it'swhile(1)how is it ever going to exit the loop??while (1)is "always" wrong in Qt....You have now changed your code...
but nothing is printed
So either it never enters the loop, or it never has anything to read.
@JonB I removed the while loop and mythread.cpp is now:
#include "mythread.h" MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() { serial = new QSerialPort(); serial->setPortName("COM1"); for(const auto &serialPortInfo : QSerialPortInfo::availablePorts()) { qDebug() << "find serial port: " << serialPortInfo.portName() ; } serial->open(QIODevice::ReadWrite); if(serial->isOpen()) { serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; } } else { qDebug() << "can't open the port"; } }But still I can't get the messages. I send data from hercules.
-
@JonB I removed the while loop and mythread.cpp is now:
#include "mythread.h" MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() { serial = new QSerialPort(); serial->setPortName("COM1"); for(const auto &serialPortInfo : QSerialPortInfo::availablePorts()) { qDebug() << "find serial port: " << serialPortInfo.portName() ; } serial->open(QIODevice::ReadWrite); if(serial->isOpen()) { serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; } } else { qDebug() << "can't open the port"; } }But still I can't get the messages. I send data from hercules.
@nanor
Start by putting inqDebug()statements to see for yourself where you are actually getting to, plus more error checking.I don't know if any serial data is available to read at the instant you open the port. And you only try to read one byte once.
-
@nanor
Start by putting inqDebug()statements to see for yourself where you are actually getting to, plus more error checking.I don't know if any serial data is available to read at the instant you open the port. And you only try to read one byte once.
@JonB Thank you for mentioning using debug. I added the line qDebug() << "inside the run function"; inside the run function, but after running, this message isn't shown. But I have started the thread inside the mainwindow class. I can't understand why this is happened!
-
@JonB Thank you for mentioning using debug. I added the line qDebug() << "inside the run function"; inside the run function, but after running, this message isn't shown. But I have started the thread inside the mainwindow class. I can't understand why this is happened!
-
@JonB Thank you for mentioning using debug. I added the line qDebug() << "inside the run function"; inside the run function, but after running, this message isn't shown. But I have started the thread inside the mainwindow class. I can't understand why this is happened!
@nanor I gave you a link to an example which uses the synchronous API. Apparently you did not really read it carefully, did you? In your loop you should use waitForReadyRead(...) as is done in the example. This makes sure that the event loop can actually do its job.
if (serial.waitForReadyRead(currentWaitTimeout)) { QByteArray responseData = serial.readAll(); -
@nanor
My own view, often times expressed, is why are you going anywhere nearQThreadat all? Every time it causes posters problems. Qt framework is asynchronous, so why do you need any threads when you have signals? -
@jsulm said in problem with writing on serial port:
I asked already why threads. Seems to be an exercise.
possibly, but the OP only states the while loop as a task. Not threads specifically.
@nanor can you word exactly, what you want to do ?
-
@jsulm said in problem with writing on serial port:
I asked already why threads. Seems to be an exercise.
possibly, but the OP only states the while loop as a task. Not threads specifically.
@nanor can you word exactly, what you want to do ?
@J-Hilk Hi
I have a button that when a user pushes it, "salam" is printed. At the same time, I want to constantly read from and write to a port (I want to read byte by byte and print them(debug them)). Now I know that I have to declare a class inheritted from qThread class and inside its run function, read and write. But I don't know if I have to declare while loop inside the run function in order to read constantly or not. In the code I posted, I have:QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; }Is this part reading the datas byte by byte and store them inside an array and print the datas?
-
@J-Hilk Hi
I have a button that when a user pushes it, "salam" is printed. At the same time, I want to constantly read from and write to a port (I want to read byte by byte and print them(debug them)). Now I know that I have to declare a class inheritted from qThread class and inside its run function, read and write. But I don't know if I have to declare while loop inside the run function in order to read constantly or not. In the code I posted, I have:QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; }Is this part reading the datas byte by byte and store them inside an array and print the datas?
@nanor said in problem with writing on serial port:
Now I know that I have to declare a class inheritted from qThread class and inside its run function,
Why? Is this a required exercise to use a thread, or are you just doing it because you think you need to??
Oh, now I see
@jsulm I was asked to do this. I was asked to send a text in while(1) loop.
EDIT OK, I have seen now you have answered that you must do it this way. Seems terrible to me! But OK :)
-
@nanor said in problem with writing on serial port:
Now I know that I have to declare a class inheritted from qThread class and inside its run function,
Why? Is this a required exercise to use a thread, or are you just doing it because you think you need to??
Oh, now I see
@jsulm I was asked to do this. I was asked to send a text in while(1) loop.
EDIT OK, I have seen now you have answered that you must do it this way. Seems terrible to me! But OK :)
-
@J-Hilk Hi
I have a button that when a user pushes it, "salam" is printed. At the same time, I want to constantly read from and write to a port (I want to read byte by byte and print them(debug them)). Now I know that I have to declare a class inheritted from qThread class and inside its run function, read and write. But I don't know if I have to declare while loop inside the run function in order to read constantly or not. In the code I posted, I have:QByteArray data = serial->read(1); for(int i=0; i<data.size(); i++) { qDebug() << data[i]; }Is this part reading the datas byte by byte and store them inside an array and print the datas?
@nanor said in problem with writing on serial port:
I want to constantly read from and write to a port (
is it specified how you have to
constantly read from and write to a portor is that implementation detail up to you?because while- loops and threads I the least optimal way to do it