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 Data from Serial Port.
Forum Updated to NodeBB v4.3 + New Features

Reading Data from Serial Port.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 182 Views
  • 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.
  • M Offline
    M Offline
    mbatra
    wrote on 5 Mar 2024, 05:34 last edited by
    #1

    Hello Everyone,

    I need help regarding one solution. I have created an application and I am reading data from Serial Port. The data comes in pack of 8 bytes ( 5A 00 01 02 03 04 05 5F ). Start and End bytes fixed (5A and 5F). I am using the below code to read data:

    if (_serialPort->open(QSerialPort::ReadOnly))
    {
    QObject::connect(_serialPort, &QSerialPort::readyRead, this, &SerialPort::dataReady);
    }

        QByteArray receivedData;
        if (_serialPort->isOpen())
        {
            receivedData.append(_serialPort->readAll());
            emit dataReceived(receivedData.toHex(' '));
        }
    

    Read the data and send it to another class. Application will be working fine till the data from serial port read is 8 bytes. When data is recieved less than or more than 8 bytes, the application crashes.

    Data is coming in 8 bytes at rate of 40 ms. I want to read only 8 bytes of data at a time and buffer should not keep on filling with the data.

    Any help will be appreciated.

    Thanks..!!

    C 1 Reply Last reply 5 Mar 2024, 05:50
    0
    • M mbatra
      5 Mar 2024, 05:34

      Hello Everyone,

      I need help regarding one solution. I have created an application and I am reading data from Serial Port. The data comes in pack of 8 bytes ( 5A 00 01 02 03 04 05 5F ). Start and End bytes fixed (5A and 5F). I am using the below code to read data:

      if (_serialPort->open(QSerialPort::ReadOnly))
      {
      QObject::connect(_serialPort, &QSerialPort::readyRead, this, &SerialPort::dataReady);
      }

          QByteArray receivedData;
          if (_serialPort->isOpen())
          {
              receivedData.append(_serialPort->readAll());
              emit dataReceived(receivedData.toHex(' '));
          }
      

      Read the data and send it to another class. Application will be working fine till the data from serial port read is 8 bytes. When data is recieved less than or more than 8 bytes, the application crashes.

      Data is coming in 8 bytes at rate of 40 ms. I want to read only 8 bytes of data at a time and buffer should not keep on filling with the data.

      Any help will be appreciated.

      Thanks..!!

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 5 Mar 2024, 05:50 last edited by Christian Ehrlicher 3 May 2024, 05:50
      #2

      @mbatra said in Reading Data from Serial Port.:

      Any help will be appreciated.

      Use the readyRead() signal and read the data in the connected slot. Since a serial port is stream-based you have to fiddle out by yourself where / when the 8 bytes are available.

      The forum search can also help here since this is asked many times..

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      1/2

      5 Mar 2024, 05:34

      • Login

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