length of transferred data through UART

Sorry, I could not find similar topic for my problem, so create a new one.
I am trying to transfer an image from Camera to NodeMCU but the lenght of the received data is not as same as the size of the image I get from :


the code in OpenMV IDE is:

import time
from pyb import UART

uart = UART(3, 500000)
uart.init(500000, bits=8 , parity=None , stop=1)

import sensor, image, time, struct
sensor.skip_frames(time = 2000)

clock = time.clock()
quality = 50;
for i in range(1):
    img = sensor.snapshot()
    ax=img.compress(quality= int(quality))
    print(ax.size()  ,  clock.fps())
    uart.write(struct.pack("<l", ax.size()))

and NodeMCU code for reading is:

#include <SPI.h>
#include <SD.h>
// Define Slave Select pin
#define SD_CS  53

byte tempread;

void setup(){

void loop(){
   if (Serial.available()){
          tempread = Serial.read();

For instance, on both OpenMV and NodeMCU IDE I get 1487 as the size of the image but I just received 628 bytes as the image data.
Any help is highly appreciated.


uart = UART(3, 500000)
uart.init(500000, bits=8 , parity=None , stop=1, timeout_char=1000)

The UART stops sending once it timeouts normally. This is the default MicroPython behavior that we inherit.

Thanks for the reply, but no success yet.
I have tried timeout with different values but it still only received around 600 bytes. I have also tried sending the original image without compression but get around 4000 bytes instead of 19200 as well.
It is worth mentioning I have tried different baud rates as well,

Sorry, I thought I replied to this post a while back.

Um, your NodeMCU code is not enough to do what I think needs to be done to receive the image. Can you comment out the part in the OpenMV Cam code where you send anything but the size and then edit the Arduino code to receive the size?

The code you are running on the NodeMCU shouldn’t work at all. It looks like you are opening a Serial port that’s connected to the PC and then printing your output to that and also expecting to receive data from the OpenMV Cam over that link… which doesn’t make sense. You’d need at least 2 UARTs. So, I don’t know what the NodeMCU module is receiving and printing.

The OpenMV code looks correct, I can’t really offer help support for the NodeMCU code however.

Yeah, I think it can be an issue on the NodeMCU side.
SO I have a couple of question:

  1. just for clarification; if the image size in OpenMV IDE is shown 1000 ( for example), then 1000 bytes should be sent by

2) As you mentioned, I think this problem can be because of the limited size of the serial buffer in nodeMCU, so in this case, is there any way to breakdown the image and send it in several parts instead of one?
3) Is there any sample code available for sending the picture through the SPI port?

I am sorry if my question might be a little away from the title of this topic and thanks in advance.

Yes, whatever is printed in the IDE is the exact number of bytes sent.

I believe the issue you are having with NodeMCU is that you need two serial ports. See this: Serial Communication between Arduino and NodeMCU, you can see they create a second uart.

Um, for various reasons, you might want to send the size to NodeMCU as ASCII text rather than in binary. You can do that by doing uart.write("%d\n" % img.size()). This will send the size in bytes as ascii letters which then you can use the serial.parseInt method on node mcu to read the size sent and get that in binary form. Then you should try printing that and confirm the link is working. Once that is the case you can then just setup a loop to read the rest of the bytes.