Python-Arduino-Prototyping-API v2- Closing Serial Port -
i using python-arduino-prototyping-api - v2 module communicate osepp uno (arduino clone). api have close() function should close port. check status of port when open program. if port open i'd close rest of program can access port.
here's code far:
from arduino import arduino import time import serial.tools.list_ports #find usb port def find_port(): #finds port arduino plugged ports = list(serial.tools.list_ports.comports()) 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)
i figured out. used pyserial check see if port open , if so, closed it. ensures port available rest of program.
from arduino import arduino import time import serial.tools.list_ports import serial #find usb port def find_port(): #finds port arduino plugged ports = list(serial.tools.list_ports.comports()) p in ports: if '0403' in p[2]: print p return p[0] usbport = find_port() #calls function arduinos usb port def closeport(): #closes port if open ser = serial.serial(usbport) if ser.isopen() == true: ser.close() closeport() #make sure port available b = arduino(usbport) #opens arduino correct port pin = 0 #assigns analog out, pin 0 b.output([]) while (true): val = b.analogread(pin) print val print usbport time.sleep(0.05)
i hope helps else.
Comments
Post a Comment