Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.
-
@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?
@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.
-
-
@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).
-
@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.
-
@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(); }
-
@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."
-
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(); } }
-
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(); } }
-
@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.
@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 ? -
@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 ? -
@mrjj
144 sizeeeee:
160 sizeeeee:
176 sizeeeee:
192 sizeeeee:
208 sizeeeee:
224 sizeeeee:
240 sizeeeee:
241 sizeeeee:@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(); } }
-
@mrjj
144 sizeeeee:
160 sizeeeee:
176 sizeeeee:
192 sizeeeee:
208 sizeeeee:
224 sizeeeee:
240 sizeeeee:
241 sizeeeee:@Rika
But it stops at 241 every time?Maybe its due to data.
Could you try in your sent functionvoid 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.
-
@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(); } }
-
@jsulm I have made your way, the above is the result from qDebug (). It only received 241 bytes so file imagexxx.jpg was not created. (my imageSize= 31021).
@Rika said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:
@jsulm I have made your way, the above is the result from qDebug (). It only received 241 bytes so file imagexxx.jpg was not created. (my imageSize= 31021).
at this point, you're not getting away with a simple I've tried that, it does not work.
@jsulm is absolutely correct. Show us your actual code
-
@Rika
But it stops at 241 every time?Maybe its due to data.
Could you try in your sent functionvoid 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.
-
@Rika said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:
@jsulm I have made your way, the above is the result from qDebug (). It only received 241 bytes so file imagexxx.jpg was not created. (my imageSize= 31021).
at this point, you're not getting away with a simple I've tried that, it does not work.
@jsulm is absolutely correct. Show us your actual code
@J-Hilk
Do you know if write blocks ?
QByteArray ba; // local
ba.fill('A', 1000);
if(serialPort->isOpen()==true){
serialPort->write(ba); // blocks here ? as ba is by reference -
@Rika said in Unable to send and receive images between 2 Raspberry connected to Zigbee via UART.:
@jsulm I have made your way, the above is the result from qDebug (). It only received 241 bytes so file imagexxx.jpg was not created. (my imageSize= 31021).
at this point, you're not getting away with a simple I've tried that, it does not work.
@jsulm is absolutely correct. Show us your actual code
@J-Hilk
Send:
#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:"; } }
Rec:
#include "serial.h" #include "ui_serial.h" #include <QtSerialPort/QSerialPort> #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())); } } serial::~serial() { delete ui; serialPort->close(); } void serial::on_readdata() { ba.append( serialPort->readAll()); qDebug()<<ba.size()<<"sizeeeee:"; if (ba.size() == 31021) { QFile newDoc("/home/pi/Desktop/imagexx.jpg"); if(newDoc.open(QIODevice::WriteOnly)){ newDoc.write(ba); } newDoc.close(); } // ui->label->setText(ba); // QPixmap b; // if(b.loadFromData(ba,"JPG")){ // ui->label->setPixmap(b); // } }
-
@J-Hilk
Do you know if write blocks ?
QByteArray ba; // local
ba.fill('A', 1000);
if(serialPort->isOpen()==true){
serialPort->write(ba); // blocks here ? as ba is by reference