# Executing Main Module Example

**URL:** <https://forums.openmv.io/t/executing-main-module-example/2202>\
**Category:** OpenMV Boards\
**Created:** [February 23, 2021, 12:41am UTC](https://forums.openmv.io/t/executing-main-module-example/2202 "2021-02-23T00:41:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alchemist](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alchemist](https://forums.openmv.io/u/Alchemist)\
**Post date:** [February 23, 2021, 12:41am UTC](https://forums.openmv.io/t/executing-main-module-example/2202/1 "2021-02-23T00:41:28Z")

</div>

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](https://forums.openmv.io/viewtopic.php?t=305).

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

```python
# 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

```python
# 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.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [February 23, 2021, 1:59am UTC](https://forums.openmv.io/t/executing-main-module-example/2202/2 "2021-02-23T01:59:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [February 23, 2021, 2:00am UTC](https://forums.openmv.io/t/executing-main-module-example/2202/3 "2021-02-23T02:00:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [February 23, 2021, 4:11am UTC](https://forums.openmv.io/t/executing-main-module-example/2202/4 "2021-02-23T04:11:04Z")

</div>

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.

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Alchemist](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alchemist](https://forums.openmv.io/u/Alchemist)\
**Post date:** [February 23, 2021, 9:27pm UTC](https://forums.openmv.io/t/executing-main-module-example/2202/5 "2021-02-23T21:27:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [February 23, 2021, 9:58pm UTC](https://forums.openmv.io/t/executing-main-module-example/2202/6 "2021-02-23T21:58:14Z")

</div>

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.
