Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort
Forum Updated to NodeBB v4.3 + New Features

QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 731 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saeidparand
    wrote on last edited by Christian Ehrlicher
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      S 1 Reply Last reply
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #3

        @saeidparand said in QSerialPort:

        HardwareControl

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

        S 1 Reply Last reply
        0
        • mrjjM mrjj

          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 ?

          S Offline
          S Offline
          saeidparand
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • K kuzulis

            @saeidparand said in QSerialPort:

            HardwareControl

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

            S Offline
            S Offline
            saeidparand
            wrote on last edited by
            #5

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

            KroMignonK 1 Reply Last reply
            0
            • S saeidparand

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

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #6

              @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.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              4

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved