length of transferred data through UART

Discussion related to "under the hood" OpenMV topics.
Kahroba
Posts: 9
Joined: Fri Dec 27, 2019 1:30 pm

length of transferred data through UART

Postby Kahroba » Mon Jan 20, 2020 6:28 am

Hello
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 :

Code: Select all

ax.size();
the code in OpenMV IDE is:

Code: Select all


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.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)

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

and NodeMCU code for reading is:

Code: Select all

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

byte tempread;

void setup(){
  Serial.begin(500000);
}

void loop(){
   if (Serial.available()){
          tempread = Serial.read();
          Serial.println(tempread);
      }
   }
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.
User avatar
kwagyeman
Posts: 4269
Joined: Sun May 24, 2015 2:10 pm

Re: length of transferred data through UART

Postby kwagyeman » Mon Jan 20, 2020 3:51 pm

Do:
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.
Nyamekye,
Kahroba
Posts: 9
Joined: Fri Dec 27, 2019 1:30 pm

Re: length of transferred data through UART

Postby Kahroba » Tue Jan 21, 2020 7:26 am

kwagyeman wrote:
Mon Jan 20, 2020 3:51 pm
Do:
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,
User avatar
kwagyeman
Posts: 4269
Joined: Sun May 24, 2015 2:10 pm

Re: length of transferred data through UART

Postby kwagyeman » Thu Jan 23, 2020 12:39 am

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.
Nyamekye,
Kahroba
Posts: 9
Joined: Fri Dec 27, 2019 1:30 pm

Re: length of transferred data through UART

Postby Kahroba » Sat Jan 25, 2020 11:11 am

kwagyeman wrote:
Thu Jan 23, 2020 12:39 am
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

Code: Select all

 uart.write(image) 
?
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.
User avatar
kwagyeman
Posts: 4269
Joined: Sun May 24, 2015 2:10 pm

Re: length of transferred data through UART

Postby kwagyeman » Sat Jan 25, 2020 11:20 am

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: https://forum.arduino.cc/index.php?topic=605324.0, 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.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 9 guests