serial- read -its not wait to get the response and recieve ""- why?
-
hi,
Im doing application that supposed to read file and send line by line to the terminal.
and before it continue to the next line it supposed to recieve reply.
I did function and everything supposed to work well, but my problem is that when am doing that line:rep=m_serial->readAll();its not wait to recieve reply and rep =""......:(
it is not wait to recoeve response...
what can I add/ remove?the function that send the commands:
void SendCommands::StartToSendCommand( QString fileName)//Ruth { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QString line; m_ok=false; while (!in.atEnd()) { line = in.readLine(); m_line=line; for(int i=0; i< line.length(); ++i) { QString letter= (QString)line[i]; m_serial->write(letter.toLatin1()); } GetReplyFromCamera(line); m_ok=false; m_i=0; QApplication::processEvents(); } } }the function that recieve the reply:
void SendCommands::GetReplyFromCamera(QString line) { QString res, com; QByteArray rep; for(int i=0; i<line.length()+1/*must be changed!!*/; ++i) { m_i++; rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response... } m_reply+=rep; if(line[i] >= 48&& line[i] <= 57) { if(m_ok==false) { res=m_reply[m_i-1]; com=line[m_i-1]; m_index=m_i; m_ok=true; } } } IsReplyRight(res, com); m_command=""; } -
@RuWex said in serial- read -its not wait to get the response and recieve ""- why?:
its not wait to recieve reply and rep =""......:(
And therefore you have to use the signal readReady() and read out the bytes which are in the buffer. Then you have to parse them according to your protocol.
-
@RuWex said in serial- read -its not wait to get the response and recieve ""- why?:
its not wait to recieve reply and rep =""......:(
And therefore you have to use the signal readReady() and read out the bytes which are in the buffer. Then you have to parse them according to your protocol.
void SendCommands::GetReplyFromCamera(QString line) { QString res, com; QByteArray rep; for(int i=0; i<line.length()+1; ++i) { m_i++; m_serial->readyRead(); rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response... } m_reply+=rep; if(line[i] >= 48&& line[i] <= 57) { if(m_ok==false) { res=m_reply[m_i-1]; com=line[m_i-1]; m_index=m_i; m_ok=true; } } } IsReplyRight(res, com); m_command=""; }like this?
because its not work:( -
void SendCommands::GetReplyFromCamera(QString line) { QString res, com; QByteArray rep; for(int i=0; i<line.length()+1; ++i) { m_i++; m_serial->readyRead(); rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response... } m_reply+=rep; if(line[i] >= 48&& line[i] <= 57) { if(m_ok==false) { res=m_reply[m_i-1]; com=line[m_i-1]; m_index=m_i; m_ok=true; } } } IsReplyRight(res, com); m_command=""; }like this?
because its not work:(@RuWex
No. What do you expect statementm_serial->readyRead();to achieve?You need to read and understand that void QIODevice::readyRead() is a Qt signal, to which you need to attach a slot. And that slot needs to accumulate/append received bytes into some member variable buffer, which you then use use to access the bytes received when enough of them have been received/accumulated.
If you do not understand Qt signals and slots you need to read about them first, as you won't get anywhere in Qt programming without a thorough grasp of these.
-
@RuWex
No. What do you expect statementm_serial->readyRead();to achieve?You need to read and understand that void QIODevice::readyRead() is a Qt signal, to which you need to attach a slot. And that slot needs to accumulate/append received bytes into some member variable buffer, which you then use use to access the bytes received when enough of them have been received/accumulated.
If you do not understand Qt signals and slots you need to read about them first, as you won't get anywhere in Qt programming without a thorough grasp of these.
@JonB hi,
I am sorry, but Im new to qt and to the programming world...
I read it: https://doc.qt.io/qt-6/qiodevice.html#readyRead
but I still dont understand what I am supposed to do...\can you please make me a favor and Tell me a little more about it?
-
@JonB hi,
I am sorry, but Im new to qt and to the programming world...
I read it: https://doc.qt.io/qt-6/qiodevice.html#readyRead
but I still dont understand what I am supposed to do...\can you please make me a favor and Tell me a little more about it?
@RuWex
You need to start by reading and understanding https://doc.qt.io/qt-6/signalsandslots.html.You might look at an example for
readyReadsignal, like https://doc.qt.io/qt-6.2/qtserialport-terminal-example.html. Note that there theMainWindow::readData()slot callsreadAll()and then immediately deals with the data received (outputting it to the terminal in their case). In your case (I think, from what you are doing, but I don't know) you will need to append the freshly received bytes into a persistent buffer (member variable), so that you look through the buffer when you have received however many characters you expect/need. -
@RuWex
You need to start by reading and understanding https://doc.qt.io/qt-6/signalsandslots.html.You might look at an example for
readyReadsignal, like https://doc.qt.io/qt-6.2/qtserialport-terminal-example.html. Note that there theMainWindow::readData()slot callsreadAll()and then immediately deals with the data received (outputting it to the terminal in their case). In your case (I think, from what you are doing, but I don't know) you will need to append the freshly received bytes into a persistent buffer (member variable), so that you look through the buffer when you have received however many characters you expect/need.@JonB I still haven't had time to do everything you wrote, but I did the main thing,
and right now its not working:(MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow), m_status(new QLabel), m_console(new Console), m_settings(new SettingsDialog), m_serial(new QSerialPort(this)) { . . . connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData) . . . }readData:
QByteArray MainWindow::readData() { DBG; const QByteArray data = m_serial->readAll(); sendC->GetReplyFromCamera(data); m_console->putData(data); return data; }GetReplyFromUsb:
void SendCommands::GetReplyFromCamera(QString line) { QString res, com; QByteArray rep; for(int i=0; i<line.length()+1; ++i) { m_i++; m_serial->readyRead(); rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response... } m_reply+=rep; if(line[i] >= 48&& line[i] <= 57) { if(m_ok==false) { res=m_reply[m_i-1]; com=line[m_i-1]; m_index=m_i; m_ok=true; } } } IsReplyRight(res, com); m_command=""; }But it still doesn't work :(
Do you know why??? -
@JonB I still haven't had time to do everything you wrote, but I did the main thing,
and right now its not working:(MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow), m_status(new QLabel), m_console(new Console), m_settings(new SettingsDialog), m_serial(new QSerialPort(this)) { . . . connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData) . . . }readData:
QByteArray MainWindow::readData() { DBG; const QByteArray data = m_serial->readAll(); sendC->GetReplyFromCamera(data); m_console->putData(data); return data; }GetReplyFromUsb:
void SendCommands::GetReplyFromCamera(QString line) { QString res, com; QByteArray rep; for(int i=0; i<line.length()+1; ++i) { m_i++; m_serial->readyRead(); rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response... } m_reply+=rep; if(line[i] >= 48&& line[i] <= 57) { if(m_ok==false) { res=m_reply[m_i-1]; com=line[m_i-1]; m_index=m_i; m_ok=true; } } } IsReplyRight(res, com); m_command=""; }But it still doesn't work :(
Do you know why???@RuWex
Well done for connecting thereadyReadsignal to aMainWindow::readData()slot. But I'm afraid I don't think you have understood what you are doing, else you would have changed yourGetReplyFromCamera(QString line)code to work from the bytes received. Instead you still havem_serial->readyRead(); rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response...there, which makes no sense.
- Never call
m_serial->readyRead();yourself. - Never call
m_serial->readAll();anywhere other then in thereadyRead()slot (MainWindow::readData()).
Other than that, in principle if you want to pass the pass bytes received to
GetReplyFromCamera()and process them immediately there then you have appropriate code. Note that you may receive thereadyRead()signal and hence callGetReplyFromCamera()multiple times (there is no guarantee that all bytes sent from serial device will arrive in a singlereadyRead(), they could be split into more than one separate "packets"). If that is OK with your code/intention then it is fine. If you need to accumulate bytes received to do your work in one go (I don't know whether you do), then you have not done that part. - Never call
-
@RuWex
Well done for connecting thereadyReadsignal to aMainWindow::readData()slot. But I'm afraid I don't think you have understood what you are doing, else you would have changed yourGetReplyFromCamera(QString line)code to work from the bytes received. Instead you still havem_serial->readyRead(); rep=m_serial->readAll(); //here rep always will be "" its not wait for I will send response...there, which makes no sense.
- Never call
m_serial->readyRead();yourself. - Never call
m_serial->readAll();anywhere other then in thereadyRead()slot (MainWindow::readData()).
Other than that, in principle if you want to pass the pass bytes received to
GetReplyFromCamera()and process them immediately there then you have appropriate code. Note that you may receive thereadyRead()signal and hence callGetReplyFromCamera()multiple times (there is no guarantee that all bytes sent from serial device will arrive in a singlereadyRead(), they could be split into more than one separate "packets"). If that is OK with your code/intention then it is fine. If you need to accumulate bytes received to do your work in one go (I don't know whether you do), then you have not done that part.@JonB my problem is that I have to use
rep=m_serial->readAll();
becuse I already have in mainWindow indlude to sendCommand page,
and if I will do include to mainWindow in sendCommands it will make me The circularity problem...
so what do you think I should do? - Never call
-
@JonB my problem is that I have to use
rep=m_serial->readAll();
becuse I already have in mainWindow indlude to sendCommand page,
and if I will do include to mainWindow in sendCommands it will make me The circularity problem...
so what do you think I should do? -
@JonB my problem is that I have to use
rep=m_serial->readAll();
becuse I already have in mainWindow indlude to sendCommand page,
and if I will do include to mainWindow in sendCommands it will make me The circularity problem...
so what do you think I should do?@RuWex said in serial- read -its not wait to get the response and recieve ""- why?:
my problem is that I have to use
rep=m_serial->readAll();You're already doing that in MainWindow::readData()!
You can't simply get readAll() at arbitrary time and expect it to return something!
It will only return what is there (was received). So, only call it in MainWindow::readData().