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. Not getting the correct response from hardware?

Not getting the correct response from hardware?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 206 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.
  • M Offline
    M Offline
    Mohit Tripathi
    wrote on last edited by
    #1
    #include "dialog.h"
    #include "ui_dialog.h"
    
    #include <QApplication>
    
    #include <QtSerialPort>
    
    
    template <class T> bool hasError(const QIODevice & d) {
        return qobject_cast<const T *>(&d) && static_cast<const T &>(d).error() != T::NoError;
    }
    
    void chkError(const QIODevice & d) {
        if (hasError<QFile>(d) || hasError<QSerialPort>(d))
            qFatal("I/O Error on %s: %s", d.objectName().toLocal8Bit().constData(),
                   d.errorString().toLocal8Bit().constData());
    }
    
    void logData(QTextStream & log, const QByteArray & data) {
        log << data.toHex() << "\nLength: " << data.size() << "\n\n";
        log.flush();
        //  chkError(*log.device());
    }
    
    void transmit(QSerialPort & port, const QByteArray & data) {
        port.write(data);
        qDebug() << "\nWrote" << data.size() << ":" << data.toHex().constData();
        chkError(port);
        // logData(log, data);
    }
    
    void receive(QSerialPort & port) {
        auto data = port.readAll();
        qDebug() << "\nRead" << data.size() << ":" << data.toHex().constData();
        // chkError(port);
        //logData(log, data);
    }
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
      //  QCoreApplication app(argc, argv);
    
        QFile logFile("Data.txt");
        if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
            qFatal("Can't open the log file: %s", logFile.errorString().toLocal8Bit().constData());
        QTextStream log(&logFile);
    
        QSerialPort port;
        port.setPortName("COM5");
        port.setBaudRate(QSerialPort::Baud9600);
        port.setDataBits(QSerialPort::Data8);
        port.setParity(QSerialPort::EvenParity);
        port.setStopBits(QSerialPort::OneStop);
        port.setFlowControl(QSerialPort::NoFlowControl);
    
        if (!port.open(QIODevice::ReadWrite))
            qFatal("Can't open the serial port: %s", port.errorString().toLocal8Bit().constData());
    
        logFile.setObjectName("Log File");
        port.setObjectName("Serial Port");
    
        const QByteArray sendPacket = QByteArrayLiteral("\x7E\x03\x00\x00\x02\x00\x02\x03\x02\x06\x00\x01\x02\x00\x09\x00\x01\x02\x02");
    
        transmit(port, sendPacket);
    //        transmit(serial, sendPacket);
            while(port.waitForReadyRead(5000))
            {
               receive(port);
               QTimer::singleShot(300, this, SLOT(quit()));
            }
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    

    Hi, I am getting the response but wrong. I am getting the address and function code same as query but, not other values here. Can I know " what I am doing wrong in this?
    Note: I am getting the correct response in Hercules software.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly are you sending ?
      What exactly are you getting ?
      What exactly are you expecting ?

      Out of curiosity, why not use the asynchronous nature of QSerialPort ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

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