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