USB_VCP issues

Hello there,

I´m facing an issue when trying to communicate between a PC application and the openMV board by using an USB_VCP() object. My goal is to send data from the camera to the PC after an appropriate commando is received. In a first attempt I tried to send the control character “CTRL-C” to the camera module which should raise an KeyboardInterrupt exception. After catching the exception the board should answer. Code:

import pyb, sensor, image, time, micropython, sys

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.


usb = pyb.USB_VCP()
pyb.enable_irq(True)          			#Enable Interrupts
usb.setinterrupt(3)           				#CTRL-C on USB --> Keyboard Interrupt
sndImgFlg = False
led = pyb.LED(1)
led.on()

while(True):
    img = sensor.snapshot()         		# Take a picture and return the image.
    try:							# Necessary - dont know why
        pass
    except KeyboardInterrupt:
        sndImgFlg = True
        led.off()
        led = pyb.LED(2)
        led.on()
    if(sndImgFlg == True):
        sndImgFlg = False
        usb.send(data="Received CMD")
	usb.send(data=10)				# Termination character

But when running the code using the terminal openMV IDE provides, I get an error and the program crashes. By the way: I can open the terminal like 1 out of 20 times. Is that a known issue?
After finding a couple of threads about problems with the KeyboardInterrupt I decided to try a different approach which looks like this:

import time, pyb, sensor


usb = pyb.USB_VCP()                         # Virtual COM Port object
usb.setinterrupt(-1)                        # No KeyboardInterrupt (Cannot be catched properly!)
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.
ctr = 1

while(True):
    led = pyb.LED(1)                        # Red LED
    while(not usb.isconnected()):
        led.on()
        time.sleep(150)
        led.off()
        time.sleep(100)
        led.on()
        time.sleep(150)
        led.off()
        time.sleep(600)
    usb.send(data="Connected")
    usb.send(data=10)                       # Termination character
    led = pyb.LED(2)                        # Green LED
    led.on()
    while(usb.isconnected()):
        time.sleep(10000)                   # TIME TO SEND CMD OR NOT
        rcv = usb.readall()                 # Also tried usb.read(), usb.recv() and usb.any()
        if(rcv == None):
            usb.send(data="Received None: " + str(ctr) + " times")
            usb.send(data=10)
            ctr += 1
        else:
            usb.send(data="Received a CMD")
            usb.send(data=10)
    led.off()
led.off()

And here I´m facing a really strange behaviour. When I don´t send a command, I get the “Received None x times”-message. But only once. After reconnecting, I get the message again only once. If I send a command I don´t get the “Received a CMD” message. Not even once. Can anybody help me with this?

Best regards

Hi,

Do you run this code while connected to the IDE ? If so you should try to place the second script in main.py and safe remove the device, reset the cam and it will run that code.

Hi, you can’t use the USB vcp port with OpenMV IDE. The IDE uses that for debug control. If you want another USB port back to the PC you need to attach an FTDI serial converter to the camera’s serial pins.

No I´m not running that code with the openMV IDE.
I place the script as main.py on the camera and reset the camera. If I´m just sending messages to the pc that works fine. But as soon as I send a message to the cam and an anwer back to the pc it seems to cause troubles. I don´t need two usb_vcp objects to communicate in both directions, do I?

All right, I´ve got the Keyboard Interrupt to work. Here´s the code for those with similar Troubles:

import time, pyb, sensor


usb = pyb.USB_VCP()                         # Virtual COM Port object
usb.setinterrupt(3)                         # KeyboardInterrupt (Cannot be catched properly!)
pyb.enable_irq(True)                        # Interrupts allowed
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

while(True):
    led = pyb.LED(1)                        # Red LED
    while(not usb.isconnected()):
        led.on()
        time.sleep(150)
        led.off()
        time.sleep(100)
        led.on()
        time.sleep(150)
        led.off()
        time.sleep(600)
    led.off()
    led = pyb.LED(2)                        # Green LED
    led.on()
    try:
        while(usb.isconnected()):
            img = sensor.snapshot()
    except KeyboardInterrupt:
        usb.send(data="KeyboardInterrupt")
        usb.send(10)
    led.off()
led.off()