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.
  • R Offline
    R Offline
    Rika
    wrote on 27 Jul 2020, 07:15 last edited by
    #1

    I have 2 Raspberry Pi4 connected to Zigbee via UART, I wrote the program to receive and send between the two devices. My program can send and receive character data very accurately, but when I send data from a photo, the program never receives enough data. I tried a PC connected to Zigbee and sent pictures to Pi via RealTerm, on Pi I received photos. What is the problem here? Or does anyone have another way to send photos between 2 Raspberry connected to Zigbee via UART? Thanks very much.
    Program send data:

    #include "ui_serial.h"
    #include <QDebug>
    #include <QBuffer>
    #include <QPixmap>
    #include <QFile>
    
    
    serial::serial(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::serial)
    {
        ui->setupUi(this);
    
        serialPort = new QSerialPort(this);
        serialPort->setPortName("ttyAMA0");
        serialPort->setBaudRate(QSerialPort::Baud115200);
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setStopBits(QSerialPort::OneStop);
        serialPort->setFlowControl(QSerialPort::NoFlowControl);
        serialPort->open(QIODevice::ReadWrite);
    
        if (serialPort->isOpen() == true){
            qDebug() <<"Port Opened....";
            connect(serialPort, SIGNAL(readyRead()), this, SLOT(on_readdata()));
        }
    }
    void serial::on_pushButton_2_clicked()
    {
        QFile* imageFile = new QFile("/home/pi/Desktop/image.jpg");
            imageFile->open(QIODevice::ReadOnly);
            QByteArray ba = imageFile->readAll();
            imageFile->close();
            delete imageFile;
            if(serialPort->isOpen()==true){
                serialPort->write(ba);
                qDebug()<<ba.size()<<"size_send:";
            }
    }
    

    Data receiving program:

    #include "ui_serial.h"
    #include <QDebug>
    #include <QBuffer>
    #include <QPixmap>
    #include <QFile>
    #include <QMessageBox>
    
    serial::serial(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::serial)
    {
        ui->setupUi(this);
    
        serialPort = new QSerialPort(this);
        serialPort->setPortName("ttyAMA0");
        serialPort->setBaudRate(QSerialPort::Baud115200);
        serialPort->setDataBits(QSerialPort::Data8);
        serialPort->setStopBits(QSerialPort::OneStop);
        serialPort->setFlowControl(QSerialPort::NoFlowControl);
        serialPort->open(QIODevice::ReadWrite);
    
        if (serialPort->isOpen() == true){
            qDebug() <<"Port Opened....";
            connect(serialPort, SIGNAL(readyRead()), this, SLOT(on_readdata()));
        }
    }
    
    void serial::on_readdata()
    {
        QByteArray ba;
        while (serialPort->waitForReadyRead(1000)){
            ba.append(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, 07:20
    0
    • R Rika
      27 Jul 2020, 07:15

      I have 2 Raspberry Pi4 connected to Zigbee via UART, I wrote the program to receive and send between the two devices. My program can send and receive character data very accurately, but when I send data from a photo, the program never receives enough data. I tried a PC connected to Zigbee and sent pictures to Pi via RealTerm, on Pi I received photos. What is the problem here? Or does anyone have another way to send photos between 2 Raspberry connected to Zigbee via UART? Thanks very much.
      Program send data:

      #include "ui_serial.h"
      #include <QDebug>
      #include <QBuffer>
      #include <QPixmap>
      #include <QFile>
      
      
      serial::serial(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::serial)
      {
          ui->setupUi(this);
      
          serialPort = new QSerialPort(this);
          serialPort->setPortName("ttyAMA0");
          serialPort->setBaudRate(QSerialPort::Baud115200);
          serialPort->setDataBits(QSerialPort::Data8);
          serialPort->setStopBits(QSerialPort::OneStop);
          serialPort->setFlowControl(QSerialPort::NoFlowControl);
          serialPort->open(QIODevice::ReadWrite);
      
          if (serialPort->isOpen() == true){
              qDebug() <<"Port Opened....";
              connect(serialPort, SIGNAL(readyRead()), this, SLOT(on_readdata()));
          }
      }
      void serial::on_pushButton_2_clicked()
      {
          QFile* imageFile = new QFile("/home/pi/Desktop/image.jpg");
              imageFile->open(QIODevice::ReadOnly);
              QByteArray ba = imageFile->readAll();
              imageFile->close();
              delete imageFile;
              if(serialPort->isOpen()==true){
                  serialPort->write(ba);
                  qDebug()<<ba.size()<<"size_send:";
              }
      }
      

      Data receiving program:

      #include "ui_serial.h"
      #include <QDebug>
      #include <QBuffer>
      #include <QPixmap>
      #include <QFile>
      #include <QMessageBox>
      
      serial::serial(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::serial)
      {
          ui->setupUi(this);
      
          serialPort = new QSerialPort(this);
          serialPort->setPortName("ttyAMA0");
          serialPort->setBaudRate(QSerialPort::Baud115200);
          serialPort->setDataBits(QSerialPort::Data8);
          serialPort->setStopBits(QSerialPort::OneStop);
          serialPort->setFlowControl(QSerialPort::NoFlowControl);
          serialPort->open(QIODevice::ReadWrite);
      
          if (serialPort->isOpen() == true){
              qDebug() <<"Port Opened....";
              connect(serialPort, SIGNAL(readyRead()), this, SLOT(on_readdata()));
          }
      }
      
      void serial::on_readdata()
      {
          QByteArray ba;
          while (serialPort->waitForReadyRead(1000)){
              ba.append(serialPort->readAll());
          }
          qDebug()<<ba.size()<<"sizeeeee:";
          QFile newDoc("/home/pi/Desktop/imagexx.jpg");
                  if(newDoc.open(QIODevice::WriteOnly)){
                      newDoc.write(ba);
                  }
                  newDoc.close();
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 27 Jul 2020, 07:20 last edited by
      #2

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

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

      R 1 Reply Last reply 27 Jul 2020, 07:54
      3
      • 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 Offline
            J Offline
            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 Offline
                J Offline
                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 Offline
                    J Offline
                    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 Offline
                        J Offline
                        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 Offline
                            J Offline
                            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 Offline
                                J Offline
                                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

                                          2/42

                                          27 Jul 2020, 07:20

                                          topic:navigator.unread, 40
                                          • Login

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