0
votes

How can I write and read hex data when I open serial port?

if (serial->isOpen()) {
    qDebug() << "seccus: " ;
    static const char buff[] = {0xA1, 0xFF, 0xFF, 0x00,97};
    serial->write(buff,sizeof(buff));
}
if (serial->bytesAvailable()) {   // If there are bytes available
    QByteArray f_data; // data container
    f_data.clear();

    if (serial->open(QIODevice::ReadWrite)) { // Try to open the port
        while(serial->bytesAvailable()) { // Reading loop
            f_data.append(serial->readAll());
       }

       serial->flush();
    }
    qDebug()<< "the result" << f_data; // Check the result
}

if(!serial->isOpen())
    qDebug() << "carte disconnect"<<serial->errorString();
1
when i run my code nothing appear in my terminal. should i convert my buff to Byte ???? i wonder what am i missing here ??meth
I don't use Qt, but shouldn't the call to serial->open(QIODevice::ReadWrite) go before the call to serial->write(buff,sizeof(buff));?Johnny Mopp
yes you have raison, but still not work . it seems that i don't have any availble byte to read. do i need to convert from hex to byte?meth
Since you are not getting anything back, maybe you are sending the wrong data. What does the client expect? Also, note you are missing 0x on the last element of the array. Is that on purpose?Johnny Mopp
i remove it. i don't know why serial does'nt receive any thing ???? i don't understand what do you mean about "sending the wrong data" ??meth

1 Answers

1
votes

To debug your issue, I would start with simplifying what you're trying to do. Try writing some test code to start, to make sure things work as you expect:

  1. Test serial is open - should not be
  2. Open serial
  3. Test serial is open - should be
  4. Test if bytes are available - should not be
  5. write data to serial. Flush.
  6. Test if bytes are available - should be
  7. Read and print to screen (i.e test your reading it correctly)
  8. Close the serial port - test its not open

Perhaps if one of these steps doesn't work as you expect, you can at least focus on why that particular part is not working.

Good luck.