QSerialPort questions
-
Hello,
I start a new thread about QSerialPort, with new questions about:
- In my case I send data from a while loop . I get data from device using readyRead signal, I check that data was write to device using bytesWritten signal. I don't know if I write correct to device because if I just use port->write - data is not send so I must put waitForBytestWritten(50) to send data.
Is this approach correct?
After a set of data sended to device and get reponse from it ( for example after 5 writes to device) , write function returns ok but data is not send. It is possible that QSerialPort stop write to device due to a possible buffer overflow?
I try this function port->clear() but I get errors after using it?
What approach do you suggest I use?
I forget to mention that read and write to port are made from main function before return app.exec().
I can use in non-blocking mode waitForBytes.. to write to device or it should work using just write function?
Thanks in advance.
- In my case I send data from a while loop . I get data from device using readyRead signal, I check that data was write to device using bytesWritten signal. I don't know if I write correct to device because if I just use port->write - data is not send so I must put waitForBytestWritten(50) to send data.
-
[quote author="talexcomputers" date="1422867809"] In my case I send data from a while loop[/quote]
This has improtant implications: a loop will block your CPU. It will constantly be going through the loop code, giving the serial port no time to actually send or receive the data. Your other problems might stem from this. You need to gice the event loop some breathing space.
And, of course, you need to start the event loop (exec()), before you start your while loop.
-
Hello,
Thanks for quick answer.This is the code. Could you help me identify other possible problems? (Except already identified problems).
This is main function of app:
@#include <QtWidgets/QApplication>
#include "driver.h"int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Driver driver;
driver.openPort("COM3");
if (driver.isPortOpen()) {QList<int> message; message.append(1); message.append(1); message.append(3); int pos = 0; while (pos < message.length()) { if (driver.getFunctionIsCalled() == false) { if(!driver.isPortOpen()) { driver.openPort("COM3"); } if (driver.testPort(message[pos]) == true) { pos++; driver.setFunctionCalled(false); } else { pos++; driver.setFunctionCalled(false); } } } if (driver.isPortOpen()) { driver.clearBuffer("COM3"); driver.closePort(); } return 0; } return app.exec();
}@
This are functions used for writing to device :
@void Driver::sendData(QByteArray message) {
int bytesSend;
bytesSend = port->write(message);
port->waitForBytesWritten(500);
if (bytesSend < message.length()) {
QMessageBox::information(0, "Error writing", "Bytes written are less then message length");
}
if (bytesSend == -1) {
QMessageBox::information(0, "Error writing", "Error writing - com error : " + port->errorString());
}
port->flush();
}@Function that calls sendData method:
@bool Driver::testPort(int message) {bool firstOk = false; bool linePrinted = false; bool is_13 = true; int tries = 0; functionCalled = true; while (1) { if (is_13 == true) { sendData(takePort); is_13 = false; } sendData(selectPortById); if (getReadData().length() >= 25 && firstOk == false) { sendData(okMessage); firstOk = true; readData.clear(); is_13 = true; } if (linePrinted == true) { if (getReadData().contains(okResponse)) { readData.clear(); QMessageBox::information(0, "INFO", "Data printed"); port->clear(); port->close(); break; } else { linePrinted = false; is_13 = true; } } if (linePrinted == false && tries == 10) { QMessageBox::information(0, "ERROR","Linie neprintara: " + port->errorString()); port->clear(); port->close(); return false; } if (getReadData().length() > 25 && firstOk == true && linePrinted == false) { sendData(okMessage); switch(message) { case 0 : { sendData(QByteArray::fromHex("FF FF FF FF FF 0A 48 3B 3B 3B 3B 3B 3B 0D BF BA FF FF FF FF FF")); sendData(QByteArray::fromHex("0A 48 3B 3B 3B 3B 3B 3B 0D BF BA FF FF FF FF FF")); break; } case 1 : { sendData(QByteArray::fromHex("FF FF FF FF FF 0A 58 3B 41 41 3B 3B 3B 3B 3B 0D B9 7E FF FF FF FF FF")); sendData(QByteArray::fromHex("0A 58 3B 41 41 3B 3B 3B 3B 3B 0D B9 7E FF FF FF FF FF")); break; } case 2 : { sendData(QByteArray::fromHex("FF FF FF FF FF 0A 45 3B 30 3B 3B 3B 3B 3B 0D 69 A9 FF FF FF FF FF")); sendData(QByteArray::fromHex("0A 45 3B 30 3B 3B 3B 3B 3B 0D 69 A9 FF FF FF FF FF")); break; } case 3 : { sendData(QByteArray::fromHex("FF FF FF FF FF FF 0A 4C 3B 3B 41 43 49 43 4C 4F 56 49 52 3B 31 2E 30")); sendData(QByteArray::fromHex("30 3B 31 2E 30 30 30 3B 31 3B 31 3B 33 3B 30 3B 30 3B 0D C0 F0 FF FF FF FF FF")); sendData(QByteArray::fromHex("0A 4C 3B 3B 41 43 49 43 4C 4F 56 49 52 3B 31 2E 30 30 3B 31 2E")); sendData(QByteArray::fromHex("30 30 30 3B 31 3B 31 3B 33 3B 30 3B 30 3B 0D C0 F0 FF FF FF FF FF")); break; } } is_13 = true; linePrinted = true; tries++; } } return false;
}@
Thanks in advanced
-
Hello,
I take your advice and change code. I create a window application and put actions from while loop (which also disappear) to individual button.
So i have a button for each action. Now indeed from function sendData disappear the waitForBytes... and flush. After all this when i press button to take device ,this answer to me very quickly . Seems everything works well until i want to send the concrete data (it consists from 3 writes to device), but device does nothing at all.So i have tried to approaches :
- with wile loop and waitForBytes
- with gui and qt loop event
In both methods device answer when i send commands to set communication but not execute data for processing.
Any ideas?
I am under pressure with this project.
Any help and suggestion are welcome.Thanks in andvance.
have a nice day