0
votes

I'm currently trying to set up communication between an Arduino UNO and a PC via a serial/USB connection using Python on a Windows PC.

The goal is to start a video on a laptop when the Arduino tells it to.

I found a guide online which lead me to the code below but Visual Studio 2017 is just showing errors seemingly at random everywhere. A lot of it seemed to come from the imports and VS's IntelliSense messing up (which I believe is fixed now).

So currently when I'm running the program it's getting stuck at the ser = serial.Serial(port, 9600, timerout=0) Line with the error that "name 'port' is not defined" Any idea why that is happening?

I'm unsure if that is the core issue since every time I'm making changes a lot of "unexpected token" and "indent" errors appear for port, newline, print and ser (completely at random)

import serial, os, time, serial.tools.list_ports

    cmd = '"C:\\Users\\Josef\\Desktop\\PYTHON_PlayVideo\\PYTHON_PlayVideo\\Video1.mp4" -f --fullscreen --play-and-exit'

    for p in serial.tools.list_ports.comports():
      sp = str(p)
      #if (sp.find('COM5') != -1):
      if (sp.find('Arduino') != -1):
          flds = sp.split()
          port = flds[0]
          print(port)

    ser = serial.Serial(port, 9600, timeout=0)

    while 1:
      try:
        line = ser.readline()
        if (line):
          print (cmd)
          os.system(cmd)
      except:
        pass
      time.sleep(1)
1
1. Personally, I prefer PyCharm over VS as it makes life simple. 2. The port variable is under an if condition, so the condition has to be true for the variable to initialize. Does the print(port) gives any output?Vignesh SP
i don't think it printed anything, i might try hardcoding which port to use. sp is being set to something along the lines of "COM5 ...." which is the same serial port that shows up in the arduino program. but i don't know the exact syntax the serial function may need so it can compile. do you have any experience with serial? also what does make PYCharm better than VS? (though i'm kinda certain that VS is not the best program for Python by the amount of dumb non logic errors i had to fix^^)nomadDev
So if your print statement is not giving anything, it is expected that serial connection will fail because the variable port does not have anything in it. PyCharm is better because it is made one and only for Python, not need to add anything special and it keeps things simple.Vignesh SP

1 Answers

0
votes

It seems that you don´t find any COM port, so port is never defined. I rework your code and this version works for me (replace USB with your keyword).

import serial, os, time, serial.tools.list_ports

# Get the COM port
Ports = serial.tools.list_ports.comports()

if(len(Ports) == 0):
    print("[ERROR] No COM ports found!")
    exit()

TargetPort = None
for Port in Ports:
    StringPort = str(Port)
    print("[INFO] Port: {}".format(StringPort))
    if("USB" in StringPort):
        TargetPort = StringPort.split(" ")[0]
        print("[INFO] Use {}...".format(TargetPort))

if(TargetPort is None):
    print("[ERROR] No target COM port found!")
    exit()

# Open the COM port
ser = serial.Serial(TargetPort, 9600, timeout = 0)
if(ser.isOpen() == False):
    ser.open()

while(True):
    try:
        line = ser.readline()
        if (line):
            print (cmd)
            #os.system(cmd)
    except:
        pass

    time.sleep(1)