我只是把手放在使用 micro-python 的 OpenMV Cam H7 Plus 上。我正在尝试一个示例,该示例应该使用 python 程序从相机中获取图片并保存。据我了解(仍然是 python 的新手)是相机 IDE 创建一个虚拟串行端口并侦听命令。主 python 程序尝试打开端口 (COM4) 并被拒绝。我认为问题在于端口已经在使用中。我怎样才能获得访问权?
IDE微型python代码
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)
主要python代码
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)
在运行 main 我得到错误:
文件“C:\Users\Vincent\usbpcvtest\lib\site-packages\serial\serialwin32.py”,第 62 行,在 open raise SerialException("could not open port {!r}: {!r}".format( self.portstr, ctypes.WinError())) serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Access is denied.', None, 5)