Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qserialport- data access problems
Forum Updated to NodeBB v4.3 + New Features

Qserialport- data access problems

Scheduled Pinned Locked Moved Mobile and Embedded
4 Posts 1 Posters 1.4k 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.
  • R Offline
    R Offline
    Ramon
    wrote on last edited by
    #1

    Hi,

    I'm trying to receive continuous serial data (numbers 0 till 9 in a loop) from microcontroller using Beaglebone Black. When ReadyRead signal comes, it should append the data to the file (serial.txt).

    Issues:
    1) The data displayed on Application output on my PC (QDebug) shows
    @"0"
    "1"
    "2"
    "3"
    "4"@
    etc. But I want it to be shown in one line, like 0123456... What am I doing wrong?

    2) Sometimes two or more numbers appear in the same line, like this:
    @"0"
    "12"
    "3"@
    Why is it so?

    3) After receiving the data for 4-5 seconds, QT stops updating the data on the console (QDebug). Is the buffer getting full?

    4) QSerialPort accepts data only if I deploy the GUI application after starting transmission from the microcontroller. It does not accept data if the GUI application deployed first. What could be the reason?

    main.cpp

    @#include "mainwindow.h"
    #include <QApplication>
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QFile>
    #include <QString>
    #include <QDebug>
    #include <QTextStream>

    void MainWindow::OpenFile (QString mFilename)
    {
    mFile.setFileName(mFilename);
    if(!mFile.open( QFile::Append | QFile::Text))
    {
    qDebug() << "Could not open file for writing";
    return;
    }
    }

    void MainWindow::Write (QString inp)
    {
    QTextStream out(&mFile);
    out << inp << "\n";
    }

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    mainwindow.cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "qserialport.h"
    #include <QSerialPort>
    #include <QDebug>
    QSerialPort *serial;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    bool status = false;
    serial = new QSerialPort(this);
    serial->setPortName("ttyO0");
    status = serial->open(QIODevice::ReadWrite);
    if(status){
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    QString mFilename = "/home/root/serial.txt";
    OpenFile (mFilename);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }
    else{
    qDebug() << "Could not open serial port";
    }
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::serialReceived()
    {
    QByteArray bytearray = serial->readAll();
    QString input = QString(bytearray);
    Write(input);
    qDebug() << bytearray;
    }@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Ramon
      wrote on last edited by
      #2

      --

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Ramon
        wrote on last edited by
        #3

        Hi all,
        Please go through the first post of this thread. Looking forward to your replies.
        Thank you.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Ramon
          wrote on last edited by
          #4

          I modified the serialReceived() function to print the no of bytes received as follows:

          @void MainWindow::serialReceived()
          {
          qDebug() << serial->bytesAvailable();
          int ExpectedNoOfBytes = 3;
          if (ExpectedNoOfBytes == serial->bytesAvailable()){
          QByteArray bytearray = serial->readAll();
          QString input = QString(bytearray);
          Write(input);
          qDebug() << bytearray;
          }
          }@

          The output is follows:
          @512
          1024
          1536
          2048
          2560
          3072
          3584
          3856
          3857
          3859
          3860
          3861 ..... @

          Initially it is in multiples of 512 and then it becomes one byte each. Looks like the if statement is not getting executed at all. Can someone tell what is happening here?

          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