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

Sending hex data via serial communication



  • Hi. I am finding way to send hex data via serial communication
    i searched it several times and followed some ways but it didn't work.
    i checked that protocol is working with using other software that sending hex data to device
    below is my code

    @
    const char data[]={0xAA,0xAA,0x01,0x00,0x00,0x0E,0x00,0x01,0x00,0x00,0x00,0x2D,0x37,0x1D,0xAA,0xAA,0x01,0x00,0x00,0x0E,0x00,0x0C,0x10,0x00,0x00,0x01,0x76,0x13};

    serial->setPortName(("COM8"));
    initSerialPort(); // baud rate and etc

    if(serial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Port is open!";
        if(serial->isWritable())
        {
            qDebug()<<"Yes, i can write to port!";
           int size = sizeof(data);
           serial->write(data,size);
        }
    }
    

    @

    and when i use other declare like uint16_t, uchar, write function cannot convert argument 1 from uint16_t (or uchar) to const char *

    i did try also this form
    @

    QByteArray hex("AAAA0100000E00010000002D371DAAAA0100000E000C100000017613");
    QByteArray data = QByteArray::fromHex(hex);
    and it also didnt work



  • @Bean_Noodle

    but it didn't work.
    and it also didnt work

    Do you get a compiler error on trying to write/send the data? In which case, copy & paste the error message and show the line number it occurs on. Or, do you get an error at the receiver side from the data you send, in which case state the error and clearly you are not sending what the receiver expects.

    We are not mind-readers, you have to supply the exact, correct information and what errors you get. "Sending hex data" is not in itself any problem, and you probably don't actually send hex anything, just bytes/words.



  • @JonB thank you for answering.
    there was no error message when i try to build with that code but device don't work at all.
    and when i declare data with form of uchar or uint16_t,

    cannot convert argument 1 from uint16_t (or uchar) to const char *

    this error message is come.



  • @Bean_Noodle said in Sending hex data via serial communication:

    there was no error message when i try to build with that code but device don't work at all.

    Then maybe what you are sending is not what the device expects, nobody here can know.

    and when i declare data with form of uchar or uint16_t,
    cannot convert argument 1 from uint16_t (or uchar) to const char *
    this error message is come.

    That is not showing the lines of code you try, both for the declarations and the call it fails on. How can we say what's wrong if you won't show what you are writing? I shan't ask again. In any case, yes, neither uint16_t nor uchar, nor pointers to them, are the same as char *, so you would need the correct cast.....



  • @JonB

    Try This

    Convert Your String into Latin1

    And Simply send the data using write()

    serial->write(SERIAL_STRING.toLatin1());



  • @Ketan__Patel__0011
    I do not want to engage in semantic , but
    are you sending RAW hex - 8 bits data or "serial communication " 8 bit code?
    By "serial communication " I mean formatted bit stream including
    start bit - data - stop bits plus optional parity bit(s) ?

    'In other words - since you have stated "it does not work " - is your receiver expecting plain 8 bits raw data or properly formatted "serial communication "?

    And I won't mention " transmission rate" etc.



  • All we have to go on is, "it doesn't work." Could mean almost anything is wrong.

    What output does your compiler produce when compiling this file?
    What output does your code actually produce at run time on the debug console?
    Do not paraphrase it, copy and paste it; even if you think it compiles or runs cleanly.

    In initSerialPort() check the return value from every call to set baud rate, bit, parity, flow control etc. Are they all true (and correct)?

    Check the return value for the call to QSerialPort::write(). Is it the value you expect, something smaller, or -1?

    Understand that bytes may be queued but not actually put on the wire by QSerialPort (QIODevice) until the code returns to the Qt event loop, i.e. sending is not synchronous/blocking. Has your code returned to the Qt event loop before you declare it "does not work" ?



  • @AnneRanch

    QString SERIAL_STRING= "%01#XXXX00000001 + <MY_HEX_VALUE>**";

    This kind of Data I am sending.

    Using this method

    @Ketan__Patel__0011 said in Sending hex data via serial communication:

    serial->write(SERIAL_STRING.toLatin1());


Log in to reply