read single byte from socket ethernet



  • Hello,
    i have been testing ethernet communication for a while, succesfully tested communication between Rpi and PC. Now i want to read single byte from PC to RPI i have used "write()", "putChar()" to send and "read()", "readAll()" to receive. now i want to receive single byte from PC. with read() you can only store incoming data in QString ,also used "getChar()" which is a boolean function. is there a way to receive single byte?


  • Qt Champions 2019

    @vishbynature said in read single byte from socket ethernet:

    with read() you can only store incoming data in QString

    Wrong. See https://doc.qt.io/qt-5/qiodevice.html#read-1 and other overloads.
    There is nothing with QString.
    But you did no tell us what you really mean: are you talking about QTcpSocket?

    ""getChar()" which is a boolean function" - and what is the problem?
    From the documentation:
    "Reads one character from the device and stores it in c. If c is 0, the character is discarded. Returns true on success; otherwise returns false."
    https://doc.qt.io/qt-5/qiodevice.html#getChar



  • @jsulm
    Ok I checked the first doc. i understand it well also i tagged QTcpSocket because all his functions(read,write,putChar,getChar) belong to that class.

    "Reads one character from the device and stores it in c. If c is 0, the character is discarded. Returns true on success; otherwise returns false."
    yes i wanted to get c from it.

    ok when i said "read single byte from socket"
    i want to send...... say 121 in a single byte
    but when i receive it as 'y'
    ascii for 'y' is 121.

    i use "putChar(121)" in my Qt code in PC to send over ethernet and receive on my Rpi by "read(1)" as 'y'

    i want to receive it as 121. is this possible?


  • Qt Champions 2019

    @vishbynature said in read single byte from socket ethernet:

    but when i receive it as 'y'
    ascii for 'y' is 121.

    y IS 121 - so it's the same, 121 when interpreted as char is y. So, it works already.

    char ch = 'y'; // Now ch contains number 121
    std::cout << ch; // Will output y
    std::cout << static_cast<int>(y); // will output 121
    


  • @jsulm
    thanks for help.
    now in my program i declare global variable say 'var'.
    and using "socket->getChar(&var);"
    i get the byte in var.
    when i print it like
    ui->textBrowser->append(QString::number(var));
    i get the value.
    was easy but had to work around it.


  • Lifetime Qt Champion

    @vishbynature

    now in my program i declare global variable say 'var'.

    You don't need a global variable for that.

    ui->textBrowser->append(QString::number(var));
    i get the value.
    was easy but had to work around it.

    That's no workaround, that's just how computers work. Every byte has a value, and you can interpret is as ASCII code, that's what happens if you store the byte in a char and print it. What you wanted to do, is to print the numeric value of the byte, hence you need to convert it with QString::number()

    Regards



  • @aha_1980
    ok, thanks help again.
    regards.