Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Not getting the correct response from hardware?



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


  • Lifetime Qt Champion

    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 ?


Log in to reply