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. Serial Port data receive and write in a text file.
Forum Updated to NodeBB v4.3 + New Features

Serial Port data receive and write in a text file.

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.3k 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
    Shivam Sharma
    wrote on last edited by
    #1

    Hi guys. I'm trying to read data from serial port and store it in a text file. Can someone please help me out with the code?
    I'm new to this and hence need help.

    jsulmJ 1 Reply Last reply
    0
    • S Shivam Sharma

      Hi guys. I'm trying to read data from serial port and store it in a text file. Can someone please help me out with the code?
      I'm new to this and hence need help.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Shivam-Sharma said in Serial Port data receive and write in a text file.:

      Can someone please help me out with the code?

      What exactly do you need?
      What did you try already?
      What does not work?

      Did you check this: https://doc.qt.io/qt-5/qserialport.html ?
      And https://doc.qt.io/qt-5/qtserialport-examples.html ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • S Offline
        S Offline
        Shivam Sharma
        wrote on last edited by
        #3

        Hey, this is what I've written. Using GTK+ to send data and read on the ui panel, but isn't working. Please help.

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QSerialPort>
        #include <QtDebug>
        #include <QFile>
        QSerialPort *serial;

        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        serial->setPortName("com4");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        serial->write("ok");
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialRecieved()));
        }

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

        void MainWindow::serialReceived()
        {
        QByteArray ba;
        ba=serial->readAll();
        ui->label->setText(ba);
        qDebug()<<ba;
        }

        jsulmJ 1 Reply Last reply
        0
        • S Shivam Sharma

          Hey, this is what I've written. Using GTK+ to send data and read on the ui panel, but isn't working. Please help.

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QSerialPort>
          #include <QtDebug>
          #include <QFile>
          QSerialPort *serial;

          MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          serial = new QSerialPort(this);
          serial->setPortName("com4");
          serial->setBaudRate(QSerialPort::Baud9600);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          serial->open(QIODevice::ReadWrite);
          serial->write("ok");
          connect(serial,SIGNAL(readyRead()),this,SLOT(serialRecieved()));
          }

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

          void MainWindow::serialReceived()
          {
          QByteArray ba;
          ba=serial->readAll();
          ui->label->setText(ba);
          qDebug()<<ba;
          }

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Shivam-Sharma said in Serial Port data receive and write in a text file.:

          but isn't working

          What EXACTLY does not work?
          Did you make sure open() actually succeeded?
          Was write() successful?
          Was serialReceived() called?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Shivam Sharma
            wrote on last edited by
            #5

            OK. So, yes signalReceived() was called.
            NO, the write() wasn't successful since there was no data being communicated to the ui.
            I don't know how to make sure open() actually suceeded.

            Basically when I input data on the GTK+ terminal, I expected it to be displayed on the ui panel, but it isn't. And just can't figure out what to do next. Is there any other code I shoud try? Could you sent it over.

            jsulmJ 1 Reply Last reply
            0
            • S Shivam Sharma

              OK. So, yes signalReceived() was called.
              NO, the write() wasn't successful since there was no data being communicated to the ui.
              I don't know how to make sure open() actually suceeded.

              Basically when I input data on the GTK+ terminal, I expected it to be displayed on the ui panel, but it isn't. And just can't figure out what to do next. Is there any other code I shoud try? Could you sent it over.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Shivam-Sharma said in Serial Port data receive and write in a text file.:

              I don't know how to make sure open() actually suceeded

              Please read documentation: https://doc.qt.io/qt-5/qserialport.html#open (tip: open() returns a boolean).

              What data do you get over serial port? Is it binary or text? If it is just some binary non printable data you can't show it like you do now. You need to convert it to text first.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • S Offline
                S Offline
                Shivam Sharma
                wrote on last edited by Shivam Sharma
                #7

                I don't get any data, that's the whole issue. There's nothing on display. At-least something should've been there. But ya, totally confused.

                jsulmJ sankarapandiyanS 2 Replies Last reply
                0
                • S Shivam Sharma

                  I don't get any data, that's the whole issue. There's nothing on display. At-least something should've been there. But ya, totally confused.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Shivam-Sharma But before you wrote that signalReceived() was called, right?
                  So, what does ba contain after

                  ba=serial->readAll();
                  

                  was executed?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Shivam Sharma

                    I don't get any data, that's the whole issue. There's nothing on display. At-least something should've been there. But ya, totally confused.

                    sankarapandiyanS Offline
                    sankarapandiyanS Offline
                    sankarapandiyan
                    wrote on last edited by
                    #9

                    @Shivam-Sharma You may try this code ... shivam

                    
                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                    
                          ui->setupUi(this);
                          QString filename1="/home/adx-soft1/Shankar/serial/myfile1.txt";
                          QFile file(filename1);
                          if(!file.exists()){
                              qDebug() << "NO FILE "<<filename1;
                          }else{
                              qDebug() << filename1<<" ...";
                          }
                          QString line1;
                          ui->sender->clear();
                          if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ ui->sender->setText(file.readAll()); }
                    
                          QList<QSerialPortInfo> list;
                          list = QSerialPortInfo::availablePorts();
                    
                          for (int i= 0; i < list.length(); i++)
                          {
                            ui->comboBox->addItem(list[i].portName());
                          }
                    
                        serial = new QSerialPort(this);
                        serial->setPortName(ui->comboBox->currentText());
                        serial->setBaudRate(QSerialPort::Baud115200);
                        serial->setDataBits(QSerialPort::Data8);
                        serial->setParity(QSerialPort::NoParity);
                        serial->setStopBits(QSerialPort::OneStop);
                        serial->setFlowControl(QSerialPort::NoFlowControl);
                        if( serial->open(QIODevice::ReadWrite)){
                        qDebug()<<"port open ok";
                           connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                        }
                       else
                       {
                           qDebug()<<"cant open port : " << serial->error();
                       }
                    
                    
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        serial->close();
                        delete ui;
                    }
                    
                    void MainWindow::on_pushButton_clicked()
                    {
                           ui->progressBar->setValue(100);
                    //    ui->receive->clear();
                    }
                    void MainWindow::checkAndSendData()
                    {
                        if(!m_lines.isEmpty()){ // this looks correct ..
                        baToSend = m_lines.takeFirst().toUtf8();
                        serial->write(baToSend);
                    
                            ui->aknowmnt->setText("Sent Message -->" +  QString(baToSend) + "\n");
                        }
                        else
                        {
                    //        ui->aknowmnt->setText("Receiving ");
                        }
                    }
                    
                    void MainWindow ::on_send_clicked()
                    {
                          line = ui->sender->toPlainText();
                          QString txt1 = ui->receive->toPlainText();
                          txt3 = ui->aknowmnt->toPlainText();
                    
                          qDebug() << "m_lines: " << m_lines;
                    
                          if(m_lines.isEmpty()){
                          QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                          inputFile.open(QIODevice::ReadOnly);
                    
                          if (!inputFile.isOpen())
                          {
                          qDebug() << "The file is not found or not open ...";
                          return;
                          }
                    
                          QTextStream stream(&inputFile);
                          while(!stream.atEnd()) {//this works ?
                          QString strline = stream.readLine();
                          m_lines.append(strline + QString("\r\n"));
                       // qDebug() << "m_lines: " << m_lines;// can u see this ?
                          }
                          }
                          checkAndSendData();
                    }
                    
                    
                    
                    void MainWindow::serialReceived()
                    {
                        QByteArray serialData = serial->readAll();
                        QString receivedData = QString::fromStdString(serialData.toStdString());
                        qDebug()<< receivedData;
                        ui->receive->append(receivedData);
                    }
                    
                    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