write on serial port in thread
-
Do not use it in thread if you do not know what do you do (and how it works). Please, see the QSerialPort examples and documentation, and the Qt documentation (about signals/slots, reentrant, event-loop) at first. And then, please provide a complete code how you creates and works with QSerialPort.
Because I already see that you use QThread && QSerialPort wrong.
by asynchronous you mean without thread?
I tried to do so but it has a huge delay!!What you tried ? I do not see any code.
-
@kuzulis_
Yes I used QThread and QSerialPort wrong together but it's not mean that I don't know what I'm doing!
I'm newbie in qt and still have trouble to use some of qt libraries.
but you are right. using thread was wrong at the first place! I didn't need it. Thanks to SGaist I realize it now!
I tried without thread before and it has delay. today I figured out using readyRead() signal is not my solution!
I used QTimer and 10 milliseconds for connecting to parser function(slot) and it's solved my problem for good.
thank you for your response. :) -
Can you show the last version of your working code ?
-
@SGaist
of course!
but I cant find how to show it as code in this new forum and I also cant find solved button!
anyway here is the code :Reader::Reader(QSerialPort *parent) : QSerialPort(parent) { serial = new QSerialPort; foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) serial->setPortName(serialPortInfo.portName()); serial->setBaudRate(QSerialPort::Baud115200); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setFlowControl(QSerialPort::NoFlowControl); serial->setStopBits(QSerialPort::OneStop); serial->open(QIODevice::ReadWrite); QTimer *timer = new QTimer(this); connect(timer , SIGNAL(timeout()) , this , SLOT(read())); timer->start(5); } int packet_counter = 0; int rssi = 2; QByteArray header; QByteArray payload; quint8 ID; void Reader::read() {//1 if(serial->bytesAvailable() >= 10) {//3 header = serial->read(1); if(header.contains(254)) {//4 header = serial->read(5); seq = header[1]; if((header.at(2) == 01) && (header.at(3) == 01)) {//7 ID = header.at(4); if(seq >= 0 && seq <=254) { packet_counter++; } if(seq == 255) { packet_counter++; if(packet_counter <= 256) { rssi = (packet_counter*100)/256; emit mRssi(rssi); } packet_counter = 1; } payload = serial->read(header.at(0)); switch (ID) { case (ATTITUDE) : { mavlink_attitude_t data; memcpy(&data,payload.data(),sizeof(mavlink_attitude_t)); emit attitude(data); } break; case (SCALED_PRESSURE) : { mavlink_scaled_pressure_t data; memcpy(&data,payload.data(),sizeof(mavlink_scaled_pressure_t)); emit scaled_pressure(data); } break; case (SENSOR_OFFSETS) : { mavlink_sensor_offsets_t data; memcpy(&data,payload.data(),sizeof(mavlink_sensor_offsets_t)); emit sensor_offsets(data); } break; case (SYS_STATUS) : { mavlink_sys_status_t data; memcpy(&data,payload.data(),sizeof(mavlink_system_time_t)); emit sys_status(data); } break; case (GPS_RAW_INT) : { mavlink_gps_raw_int_t data; memcpy(&data,payload.data(),sizeof(mavlink_gps_raw_int_t)); emit gps_raw_int(data); } break; case (SERVO_OUTPUT_RAW) : { mavlink_servo_output_raw_t data; memcpy(&data,payload.data(),sizeof(mavlink_servo_output_raw_t)); emit servo_output_raw(data); } break; case (RC_CHANNELS_RAW) : { mavlink_rc_channels_raw_t data; memcpy(&data,payload.data(),sizeof(mavlink_rc_channels_raw_t)); emit rc_channels_raw(data); } break; case (WIND) : { mavlink_wind_t data; memcpy(&data,payload.data(),sizeof(mavlink_wind_t)); emit wind(data); } break; } }//7 else {//8 header.clear(); }//8 }//4 else {//9 header.clear(); }//9 }//3 }//1
this is the reading part. I also tested writing and it's worked just fine. writing part is not complete yet. I will put that here as soon as it's completed.
thanks again SGaist. :)[edit: Added missing coding tags SGaist]
-
Why is Reader a QSerialPort ?
Also, your loop in the constructor opens every serial port of your computer
-
@SGaist
What should it be?
Yes I should create a list for available ports and let user choose. it's still not complete. I should work more on details yet. -
Use e.g. a default value that you know is working and add a configuration dialogue
-
@SGaist
Did it :)
Thank you :) -
There's also QSettings to store that value so you can reuse it on next startup, that will avoid your user to change it each time they load your application if the port is not the same as yours
-
@SGaist
I think if I store available ports string in a combo box will do this for me. right?