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. Reading from serial Port
Qt 6.11 is out! See what's new in the release blog

Reading from serial Port

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.7k 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.
  • C Offline
    C Offline
    chandradeo
    wrote on last edited by
    #1

    Hi friends i am trying to develope an application which reads data from serial port and display. Actually my data is in a format like STXss:mm:hh:od:os\r\r\r\r\r\r\rr\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\rr\ ie every data frame contains start of Text them hou ,min and second information and lastly carrieage returns padded in such a way that it becomes 285 bytes in total. Now this frame is emitted by a device every one second. I want to read the data and display HOUR ,MIN and SECOND in three different TextBox or LineEdit in Qt.

    The problem is that till now i have not been able to read the data completely ie some time i get half frame some time full . Some time HOUR text box displays the minute information and so on. I have read the data using the timer of i sec and also using the event based reading none of them display that data correctly. I am using qt5 like code given below

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "qextserialport.h"
    #include <QTimer>

    char databuff[1024];
    QextSerialPort *port;
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

        port = new QextSerialPort("ttyS0");
    
       port->open(QIODevice::ReadWrite);
    
       // port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
    

    port->setBaudRate(BAUD9600);

    port->setFlowControl(FLOW_OFF);

    port->setParity(PAR_NONE);

    port->setDataBits(DATA_8);

    port->setStopBits(STOP_1);

    //port->setQueryMode(QextSerialPort::EventDriven);

    // connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(onDataAvailable()));
    timer->start(1000);
    }

    void MainWindow::onDataAvailable()
    {

     port->read(databuff,15);
     QString str(databuff[10]);    
    

    ui->lineEdit->setText( ui->lineEdit->text().append(databuff) );
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    port->close();
    }
    @

    please suggest any solution. Is there any way such that i can start reading when STX arrives and end reading when \r\r\r arrives. Thank u all in advance

    Edit: please mark your code as code in future, Gerolf

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You need to align the data yourself. Scan the results you are getting, then take STX as the beginning and read further bytes.

      (Z(:^

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chandradeo
        wrote on last edited by
        #3

        [quote author="sierdzio" date="1399980456"]You need to align the data yourself. Scan the results you are getting, then take STX as the beginning and read further bytes.[/quote]

        Hi sierdzio thank u very much for ur reply . can u please explain using some code.?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Take a chunk of data (read(), readAll(), etc.), and do this:
          @
          QString chunk = serialPort.readAll();

          int beginning = chunk.indexOf("STX");
          chunk = chunk.mid(beginning + 3, 13); // This will cut only your hard data form the chunk

          int seconds = chunk.left(2).toInt(); // this will give you seconds
          // etc.
          @

          (Z(:^

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chandradeo
            wrote on last edited by
            #5

            [quote author="sierdzio" date="1399993854"]Take a chunk of data (read(), readAll(), etc.), and do this:
            @
            QString chunk = serialPort.readAll();

            int beginning = chunk.indexOf("STX");
            chunk = chunk.mid(beginning + 3, 13); // This will cut only your hard data form the chunk

            int seconds = chunk.left(2).toInt(); // this will give you seconds
            // etc.
            @[/quote]

            Hi sierdzio thank u for ur reply. As i tried above code id did not work because the first character in the incoming string is not Exactly "STX" but it a symbol(face like) whose decimal value is ==1.
            No can u please give me the modified code so that i can read the data correctly. Thank u in advance

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Please do it yourself. I do not know the exact binary structure of your packets, and I don't have any testing rig to make sure my solution will work. What I gave you is an idea on how to - possibly - make it work; the rest of the job is yours.

              (Z(:^

              1 Reply Last reply
              0
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #7

                Just a little correction: STX has the decimal value 2 (not 1) it's one of the "ASCII ctrl characters":http://www.asciitable.com/

                1 Reply Last reply
                0

                • Login

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