Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. read single byte from socket ethernet
QtWS25 Last Chance

read single byte from socket ethernet

Scheduled Pinned Locked Moved Solved Mobile and Embedded
qtcpsocketethernet in qtread one byteclientserver
7 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vishbynature
    wrote on 24 Jul 2019, 08:24 last edited by
    #1

    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?

    J 1 Reply Last reply 24 Jul 2019, 08:28
    0
    • V vishbynature
      24 Jul 2019, 08:24

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Jul 2019, 08:28 last edited by jsulm
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply 24 Jul 2019, 09:03
      4
      • J jsulm
        24 Jul 2019, 08:28

        @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

        V Offline
        V Offline
        vishbynature
        wrote on 24 Jul 2019, 09:03 last edited by
        #3

        @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?

        J 1 Reply Last reply 24 Jul 2019, 11:12
        0
        • V vishbynature
          24 Jul 2019, 09:03

          @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?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 24 Jul 2019, 11:12 last edited by
          #4

          @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
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          V 1 Reply Last reply 27 Jul 2019, 09:09
          5
          • J jsulm
            24 Jul 2019, 11:12

            @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
            
            V Offline
            V Offline
            vishbynature
            wrote on 27 Jul 2019, 09:09 last edited by
            #5

            @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.

            A 1 Reply Last reply 27 Jul 2019, 09:17
            0
            • V vishbynature
              27 Jul 2019, 09:09

              @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.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 27 Jul 2019, 09:17 last edited by
              #6

              @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

              Qt has to stay free or it will die.

              V 1 Reply Last reply 27 Jul 2019, 09:22
              2
              • A aha_1980
                27 Jul 2019, 09:17

                @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

                V Offline
                V Offline
                vishbynature
                wrote on 27 Jul 2019, 09:22 last edited by vishbynature
                #7

                @aha_1980
                ok, thanks help again.
                regards.

                1 Reply Last reply
                0

                1/7

                24 Jul 2019, 08:24

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved