Lopy with VC0706



  • Hello,
    I am currently trying to make the VC0706 work on my Lopy.

    I tried on my raspi first to ensure the camera module is correctly working and that the python code is OK as well. I tested both 5V and 3.3V and it was working fine.

    Now, I tried to run it on my Lopy: I connected the Rx and Tx pins of the VC0706 to the Tx and Rx of the Lopy (P3 (or G24) and P4 (or G11)).

    I write a command to set the size of the images tkaen and then I want to check the response from the module to ensure the setting is OK. But I have a None response.

    Here is what I do:

    s = UART(1, 38400)
    s.init(baudrate=38400, timeout_chars=2, bits=8, parity=None, stop=1)
    nbBytes = s.write(b'560031050401001900') #the string parameter is generated in my code
    s.read(nbBytes)

    I tried to change the timeout_chars by without any success.

    Also, I connected a wire between the Rx and the Tx of the Lopy and I sent some data to it to ensure the UART was working fine

    s.write(b'560031050401001900')
    18
    s.read(18)
    b'105040100190056003'

    So I saw that the charcters were not really in the correct order and I wondered if that was normal or not?

    If anyone has any idea, I'll take it!

    Thank you :)



  • @matamune
    you can turn automatic gc on

    gc.enable()
    

    and also you can do manual gc.collect()



  • Ok so it worked: I had to wrap my array into a bytearray i.e: f.write(bytearray(myArray)) after having opened the file in wb mode.

    The picture I saved is around 4kb but this is low quality mode. When I tried to set the better quality mode, the picture is around 40kb I get a memory allocation error. As I write the data into the file as I get the data, should I call the garbage collector in order to avoid such error? Or this is simply not possible due to internal memory limitation?

    I'll do some rework on the code and I'll put it here ;)

    Thank you



  • @livius said in Lopy with VC0706:

    @matamune said in Lopy with VC0706:

    I think I'll do a write(''.join(myArray) ) to write th

    no this is not good way
    you can write binary data to file
    but you must open it with wb or r+b mode

    Oh, OK then I'll do a simple f.write(r[:5 + nbBytes + 5]) after opened the file with wb flag.

    I'll test and I'll let you know ;)



  • @matamune said in Lopy with VC0706:

    I think I'll do a write(''.join(myArray) ) to write th

    no this is not good way
    you can write binary data to file
    but you must open it with wb or r+b mode



  • Sorry for the double post.

    So I tried that, and I think it will work correctly.

    First I struggled with an Erno 5 EIO error when trying to open a file.
    This was caused by my s = UART(1, BAUD) line: the UART was initialized using the default pins which are also used by the SD reader. Even so I did s.init(...) with custom pins for the Tx/Rx I was not freeing the slot I guess.

    Now I have a object with buffer protocol required error when I try to write data into the file but I think it is because I try to write an array directly into the file and that I opened it with 'w' flag instead of 'wb' while I want to write an image (thus bytes) into the file.
    I think I'll do a write(''.join(myArray) ) to write the photo read from the VC0706 module.

    I'll keep you posted with my final solution ;)



  • @livius said in Lopy with VC0706:

    @matamune said in Lopy with VC0706:

    if there is anyway to solve

    Write data into file instead to big buffer in memory

    Hi,
    Thank you, that what I thought, I'll try that tonight then.



  • @matamune said in Lopy with VC0706:

    if there is anyway to solve

    Write data into file instead to big buffer in memory



  • @seb said in Lopy with VC0706:

    s = UART(1, 38400)
    s.init(baudrate=38400, timeout_chars=2, bits=8, parity=None,
    stop=1, pins=('P3', 'P4')) # TX, RX

    s.readall()
    nbBytes = s.write(b'560031050401001900')
    time.sleep(0.01)
    print(s.read(nbBytes))

    Hi,
    Ok with the sleep before the read I got it working.

    Now, I have another issue: I try to read 3592 bytes (or around 48000 bytes) by packet of 300 bytes but at some point when I try to send the image to a server using urequest (socket) I got

    MemoryError: memory allocation failed, allocating 7688 bytes

    I guess there is limited amount of memory available in the LoPy and I wonder if there is anyway to solve this kind of memory issue by freeing some space somewere (I did not put any other file on the Lopy)?

    Thank you again ;)



  • @matamune said in Lopy with VC0706:

    s.read(18)

    Hi,

    A couple of things to note with the UART. Firstly, the buffer is running all the times, so if you want to read an exact number of bytes, you will need to clear the buffer before you send the data with a s.readall(). Secondly, it takes some time for the message to send so you will need to wait. With the example you posted above where the UART is connected to itself I got it working as expected with the following code:

    import time
    
    s = UART(1, 38400)
    s.init(baudrate=38400, timeout_chars=2, bits=8, parity=None,
           stop=1, pins=('P3', 'P4'))  # TX, RX
    
    s.readall()
    nbBytes = s.write(b'560031050401001900')
    time.sleep(0.01)
    print(s.read(nbBytes))
    


Pycom on Twitter