Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSerialPort



  • HI dear friend , I use this snippet code for interacting to serial port.
    this code work it doesn't any problem.I can send and write some data.
    But I want check the data has been received If the data is correct send another data But I don't know how can I fix this problem.I don't have any Idea about solving this challenge.

    serial->setPortName(portname);
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::HardwareControl);
    if(!serial->isOpen())
    { 
         serial->open(QIODevice::ReadWrite);
         serial->write("start"); 
         connect(serial,SIGNAL(readyRead()),this,SLOT(serialRecived()));
    
    }
    else {
         qDebug() << "Serial Error:" <<  serial->errorString();
         ui->label->setText("error:"+serial->errorString());
    }
    

    —————SLOT——————

    void MainWindow::serialRecived()
    {
     QByteArray out;
    out=serial->readAll();   
    if(out=="OK") 
    //check the data has been received if it is OK I must send another data //to the serial port and after send I must listen to the another response 
    //from serial port then I must check and send another data again.....
        { 
            serial->write("another_Data");
        }
    }
    

    Is there any one can help me please?


  • Lifetime Qt Champion

    Hi
    Normally you handle such interaction with a small state machine.
    How it must be coded depends on what should happen if data is not ok. Should all data from start be resend of just the last ?

    And what is "another_Data". A simply text or something more complex ?


  • Qt Champions 2020

    @saeidparand said in QSerialPort:

    HardwareControl

    This mode assumes, that you need in additional CTS/RTS line connected.



  • @mrjj said in QSerialPort:

    How it must be coded depends on what should happen if data is not ok. Should all data from start be resend of just the last ?
    And what is "another_Data". A simply text or something more complex ?

    this scenario is : for instance I must send USER_NAME and dongle send the response for me it it is "OK" I must send PASS_WORD then dongle response "OK2" then I must send LOGIN word..
    that's all

    another_Data is not a complex word but is has been encrypted.
    I read all the state machine but unfortunately I didn't get any Idea that how I can use state machine in QSerialport.

    Do you have any sample code?



  • @kuzulis
    I didn't understand what your mean is.
    May I ask you explain about this?



  • @saeidparand On Serial port hardware, you have many signals, not just Tx (Transmission) and Rx (Reception), you can have some optional signals which are used to handle transmission flow:

    • DTR (Data Terminal Ready)
    • DSR (Data Set Ready)
    • RTS (Request to Send)
    • CTS (Clear To Send)

    DTR/DSR are crossed between source and destination device, and RTS/CTS also.
    Those signals are used on hardware to handle transmission handshaking, so data are only send when counterpart is ready to receive.
    But those signals are optional, and many serial communication do not use then, so only enable hardware handshaking when your source and destination device have those signals and your have a so called "null modem" câble between them.
    In other case, disable hardware handshaking serial->setFlowControl(QSerialPort::NoFlowControl); or data will never be sent.


Log in to reply