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.
Forum Updated to NodeBB v4.3 + New Features

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 7.0k Views 1 Watching
  • 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 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();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • R Rika

      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();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      3
      • jsulmJ jsulm

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

        mrjjM jsulmJ 2 Replies Last reply
        0
        • R Rika

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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.

          jsulmJ 1 Reply Last reply
          1
          • mrjjM mrjj

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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

            mrjjM 1 Reply Last reply
            1
            • jsulmJ jsulm

              @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());
              }
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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)

              jsulmJ 1 Reply Last reply
              0
              • mrjjM mrjj

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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

                mrjjM 1 Reply Last reply
                0
                • jsulmJ jsulm

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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

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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    1
                    • R Offline
                      R Offline
                      Rika
                      wrote on 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.

                      jsulmJ 1 Reply Last reply
                      0
                      • R Rika

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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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
                        • jsulmJ jsulm

                          @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 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();
                          }
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • R Rika

                            @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();
                            }
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 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
                            1
                            • jsulmJ jsulm

                              @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 last edited by
                              #14

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

                              jsulmJ 1 Reply Last reply
                              0
                              • R Rika

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

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 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
                                2
                                • jsulmJ jsulm

                                  @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 last edited by
                                  #16

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

                                  mrjjM 1 Reply Last reply
                                  0
                                  • R Rika

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

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

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

                                    R 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

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

                                      R Offline
                                      R Offline
                                      Rika
                                      wrote on 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.

                                      mrjjM 1 Reply Last reply
                                      0
                                      • R Rika

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

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 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
                                        0
                                        • mrjjM mrjj

                                          @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 last edited by
                                          #20

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

                                          jsulmJ mrjjM 2 Replies Last reply
                                          0

                                          • Login

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