Executing Main Module Example

Hi Guys,

Going back to basic please.

I have read other members in the past struggle with this concept. eg Missing modules when installing on Windows 7 Missing modules when installing on Windows 7 - OpenMV Products - OpenMV Forums.

Using the IDE, the code will flash the correctly the colours when connected and disconnected.

# Main Module Example
#
# When your OpenMV Cam is disconnected from your computer it will either run the
# main.py script on the SD card (if attached) or the main.py script on
# your OpenMV Cam's internal flash drive.

import time, pyb

led = pyb.LED(3) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
usb = pyb.USB_VCP() # This is a serial port object that allows you to
# communciate with your computer. While it is not open the code below runs.

while(not usb.isconnected()):
    led = pyb.LED(1)
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(100)
    led = pyb.LED(3)
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(600)
    print("HELLO DISCONNECTED")

led = pyb.LED(2) # Switch to using the green LED.

while(usb.isconnected()):
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(100)
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(600)
    print("Hello CONNECTED")

Saving the code to main.py via Tools → Save to main.py and then reset cam… the cam will flash red and blue - ie disconnected.

A few times, you mentioned “Now on the next power up, the cam will run your code instead of the default”. The USB cable is giving power to the cam - hence flash disconnected colours. Without using IDE, how can I make a connection the the cam?

Using python (Atom/Visual Studio Code) to connect to the CAM’s com port

# Windows OS
import serial
import time

ser = serial.Serial('COM7', 115200, timeout=1, dsrdtr=False)
ser.dtr = False
ser.setDTR(False)

print(ser.readline())
print(ser.isOpen)

while True:
    line = ser.readline().strip()
    if line:
        print(line)

By running the python code (windows 7), I would assume main.py script is executed and switch colours to ‘connected colours’…but it does not. I would expect to read one of the print statements too but it doesnt.

What am I missing? Have a not setup my python environment properly?

As you know, my first goal is to read the CAM’s print() statement in python.

Thank you in advance.

Hi, I replied to you via the private messages about this and showed that the code works fine for me. I don’t know how to help you given standard tooling works with them system for what you are trying to do.

After you updated the code on the camera please verify the file was written. You should be able to see the main.py being updated with your script.

This is the config for a serial port in Python that works with the cam you need these exact flags, I see you turn of DTR/DSR might be the issue.

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)

Yes Kwabena, we have been communicating privately and your help is much appreciated.

Ibrahim’s reply to suggested that I should post here to share knowledge etc.

Yes, the code is saved properly. Red light confirms saving and IDLE confirms the code has been updated.

I don’t understand why the code posted works for you and not me. Is my python environment not properly configured? Windows has something disabled?

Is there another test I could try? For example a python code that can change the LED colours on the CAM?

Any other suggestion to try?

Um, well, maybe add a while loop around both loops. Your script exits if any process opens the port. It’s quite common for apps to be on a PC that open and close serial ports on plugin for any UART device.