pyb.USB_VCP and sensor (and led) interference ?

Hello,
I’m developping a simple “get an image and send it” (BTW, any such code “ready to go” is welcome, …)
From my experiments, there is an interference between the pyb.USB_VCP and sensor (and maybe led) modules.
Code is below. In a nutshell, I have the red led blinking each time the usb.readline() is empty, and if something to read, send something back with green led blinking. This part works well (as standalone once the cam is reset)
Now I add sensor.reset(): blinking and usb communication still ok. I add sensor.skip_frames(): still ok.
BUT if I add anything else, e.g., “sensor.set_framesize(sensor.QVGA)”, red led blinking is not happening anymore (???) even if communication is still ok, and if I add an “img = sensor.snapshot()” (or even set_pixformat), the communication is not working anymore.
Note that under the IDE, the led is blinking (!), but not when resetting the cam. I tested many things to turn around (switching lines, adding delays)
Maybe I’m missing something, or there is a problem.
I’m under Windows 10.

I hope someone can help.
Thanks for the attention and regards,
Pascal Mayer
########################## code ######################
import time, pyb, sensor, image

RedLed = pyb.LED(1)
GreenLed = pyb.LED(2)
usb = pyb.USB_VCP()

sensor.reset() # Initialize the camera sensor.

sensor.set_framesize(sensor.QQVGA)

#sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE

sensor.skip_frames(10) # Let new settings take affect.

#sensor.set_whitebal(False) # Turn off white balance.
#sensor.set_framesize(sensor.QQVGA) # or sensor.QQVGA (or others)
#sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
#sensor.skip_frames(10) # Let new settings take affect.

#img = sensor.snapshot()

while(True):
----line = usb.readline()
----if (line):
--------GreenLed.on()
--------pyb.delay(300)
--------GreenLed.off()
--------pyb.delay(300)
--------s = str(line,“utf-8”)
--------if (“go” in s):
------------usb.write(“YES GO\n”)
------------s3 = “Size " + str(5.3) + " " + str(123) + " \n”
------------usb.write(s3)
--------else:
------------s2 = s + " Rodger"
------------usb.write(s2)
----else:
--------RedLed.on()
--------pyb.delay(300)
--------RedLed.off()
--------pyb.delay(300)

Hi, are you trying to use the USB VCP port while the IDE is connected? The IDE and your application can’t share the port. It’s safer to use the regular UART and command the system through that.

Mmm, please create a bug request for a second USB VCP ort n the GitHub. It probably is possible to add another port for this type of activity.

Hello,
Thanks for the answer and attention.
No, I’m not referring to what is happening with the IDE connected.
Once the board is reset and running on its own, the USB connection (and led blinking) works fine as long as I’m not adding sensor.set_“something”, or sensor.snapshot(). That’s why Im’ suspecting an interference between pyb.delay, pyb.USB_VCP and the sensor codes, maybe on the sharing of hardware timers ?

I’ll try to set a bug request on GitHub (but not sure I know how to do).
Thanks and regards,
Pascal

I’ve seen this problem before. It’s do to the readline() call mostlikely. That blocks. Instead, do the any() call and accumulate bytes yourself into a string. Then look for a newline at the end of the string and parse that. Basically, don’t try to read more bytes than any() says are available.

Thanks, I’ll try this and let you know.
Regards, Pascal.