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

problem conversion hex ascii



  • hello, i'm trying to send a QByteArray from a serialport to read it on another serialport on my Pi4. i use async serial port;

    the message to send is made of 27 bytes of 2 octets starting by "a1"

    this is a part of code :
    "
    BuffSerie = QByteArray::fromHex("a1061c0800080b01b70e4701b705ff00000000000000000000fe28");
    SerialPortWriter.write(BuffSerie);
    "
    the result is this one :

    "\xA1\x06\x1C\b\x00\b\x0B\x01\xB7\x0EG\x01\xB7\x05\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFE("

    with a problem because there a "b" instead of x08 and a "G" instead of x47. moreover the "b" replace the "080008" original character...

    i saw on the web that 08 is the hex value for "b" character on ascii and 47 is the hex value for "G" on ascii.
    i don't understand why there is a conversion between hex and ascii and where there is this conversion, at the sent or at the reception ????

    is someone can help me please ? if you need more detail, don't hesitate to tell me what you need...

    i hope that my question is clear, i'm not an informaticien neither a good english speaker !!! :-)
    thank you!


  • Lifetime Qt Champion

    @powair said in problem conversion hex ascii:

    i don't understand why there is a conversion between hex and ascii and where there is this conversion, at the sent or at the reception ????

    What you see is the debug output of your QByteArray so all is right since the debug output prints only the hex code if there is no printable character.



  • @powair Hello and welcome to Qt Forum.

    What you are seeing is related to "ASCII encoding table"
    Some bytes have a "special" usage, for example:

    • char 10 or 0x0a (in hexadecimal), is also called "newline" and noted '\n'
    • char 13 or 0x0d (in hexadecimal), is also called "carriage return" and noted '\r'
    • char 8 or 0x08 (in hexadecimal), is also called "backspace" and noted '\b'

    If you want to compare the received buffer use toHex(), like this:

    QByteArray buffer = m_serial.readAll();
    qDebug() << QString(buffer.toHex());
    


  • thank you guys to this first step!!!
    using the QString (buffer.toHex()), i solved my problem and i see the correct print on my screen.

    but the print on my screen was just used to check if the information "a1061c0800080b01b70e4701b705ff00000000000000000000fe28" was well sent!

    when i receive this information i check that the checksum (fe28) is equal to the one received, and it's not the right !

    i used the following code :

    int Serial::CRC16()
    {

    uint16_t   Crc ,  RxCrc, toto;
    char i,j;
    Crc = 0;
    for(i=0;i<25;i++)							//25  = 2x12 PLUS start  si pas de start init ===>  i=1
        {
        toto = (uint16_t)  (BuffSerie.at(i));  
        
        Crc  = Crc ^ (toto << 8);
        for(j=0;j<8;j++)
            {
            if(Crc & 0x8000)
                {
                Crc = Crc<< 1^0x1021 ;
                }
            else
                {
                Crc = Crc<<1;
                }
            }
        }
    RxCrc =  BuffSerie.at(25);
    RxCrc = RxCrc << 8;
    RxCrc = RxCrc | BuffSerie.at(26);
    qDebug() << (Crc);
    qDebug() << (RxCrc);
    //fprintf(outfile,"%x",BuffSerie.at(25));
    //fprintf(outfile,"%x",BuffSerie.at(26));
    if (Crc==RxCrc)
        return(1);
    else
        return(0);
    

    }

    the calcul is wrong.

    it seems to me that the original information is modified when i write it in the BuffSerie.
    would that be it?

    to understand better, the information is a signal sent by a radiocommand, this signal is done with 26 bytes of two octet (the first one is "A1") and one more byte that is the checksum.
    when a read the real signal, i can check the checksum and write it in a textfile, but when a write the value of the signal in a buffer, it doesn't work !!!

    i don't know why ???


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you sure you are computing the checksum the same way ? e.g. using the raw values and not the hex version ?
    Are you sure you are sending the checksum the correct way ?



  • the only things that i'm sur, that is i don't know really what i do ! :-) !!! can you detail your thought?


Log in to reply