I am using the Python-Arduino-Prototyping-API - v2 module to communicate with my Osepp Uno (Arduino Clone). The API does have a close() function which should close the port. I would like to check the status of the port when I open the program. If the port is open I'd like to close it so that the rest of the program can access the port.
Here's my code so far:
from arduino import Arduino
import time
import serial.tools.list_ports
#Find USB Port
def find_port(): #Finds which port the arduino is plugged into
ports = list(serial.tools.list_ports.comports())
for p in ports:
if '0403' in p[2]:
print p
return p[0]
usbport = find_port()
b = Arduino(usbport) #adds port
pin = 0
b.output([])
while (True):
val = b.analogRead(pin)
print val
print usbport
time.sleep(0.05)