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

How could I send an array to serial port?



  • Hi,

    I need to send this array

    test[121]={33, 20, 23, 20, 27, 27, 25, 29, 14, 12, 10, 74, 24, 29, 44, 57, 63, 62, 61, 42, 30, 30, 185, 178, 93, 50, 68, 74, 76, 69, 59, 36, 38, 255, 255, 115, 56, 73, 89, 91, 78, 64, 41, 45, 255, 248, 88, 61, 76, 105, 109, 89, 69, 45, 72, 255, 245, 85, 63, 76, 103, 119, 91, 67, 47, 65, 255, 252, 100, 60, 72, 94, 107, 83, 62, 39, 54, 255, 255, 134, 52, 67, 78, 84, 72, 56, 28, 56, 255, 255, 205, 58, 59, 66, 66, 61, 44, 23, 60, 255, 255, 255, 123, 46, 60, 61, 56, 37, 39, 60, 238, 241, 249, 176, 52, 47, 52, 47, 85, 93, 91};

    To Serial Port, however it is sending:

    !\x14\x17\x14\x1B\x1B\x19\x1D\x0E\f\nJ\x18\x1D,9?>=*\x1E\x1E\xB9\xB2]2DJLE;$&\xFF\xFFs8IY[N@)-\xFF\xF8X=LimYE-H\xFF\xF5U?Lgw[C/A\xFF\xFC""d<H^kS>'6\xFF\xFF\x86""4CNTH8\x1C""8\xFF\xFF\xCD:;BB=,\x17<\xFF\xFF\xFF{.<=8%'<\xEE\xF1\xF9\xB0""4/4/U][

    There is a way to send integer number to serial port?

    Thanks



  • Hi @manum,

    Actualy that's what you are doing here. But it seems you are reading them as some hexadecimal string format. I guess you are displaying the content of a QByteArray with qDebug() here. What you can do is using data() to get a char * and read the data as char * or casted to unsigned char *.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you currently sending the data ?
    What is on the receiving end ?



  • Hi again,

    My code is

    QByteArray c(test, 121);
    serial2.write(c);

    I send to serial port, the receiving is an arduino, the code is:
    byte patron[121];
    for(int x=1; x<122; x++){
    patron[x-1]=pattern[x];
    }

    And then the qt code receive the comunication from arduino

    QByteArray buffer= serial2.readAll();

    qDebug() << "resultado= "<<buffer;

    The issue is that when I check the array test with the following code:

    qDebug() <<"c= "<< c;

    Qt print :

    !\x14\x17\x14\x1B\x1B\x19\x1D\x0E\f\nJ\x18\x1D,9?>=*\x1E\x1E\xB9\xB2]2DJLE;$&\xFF\xFFs8IY[N@)-\xFF\xF8X=LimYE-H\xFF\xF5U?Lgw[C/A\xFF\xFC""d<H^kS>'6\xFF\xFF\x86""4CNTH8\x1C""8\xFF\xFF\xCD:;BB=,\x17<\xFF\xFF\xFF{.<=8%'<\xEE\xF1\xF9\xB0""4/4/U][

    Instead of

    test[121]={33, 20, 23, 20, 27, 27, 25, 29, 14, 12, 10, 74, 24, 29, 44, 57, 63, 62, 61, 42, 30, 30, 185, 178, 93, 50, 68, 74, 76, 69, 59, 36, 38, 255, 255, 115, 56, 73, 89, 91, 78, 64, 41, 45, 255, 248, 88, 61, 76, 105, 109, 89, 69, 45, 72, 255, 245, 85, 63, 76, 103, 119, 91, 67, 47, 65, 255, 252, 100, 60, 72, 94, 107, 83, 62, 39, 54, 255, 255, 134, 52, 67, 78, 84, 72, 56, 28, 56, 255, 255, 205, 58, 59, 66, 66, 61, 44, 23, 60, 255, 255, 255, 123, 46, 60, 61, 56, 37, 39, 60, 238, 241, 249, 176, 52, 47, 52, 47, 85, 93, 91};

    If Arduino doesn't receive the array as integer, it doesn't work.

    Maybe it is clearer now.

    Thanks in advanced



  • @manum
    qDebug() displays the content of QByteArray as string/printable characters. Characters outside normal printable will be displayed in \xDD format. Bearing that in mind, the output you show does match byte-for-byte with what you have in your test[] array, e.g. from the start 33, 20, 23 shows as !\x14\x17. So it is sending what you give it, you are just not understanding how qDebug() is showing the bytes.

    Now, whether what you are doing at the receiving end with the stream is correct/appropriate I cannot say.



  • @JonB Thanks, so how could I print "test"?



  • @manum
    Well, you are already printing it! Just I think you don't like the output format.

    I think you mean to see it more like your 33, 20, 23, 20, 27, ... input. So you want a decimal-number-string output for each byte received. I can't test, but I think something like http://doc.qt.io/qt-5/qstring.html#number-1 will do it, so call QString::number(b) for each b byte in the QByteArray?



  • @manum

        QString output;
        for(int i = 0; i < c.size(); i++){
            output += QString::number(c.at(i) & 0xFF);
            if(i < (c.size() - 1))
                output += ",";
        }
        qDebug() << output;
    

Log in to reply