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

Issue reading from QSerialPort



  • i want to read a qbyte array how to do that
    my array look like
    QByteArray rq;
    if
    ( serial->open(QIODevice::ReadWrite)==true){
    qDebug()<<"connected";

        rq.resize(7);
        rq[0] = 0xAA;
        rq[1] = 0x55;
        rq[2] = 0x07;
        rq[3] = 0x20;
        rq[4] = 0x00;
        rq[5] = 0x00;
        rq[6] = 0xBB;
    

    }else
    {
    qDebug()<<"Not connected";
    }
    serial->write( rq);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }
    this bytedata has version number in it i want to find the version number in the rq[7] am sending and want to write it



  • @Anna_64
    Don't understand your question. But as a general (doubtless unrelated) point:

    serial->write( rq);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    

    I assume the intention here is that you write some data and then as result when the other end reads it it sends back some data for you to read? If that is so, I suggest you swap the order of your calls to:

    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    serial->write( rq);
    

    Your way is (at least theoretically) open to the reply being received immediately after the write(), before you have attached your signal slot handler. I think it's good general practice to attach your handlers before they can possibly get called, just in case....


  • Lifetime Qt Champion

    @Anna_64

    1. Your question is not related to this thread as you're not using QDataStream, you should open your own thread
    2. Your question is not clear: "i want to find the version number in the rq[7] am sending and want to write it" - write it to where, and if it is already in rq[7] then what is the problem in finding it?


  • @JonB QSerialPort *serial;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }
    MainWindow::~MainWindow()
    {
    delete ui;
    serial->close();
    }
    void MainWindow::serialReceived()
    {
    QByteArray ba;
    ba=serial->readAll();
    //qDebug()<<
    }
    void MainWindow::on_Connect_clicked()
    {
    QFile file("C:\Users\pavt\Desktop\binfile_enc.bin");
    if (!file.open(QIODevice::ReadOnly)) return;
    QByteArray iContents = file.readAll();
    ushort c3 = 0xFF;
    c3 = (unsigned char) iContents.at(2) | (unsigned char) iContents.at(3) << 8;
    qDebug()<<file.size();
    }
    void MainWindow::on_COM9_clicked()
    {
    serial = new QSerialPort(this);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    serial->setPortName("COM9");
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    QByteArray rq;
    if
    ( serial->open(QIODevice::ReadWrite)==true){
    qDebug()<<"connected";

        rq.resize(10);
        rq[0] = 0xAA;
        rq[1] = 0x55;
        rq[2] = 0x07;
        rq[3] = 0x00;
        rq[4] = 0x00;
        rq[5] = 0x20;
        rq[6] = 0x00;
        rq[7] = 0x02;
        rq[8] = 0x00;
        rq[9] = 0xBB;
    

    }else
    {
    qDebug()<<"Not connected";
    }
    serial->write( rq);
    }
    this s the data am sending to a device and it writes this data into a device now i want to read the data from the device because rq[8] contains my version number i want to fetch that version number inside a button click so when i click button named version i should display my version data which is now written in device



  • @Anna_64
    So, in some shape or form, the receiving end needs to receive the 10 bytes (e.g. perhaps with similar connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())); and call readAll() in the slot each time data is received, you may find that going to https://doc.qt.io/qt-5/qdatastream.html and searching for "Using Read Transactions" helps you for this, or you can do the necessary code yourself). And then just pick out response_buffer[8] if that's what you're interested in. I don't see there's anything else to say.



  • @JonB ican write data to device but cannot read from the device using serial->readAll();



  • @Anna_64
    Well I don't have your device so I do not know whether it can send, whether it is sending, whether you are receiving, whether your code is correct, or what. Nor do I know what your code where you call serial->readAll(); looks like (where it is being called from etc.) So far you have only shown code which sends.

    If the code is just as you show, after your ba=serial->readAll(); what about at least qDebug() << ba.size() so we can at least see how many bytes were received? And if that turns out to be 0, don't forget:

    This function has no way of reporting errors; returning an empty QByteArray can mean either that no data was currently available for reading, or that an error occurred.

    You might find that https://doc.qt.io/qt-5/qiodevice.html#read or https://doc.qt.io/qt-5/qiodevice.html#errorString would then tell you if there was an error.



  • @JonB i can read also
    its like "\xAAU\t\x00\x00 \x00\x02\x00\x00\x03!"
    and lenght s 12
    now i have to take only 10th one that is 03 how to do that



  • @JonB its showing 12bytes


  • Lifetime Qt Champion

    Hi,

    I have forked your question into its own topic thread.

    Why are you creating a new QSerialPort object each time you call on_COM9_clicked ?

    Do you have a protocol to communicate with your device ? e.g. a start and stop sequence so you can separate frames you send and receive ?

    If you have one, then you can parse the received data once you know you have enough bytes to contain at least one frame.



  • @Anna_64 said in Issue reading from QSerialPort:

    now i have to take only 10th one that is 03 how to do that

    It seems a surprising question to ask, but if that's really what you want to know it would be ba[9].

    You should also always read, understand & act on what @SGaist asks/suggests.



  • @JonB how to split a bin file of size 1050 bytes into 64 chunks in qt



  • @SGaist i want to split a file which looks like
    QFile file("C:\Users\Desktop\binfile_enc.bin");
    this file is of size 1050.624 bytes
    i want to split into 64 chunks please help



  • @Anna_64

    @JonB how to split a bin file of size 1050 bytes into 64 chunks in qt

    This has nothing to do with Qt. You would obviously split it into chunks of 1050 / 64 bytes.



  • @JonB its a stupid answer



  • @Anna_64

    @JonB its a stupid answer

    Thank you so much. It's an exact answer to the question as posed by you. And follows on from the time I spent trying to answer your earlier questions. I haven't insulted you so far in your questions, thanks for insulting me. You are rude. I'm done with you, find out how people will respond to you, given that this support forum provides voluntary help....


Log in to reply