USB VCP Acces denied with pySerial

I just put my hand on a OpenMV Cam H7 Plus. I am trying an example(image_transfer_jpg_streaming_as_the_remote_device.py) . As far as i understand (still new to python) , the camera IDE create a virtual serial port and listen for a command. The main python program try to open the port (COM4) and is denied. The probleme, i think, is the port is already in use by the IDE.

How can i get access?

I cant find “image_transfer_jpg_streaming_as_the_controller_device.py” that should go with this exemple.

My goal is to take picture of the bubble on top a seringe and send only the image of the bubble(Macro photography). So doing the image detection and doing some image processing before transfering the image make me choose this camera so i can get high resolution of the bubble.

So here is the code, mostly from the exemple with some minor changes

IDE 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)

Thanks in advance

Hi,

Yes if you have the IDE open and connected to the camera then you will get an error with the com port as its already in use.

Saving your IDE Code to main.py on the openmv H7 and disconnecting the device from the ide should resolve the problem.

Then run the code you have labelled as main on your pc/laptop or whatever you are using.

If i copy the IDE code in the main doest mean i need to install the module for micro python on my computer also?

And it sdoesnt make sense… I ihave in 1 part of the code to send a line but the listen command is in a while loop…

OH and the actual main.py must be start from the cam?

Hi,

No, you do not need micro python installed on your computer. Micro python is just a lite version of python specifically for use on microcontrollers like the OpenMV products for example.

When you save your IDE code as main.py on the OpenMV H7 you need to disconnect the camera from the IDE. I normally disconnect the cam in the IDE and also plug the usb out and back in again just to be sure.

Then you can run the code you have labelled as Main Python Code on your computer as standard python code assuming you have the necessary packages installed such as pyserial etc.

Please avoid examples that try to use the USB port if you are a beginner. You need to have code that’s basically bug free to use that port.

We only have one VCP port on the device. While you can write code to use it… you loose the ability to debug with OpenMV IDE. So, your code needs to be very correct. Given our new partnership with Arduino this may get updated sooner than later.

MicroPython added the ability to have up to 3 USB VCP ports. We may be able to get that working in the future to make a special debug and normal port for the IDE and user applications. However, we are not there yet.

If you have the WiFi shield it’s far easier to stream data using that than USB for getting your script up and running. You can also use a USB to serial converter with our hardware serial ports.

Alright i figured it out! Thanks!

I didnt know that the main.py put in the cam was run when the computer open the port.

I put pyb.led in the main.py to know when the cam was ready to receive the data. But yes, you cant see whhat ht emain.py does so it need to be bug free, but you still can test your program with the IDE. This cam is a nice Jewel compared of what i tried to used before… An obsolete MC-433C Firewire CCD Camera

Yup, and we keep releasing firmware updates! So, it gets better.