Search code examples
serial-portpyserialusbserialmicropythonopenmv

USB VCP Acces denied with pySerial


I just put my hand on a OpenMV Cam H7 Plus that is using micro-python. I am trying an example that is supposed to take a python program to ask for a picture from the camera and save it. As far as I understand (still new to python) is the camera IDE creates a virtual serial port and listens for a command. The main python program try to open the port (COM4) and is denied. The problem , I think, is the port is already in use. How can I get access?

IDE micro-python code

import sensor, image, time, ustruct
from pyb import USB_VCP

usb = USB_VCP()
sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.

print("USB is a Com Port", usb.isconnected())


while(True):
    cmd = usb.recv(4, timeout=5000)
    if (cmd == b'snap'):
        img = sensor.snapshot().compress()
        usb.send(ustruct.pack("<L", img.size()))
        usb.send(img)

Main python Code

import serial
import struct
port = 'COM4'
sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,
                   xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True)
sp.setDTR(True) # dsrdtr is ignored on Windows.
sp.write("snap")
sp.flush()
size = struct.unpack('<L', sp.read(4))[0]
img = sp.read(size)
sp.close()

with open("img.jpg", "w") as f:
    f.write(img)

And in running the main i get the error:

File "C:\Users\Vincent\usbpcvtest\lib\site-packages\serial\serialwin32.py", line 62, in open raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)


Solution

  • I find out how it works. The Cam auto-run the main.py when you initialize the port with a program on another computer. So all i had to do is renamed my IDE micro-python code "main.py" and to put it in the root of the cam.

    the only bug is if you start the IDE you need to reconnect the cam physicaly so that the cam boot the main.py before you start your program on the main computer.