def find_all_ports(self): '''Return a list of port names found. OS independent in nature. Unsupported OS raises OSError exception''' port_list=[] #List to return ports information #Find all ports for windows if sys.platform.startswith('win'): import serial.tools.list_ports_windows as sertoolswin ports = sertoolswin.comports() #Find all ports for Linux based OSes elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): import serial.tools.list_ports_linux as sertoolslin ports = sertoolslin.comports() #Find all ports for MacOSX elif sys.platform.startswith('darwin'): import serial.tools.list_ports_osx as sertoolsosx ports = sertoolsosx.comports() #Raise exception for unsupported OS else: raise OSError("Unsupported Platform/OS") #Fill the return list with information found print('Available ports are:') for port, desc, hwid in sorted(ports): port_list.append(port) print("{}: {} with id: {}".format(port, desc, hwid)) return port_list
def load_devices(self): self.devices = Gtk.ListStore(str, str) lis = ports.comports() for port in lis: self.devices.append([port.device, port.manufacturer]) self.devices.append(["Search", "None"]) self.combo.set_model(self.devices) self.connected_dev = None
def list_serial_ports() -> list: """Lists connected serial ports on UNIX systems. Returns: list: List of connected ports """ return comports()
def get_connection_to_arduino(): from serial.tools.list_ports_linux import comports for p in comports(): if p.description == "Arduino Uno": return serial.Serial(p.device) raise Exception("Arduino Not Found")
def available_serial_ports(self) -> List[str]: available_ports = [] for port in comports(): if not self.is_port_available(port.device): logging.debug(f"Port {port.device} found but not available.") continue available_ports.append(port.device) logging.debug(f"Port {port.device} found and available.") return available_ports
def getDevices(): iterator = sorted(comports()) devices = {} for port in iterator: l = port[1].split(' (') portName = port[0] device = l[0].strip(')') devices.update({device: portName}) return devices
def getConnectionToArduino(): """ Lists serial devices and connects to first Arduino Uno.""" from serial.tools.list_ports_linux import comports for p in comports(): if p.description == "Arduino Uno": return serial.Serial(p.device) raise Exception("Arduino Not Found")
def detect_serial_flight_controllers() -> List[FlightController]: """Check if a Pixhawk1 or any other valid serial flight controller is connected. Returns: List[FlightController]: List with connected serial flight controller. """ serial_path = "/dev/autopilot" for port in comports(include_links=True): if not port.device == serial_path: continue return [ FlightController( name=port.product or port.name, manufacturer=port.manufacturer, platform=Platform.Pixhawk1, path=serial_path, ) ] return []
def click_connect(self, widget): itr = self.combo.get_active_iter() model = self.combo.get_model() if itr == None: print(f'Select Valid device') return selected = model[itr][0] for port in ports.comports(): if (port.device == selected): self.connected_dev = serial.Serial(selected) self.connected = True self.button.set_sensitive(False) self.button2.set_sensitive(True) self.combo.set_sensitive(False) gobject.timeout_add(50, self.update_draw) return print(f'{selected} is not connected device')
def __init__(self, names): self.names = names import serial.tools.list_ports_linux as listPorts ports = listPorts.comports() for p in ports: if p[2] != 'n/a': found = False ser = serial.Serial(p[0]) time.sleep(2) ser.write("name") for i in range(1000): time.sleep(0.01) if ser.inWaiting(): # print "inWaiting" numBytes = ser.inWaiting() inputBytes = ser.read(numBytes) found = True break if found: self.arduinos[inputBytes] = serial.Serial(p[0])
def getPorts(): arduinos = [] import serial.tools.list_ports_linux as listPorts ports = listPorts.comports() for p in ports: if p[2] != 'n/a': found = False ser = serial.Serial(p[0]) time.sleep(2) ser.write("name") for i in range(1000): time.sleep(0.01) if ser.inWaiting(): # print "inWaiting" numBytes = ser.inWaiting() inputBytes = ser.read(numBytes) found = True break if found: arduinos.append(serial.Serial(p[0])) print(arduinos)
def detect_serial_flight_controllers() -> List[FlightController]: """Check if a Pixhawk1 or a Pixhawk4 is connected. Returns: List[FlightController]: List with connected serial flight controller. """ sorted_serial_ports = sorted( comports(), key=lambda port: port.name) # type: ignore unique_serial_devices: List[SysFS] = [] for port in sorted_serial_ports: # usb_device_path property will be the same for two serial connections using the same USB port if port.usb_device_path not in [ device.usb_device_path for device in unique_serial_devices ]: unique_serial_devices.append(port) return [ FlightController( name=port.product or port.name, manufacturer=port.manufacturer, platform=Detector.detect_serial_platform(port), path=port.device, ) for port in unique_serial_devices if Detector.detect_serial_platform(port) is not None ]
def serial_ports(unique_id): ports = list(serial_tools.comports()) for p in ports: if unique_id in p.hwid: return p.device
#!/usr/bin/python import serial import math import os import serial.tools.list_ports_linux as sp import rospy from rospy.exceptions import ROSInterruptException from rospy.topics import Publisher from std_msgs.msg import String list = sp.comports() PORTAPPLY = 'sudo chmod 666 ' + list.device os.system(PORTAPPLY) ser = serial.Serial(list.device, baudrate=115200) def de2ra(de): return de * math.pi / 180 class imuaction: def __init__(self): self.pub = rospy.Publisher('imudata', String, queue_size=30) self.sub = rospy.Subscriber('eulerdata', String, self.euler_caller) def talker(self): while not rospy.is_shutdown(): datas = ser.readline() self.pub.publish(datas) def euler_caller(self, data):
#!/home/manuel/virtualenv/hardware/bin/python from serial import Serial from serial.tools import list_ports_linux import time import subprocess print("Multimedia keyboard handler") arduino = None try: while True: for port in list_ports_linux.comports(): if(port[2] != 'n/a'): print("Checking: ", port[0]) arduino = Serial(port[0], timeout=5) agree = arduino.read() if(agree == b'Z'): arduino.write(b'K') break else: print("No multimedia keyboard found, waiting for retry...") time.sleep(10) continue print("Arduino Keyboard found") break i = j = k = m = n = o = 0 play = True commands = {
devices = glob.glob('/dev/tty*c') return [list_ports_common.ListPortInfo(d) for d in devices] elif plat[:3] == 'aix': # AIX def comports(): """scan for available ports. return a list of device names.""" devices = glob.glob('/dev/tty*') return [list_ports_common.ListPortInfo(d) for d in devices] else: # platform detection has failed... import serial sys.stderr.write("""\ don't know how to enumerate ttys on this system. ! I you know how the serial ports are named send this information to ! the author of this module: sys.platform = %r os.name = %r pySerial version = %s also add the naming scheme of the serial ports and with a bit luck you can get this module running... """ % (sys.platform, os.name, serial.VERSION)) raise ImportError("Sorry: no implementation for your platform ('%s') available" % (os.name,)) # test if __name__ == '__main__': for port, desc, hwid in sorted(comports()): print("%s: %s [%s]" % (port, desc, hwid))
elif plat[:3] == 'aix': # AIX def comports(): """scan for available ports. return a list of device names.""" devices = glob.glob('/dev/tty*') return [list_ports_common.ListPortInfo(d) for d in devices] else: # platform detection has failed... import serial sys.stderr.write("""\ don't know how to enumerate ttys on this system. ! I you know how the serial ports are named send this information to ! the author of this module: sys.platform = %r os.name = %r pySerial version = %s also add the naming scheme of the serial ports and with a bit luck you can get this module running... """ % (sys.platform, os.name, serial.VERSION)) raise ImportError( "Sorry: no implementation for your platform ('%s') available" % (os.name, )) # test if __name__ == '__main__': for port, desc, hwid in sorted(comports()): print("%s: %s [%s]" % (port, desc, hwid))
# this code prints available data came from serial to terminal import serial import serial.tools.list_ports_linux as ports #list of connected devices lis = ports.comports() #connect to device device = serial.Serial(lis[0].device) while True:# the program is always running while device.in_waiting:# check if available data print(device.readline().decode("utf-8"))