Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.

Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.

Scheduled Pinned Locked Moved Unsolved General and Desktop
42 Posts 4 Posters 6.8k 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.
  • J jsulm
    27 Jul 2020, 07:20

    @Rika said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:

    while (serialPort->waitForReadyRead(1000)){
    ba.append(serialPort->readAll());
    }

    How do you actually know when the whole picture was received?
    You need a protocol. For example you could first send an int containing the image size and then send the actual image. This way the receiver would know how many bytes to receive.

    R Offline
    R Offline
    Rika
    wrote on 27 Jul 2020, 07:54 last edited by
    #3

    @jsulm Thank you for replying. I tried sending images from a PC (using Realterm to send pictures), on my Pi 4, I ran the receiving program and got the images. Could the error be in the sending program?

    M J 2 Replies Last reply 27 Jul 2020, 07:57
    0
    • R Rika
      27 Jul 2020, 07:54

      @jsulm Thank you for replying. I tried sending images from a PC (using Realterm to send pictures), on my Pi 4, I ran the receiving program and got the images. Could the error be in the sending program?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Jul 2020, 07:57 last edited by mrjj
      #4

      @Rika
      Hi
      There is no real error as such. it's just that data will come as multiple read and
      the current code will then save only the last seen data piece as imagexx.jpg
      and the image will always appear broken.

      Overlooked waitForReadyRead in slot

      That is why @jsulm want you to include the size of the image so we can know we got all data.

      J 1 Reply Last reply 27 Jul 2020, 08:01
      1
      • M mrjj
        27 Jul 2020, 07:57

        @Rika
        Hi
        There is no real error as such. it's just that data will come as multiple read and
        the current code will then save only the last seen data piece as imagexx.jpg
        and the image will always appear broken.

        Overlooked waitForReadyRead in slot

        That is why @jsulm want you to include the size of the image so we can know we got all data.

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 27 Jul 2020, 08:01 last edited by
        #5

        @mrjj said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:

        There is no real error as such. it's just that data will come as multiple read and
        the current code will then save only the last seen data piece as imagexx.jpg

        Are you sure?

        QByteArray ba;
        while (serialPort->waitForReadyRead(1000)){
            ba.append(serialPort->readAll());
        }
        

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

        M 1 Reply Last reply 27 Jul 2020, 08:04
        1
        • J jsulm
          27 Jul 2020, 08:01

          @mrjj said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:

          There is no real error as such. it's just that data will come as multiple read and
          the current code will then save only the last seen data piece as imagexx.jpg

          Are you sure?

          QByteArray ba;
          while (serialPort->waitForReadyRead(1000)){
              ba.append(serialPort->readAll());
          }
          
          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Jul 2020, 08:04 last edited by mrjj
          #6

          @jsulm
          Hi
          Àhh on first ready signal, it will call the slot and the
          waitForReadyRead + loop will then read all remaining data.
          Ok that should actually do it.
          ( if really never breaks the while loop)

          J 1 Reply Last reply 27 Jul 2020, 08:10
          0
          • M mrjj
            27 Jul 2020, 08:04

            @jsulm
            Hi
            Àhh on first ready signal, it will call the slot and the
            waitForReadyRead + loop will then read all remaining data.
            Ok that should actually do it.
            ( if really never breaks the while loop)

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 27 Jul 2020, 08:10 last edited by
            #7

            @mrjj Yes, possible that the timeout kicks in and terminates the loop before everything was received.

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

            M 1 Reply Last reply 27 Jul 2020, 08:17
            0
            • J jsulm
              27 Jul 2020, 08:10

              @mrjj Yes, possible that the timeout kicks in and terminates the loop before everything was received.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 27 Jul 2020, 08:17 last edited by mrjj
              #8

              @jsulm
              Hi
              I also wonder if that would happen if image was say 1 MB.

              But it should be clear to poster if its called more than once with the
              qDebug()<<ba.size()<<"sizeeeee:";

              1 Reply Last reply
              0
              • R Rika
                27 Jul 2020, 07:54

                @jsulm Thank you for replying. I tried sending images from a PC (using Realterm to send pictures), on my Pi 4, I ran the receiving program and got the images. Could the error be in the sending program?

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 27 Jul 2020, 08:21 last edited by
                #9

                @Rika I suggest you implement the receiving part in a proper way: without mixing asynchronous and synchronous ways to read data. Remove the loop from serial::on_readdata(). Put QByteArray ba as member variable in the class. Accumulate data in ba untill you got all data.

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

                R 1 Reply Last reply 27 Jul 2020, 08:29
                1
                • R Offline
                  R Offline
                  Rika
                  wrote on 27 Jul 2020, 08:25 last edited by
                  #10

                  @jsulm @mrjj I used qDebug to find out how much data was sent and received. I sent a 30kB image, the data I got from "qDebug()<<ba.size()<<"sizeeeee:";" was 132, I tried many times and all received 132. I used different photos, they gave different results but always the same.

                  J 1 Reply Last reply 27 Jul 2020, 08:28
                  0
                  • R Rika
                    27 Jul 2020, 08:25

                    @jsulm @mrjj I used qDebug to find out how much data was sent and received. I sent a 30kB image, the data I got from "qDebug()<<ba.size()<<"sizeeeee:";" was 132, I tried many times and all received 132. I used different photos, they gave different results but always the same.

                    J Online
                    J Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 27 Jul 2020, 08:28 last edited by
                    #11

                    @Rika Please read https://doc.qt.io/qt-5/qserialport.html#waitForReadyRead "This function blocks until new data is available for reading and the readyRead() signal has been emitted" and my previous post (no need for waitForReadyRead and the loop).

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

                    1 Reply Last reply
                    2
                    • J jsulm
                      27 Jul 2020, 08:21

                      @Rika I suggest you implement the receiving part in a proper way: without mixing asynchronous and synchronous ways to read data. Remove the loop from serial::on_readdata(). Put QByteArray ba as member variable in the class. Accumulate data in ba untill you got all data.

                      R Offline
                      R Offline
                      Rika
                      wrote on 27 Jul 2020, 08:29 last edited by Rika
                      #12

                      @jsulm You mean this?

                      void serial::on_readdata()
                      {
                          QByteArray ba(serialPort->readAll());
                          qDebug()<<ba.size()<<"sizeeeee:";
                          QFile newDoc("/home/pi/Desktop/imagexx.jpg");
                                  if(newDoc.open(QIODevice::WriteOnly)){
                                      newDoc.write(ba);
                                  }
                                  newDoc.close();
                      }
                      
                      J 1 Reply Last reply 27 Jul 2020, 08:30
                      0
                      • R Rika
                        27 Jul 2020, 08:29

                        @jsulm You mean this?

                        void serial::on_readdata()
                        {
                            QByteArray ba(serialPort->readAll());
                            qDebug()<<ba.size()<<"sizeeeee:";
                            QFile newDoc("/home/pi/Desktop/imagexx.jpg");
                                    if(newDoc.open(QIODevice::WriteOnly)){
                                        newDoc.write(ba);
                                    }
                                    newDoc.close();
                        }
                        
                        J Online
                        J Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 27 Jul 2020, 08:30 last edited by
                        #13

                        @Rika No, not like this. Please read my post above "Put QByteArray ba as member variable in the class. Accumulate data in ba untill you got all data."

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

                        R 1 Reply Last reply 27 Jul 2020, 08:33
                        1
                        • J jsulm
                          27 Jul 2020, 08:30

                          @Rika No, not like this. Please read my post above "Put QByteArray ba as member variable in the class. Accumulate data in ba untill you got all data."

                          R Offline
                          R Offline
                          Rika
                          wrote on 27 Jul 2020, 08:33 last edited by
                          #14

                          @jsulm Sorry, I don't really understand what you mean. Can you give an example with code?

                          J 1 Reply Last reply 27 Jul 2020, 08:43
                          0
                          • R Rika
                            27 Jul 2020, 08:33

                            @jsulm Sorry, I don't really understand what you mean. Can you give an example with code?

                            J Online
                            J Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on 27 Jul 2020, 08:43 last edited by
                            #15

                            @Rika

                            void serial::on_readdata()
                            {
                                ba.apend(serialPort->readAll());
                                qDebug()<<ba.size()<<"sizeeeee:";
                                if (ba.size() == imageSize) {
                                    QFile newDoc("/home/pi/Desktop/imagexx.jpg");
                                    if(newDoc.open(QIODevice::WriteOnly)){
                                        newDoc.write(ba);
                                    }
                                    newDoc.close();
                                }
                            }
                            

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

                            R 1 Reply Last reply 27 Jul 2020, 08:57
                            2
                            • J jsulm
                              27 Jul 2020, 08:43

                              @Rika

                              void serial::on_readdata()
                              {
                                  ba.apend(serialPort->readAll());
                                  qDebug()<<ba.size()<<"sizeeeee:";
                                  if (ba.size() == imageSize) {
                                      QFile newDoc("/home/pi/Desktop/imagexx.jpg");
                                      if(newDoc.open(QIODevice::WriteOnly)){
                                          newDoc.write(ba);
                                      }
                                      newDoc.close();
                                  }
                              }
                              
                              R Offline
                              R Offline
                              Rika
                              wrote on 27 Jul 2020, 08:57 last edited by
                              #16

                              @jsulm I tried it and it failed, I sent 31021 bytes and always received 241 bytes.

                              M 1 Reply Last reply 27 Jul 2020, 08:59
                              0
                              • R Rika
                                27 Jul 2020, 08:57

                                @jsulm I tried it and it failed, I sent 31021 bytes and always received 241 bytes.

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 27 Jul 2020, 08:59 last edited by
                                #17

                                @Rika
                                So it only shows
                                qDebug()<<ba.size()<<"sizeeeee:";
                                1 time with size 241 ?

                                R 1 Reply Last reply 27 Jul 2020, 09:05
                                0
                                • M mrjj
                                  27 Jul 2020, 08:59

                                  @Rika
                                  So it only shows
                                  qDebug()<<ba.size()<<"sizeeeee:";
                                  1 time with size 241 ?

                                  R Offline
                                  R Offline
                                  Rika
                                  wrote on 27 Jul 2020, 09:05 last edited by Rika
                                  #18

                                  @mrjj No, the last time was 241. My old way it also received only 241 bytes, so I was thinking of the possibility of an error sent by the program.

                                  M 1 Reply Last reply 27 Jul 2020, 09:12
                                  0
                                  • R Rika
                                    27 Jul 2020, 09:05

                                    @mrjj No, the last time was 241. My old way it also received only 241 bytes, so I was thinking of the possibility of an error sent by the program.

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 27 Jul 2020, 09:12 last edited by
                                    #19

                                    @Rika said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:

                                    No, the last time was 241.

                                    So you do get mutiple
                                    qDebug()<<ba.size()<<"sizeeeee:";
                                    messages ?

                                    R 1 Reply Last reply 27 Jul 2020, 09:19
                                    0
                                    • M mrjj
                                      27 Jul 2020, 09:12

                                      @Rika said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:

                                      No, the last time was 241.

                                      So you do get mutiple
                                      qDebug()<<ba.size()<<"sizeeeee:";
                                      messages ?

                                      R Offline
                                      R Offline
                                      Rika
                                      wrote on 27 Jul 2020, 09:19 last edited by
                                      #20

                                      @mrjj
                                      144 sizeeeee:
                                      160 sizeeeee:
                                      176 sizeeeee:
                                      192 sizeeeee:
                                      208 sizeeeee:
                                      224 sizeeeee:
                                      240 sizeeeee:
                                      241 sizeeeee:

                                      J M 2 Replies Last reply 27 Jul 2020, 09:20
                                      0
                                      • R Rika
                                        27 Jul 2020, 09:19

                                        @mrjj
                                        144 sizeeeee:
                                        160 sizeeeee:
                                        176 sizeeeee:
                                        192 sizeeeee:
                                        208 sizeeeee:
                                        224 sizeeeee:
                                        240 sizeeeee:
                                        241 sizeeeee:

                                        J Online
                                        J Online
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 27 Jul 2020, 09:20 last edited by
                                        #21

                                        @Rika As I already said: you have to accumulate all these data in ba and when you got everything then you store it in the file.
                                        Again the code:

                                        void serial::on_readdata()
                                        {
                                            ba.apend(serialPort->readAll());
                                            qDebug()<<ba.size()<<"sizeeeee:";
                                            if (ba.size() == imageSize) {
                                                QFile newDoc("/home/pi/Desktop/imagexx.jpg");
                                                if(newDoc.open(QIODevice::WriteOnly)){
                                                    newDoc.write(ba);
                                                }
                                                newDoc.close();
                                            }
                                        }
                                        

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

                                        R 1 Reply Last reply 27 Jul 2020, 09:26
                                        0
                                        • R Rika
                                          27 Jul 2020, 09:19

                                          @mrjj
                                          144 sizeeeee:
                                          160 sizeeeee:
                                          176 sizeeeee:
                                          192 sizeeeee:
                                          208 sizeeeee:
                                          224 sizeeeee:
                                          240 sizeeeee:
                                          241 sizeeeee:

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 27 Jul 2020, 09:24 last edited by mrjj
                                          #22

                                          @Rika
                                          But it stops at 241 every time?

                                          Maybe its due to data.
                                          Could you try in your sent function

                                          void serial::on_pushButton_2_clicked()
                                          {
                                          
                                          
                                                  QByteArray ba;
                                                  ba.fill('A', 1000);
                                                  if(serialPort->isOpen()==true){
                                                      serialPort->write(ba);
                                                      qDebug()<<ba.size()<<"size_send:";
                                                  }
                                          }
                                          

                                          and see if that changes how you the reading goes.
                                          This just sends 1000 A.

                                          Also i wonder if write will copy the data and then send / if write sent it all there as else we have the issue of the sent buffer running out of scope.

                                          R 1 Reply Last reply 27 Jul 2020, 09:33
                                          0

                                          12/42

                                          27 Jul 2020, 08:29

                                          topic:navigator.unread, 30
                                          • Login

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