the program freezes
-
hello, I am making a project with QT, in this project I am reading data from serial. When the device with serial in my project is disconnected, it performs a port scan again and connects to the device. but I want these parts to continue independently of the program by doing the reading and searching parts with threads. but even if I do port search and data reading part with thread, the program freezes while detecting the connection and reconnecting.
How can I make a better logic because of the signal and slot mechanism?Serial::Serial(): voltage(0), cam_value(0), prev_byte(FOOTHER), state(0){ port_ = new QSerialPort(this); connectionTimer = new QTimer(this); connectionTimer->setInterval(1000); connectionTimer->start(); initializationData(RESET_ALL); checkDeviceConnection(); // connect(port_, &QSerialPort::readyRead, this, &Serial::readBytes); // connect(connectionTimer, &QTimer::timeout, this, &Serial::isConnected); serial_thread = std::thread{std::bind(&Serial::spinSerial, this)}; } Serial::~Serial(){ port_->close(); connectionTimer->stop(); delete connectionTimer; delete port_; } void Serial::spinSerial(){ // connect(port_, &QSerialPort::readyRead, this, &Serial::readBytes); // connect(connectionTimer, &QTimer::timeout, this, &Serial::isConnected); } void Serial::isConnected(){ elapsed_time = std::chrono::steady_clock::now() - prev_time; if((elapsed_time.count()/1000000) > (time_coef * TIME_OUT)){ checkDeviceConnection(); isConnect = false; // qDebug() << "Disconnected From Device"; }else{ // qDebug() << "Connection Established"; } } void Serial::checkDeviceConnection(){ if(port_->isOpen()) { port_->close(); } for(const QSerialPortInfo port_list : QSerialPortInfo::availablePorts()){ if((port_list.manufacturer() == "FTDI") || (port_list.description() == "Arduino Mega 2560") ){ prev_time = std::chrono::steady_clock::now(); port_->setPortName(port_list.portName()); port_->setBaudRate(BAUDRATE); port_->setDataBits(QSerialPort::Data8); while(port_->open(QIODevice::ReadWrite)){ qDebug() << "ReadWrite "; isConnect = true; } time_coef = 10; } #ifdef DEBUG qDebug() << "-----------------------------------------------------"; qDebug() << "portName: " << port_list.portName(); qDebug() << "systemLocation: " << port_list.systemLocation(); qDebug() << "description: " << port_list.description(); qDebug() << "manufacturer: " << port_list.manufacturer(); qDebug() << "serialNumber: " << port_list.serialNumber(); qDebug() << "vendorIdentifier: " << port_list.vendorIdentifier(); qDebug() << "productIdentifier: " << port_list.productIdentifier(); #endif } } void Serial::readBytes(){ while(port_->bytesAvailable()){ uint8_t cur_byte; port_->read((char*)&cur_byte, 1); if(state == 0){ if((cur_byte == HEADER) && (prev_byte == FOOTHER)){ rx_buffer[state++] = cur_byte; } else { state = 0; } }else if( state < HEADER_LEN + PAYLOAD_LEN){ rx_buffer[state++] = cur_byte; }else if(state < HEADER_LEN + FOOTHER_LEN + PAYLOAD_LEN){ state = 0; prev_byte = cur_byte; if(cur_byte == FOOTHER){ raw_data[0] = (static_cast<int16_t>(rx_buffer[1] & 0x01)); raw_data[1] = (static_cast<int16_t>((rx_buffer[1]>>1) & 0x01)); raw_data[2] = (static_cast<int16_t>((rx_buffer[1]>>2) & 0x01)); raw_data[3] = (static_cast<int16_t>((rx_buffer[1]>>3) & 0x01)); raw_data[4] = (static_cast<int16_t>((rx_buffer[1] >> 4) | ((rx_buffer[2] << 4) & 0x03FF))); raw_data[5] = (static_cast<int16_t>((rx_buffer[2] >> 6) | ((rx_buffer[3] << 2) & 0x03FF))); raw_data[6] = (static_cast<int16_t>((rx_buffer[4]) | ((rx_buffer[5] << 8) & 0x03FF))); raw_data[7] = (static_cast<int16_t>((rx_buffer[5] >> 2) | ((rx_buffer[6] << 6) & 0x03FF))); raw_data[8] = (static_cast<int16_t>((rx_buffer[6] >> 4) | ((rx_buffer[7] << 4) & 0x03FF))); raw_data[9] = (static_cast<int16_t>((rx_buffer[7] >> 6) | ((rx_buffer[8] << 2) & 0x03FF))); time_coef = 1; if(debug_serial){ qDebug() << raw_data[0] << raw_data[1] << raw_data[2] << raw_data[3] << raw_data[4]; qDebug() << raw_data[5] << raw_data[6] << raw_data[7] << raw_data[8] << raw_data[9]; qDebug() << "------------------"; } }else{ state = 0; } }else { state = 0; } prev_byte = cur_byte; prev_time = std::chrono::steady_clock::now(); } } void Serial::writeBytes(uint8_t new_data){ // qDebug() << new_data; if ( !(port_->isOpen())) { return ;} if(new_data == tx_data[1]) { return ;} tx_data[1] = new_data; port_->write((char*)(tx_data), 5); port_->flush(); }
-
@serkan_tr said in the program freezes:
eading and searching parts with threads. but even if I do port search and data reading part with thread, the program freezes while detecting the connection and reconnecting.
Why? What do you gain from this except headaches?
-
@Christian-Ehrlicher I may have expressed myself wrong. My purpose is that the data coming from the serial port does not affect the program.
-
@serkan_tr said in the program freezes:
My purpose is that the data coming from the serial port does not affect the program.
It does not since QSerialPort is (like nearly all other I/O classes in Qt) asynchronous.
-
S serkan_tr has marked this topic as solved on