Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Serial Port data receive and write in a text file.



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


  • Qt Champions 2019

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



  • 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;
    }


  • Qt Champions 2019

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



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


  • Qt Champions 2019

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



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


  • Qt Champions 2019

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

    ba=serial->readAll();
    

    was executed?



  • @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);
    }
    

Log in to reply