本文整理汇总了Python中serial.tools方法的典型用法代码示例。如果您正苦于以下问题:Python serial.tools方法的具体用法?Python serial.tools怎么用?Python serial.tools使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类serial
的用法示例。
在下文中一共展示了serial.tools方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: is_micropython_usb_device
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def is_micropython_usb_device(port):
"""Checks a USB device to see if it looks like a MicroPython device.
"""
if type(port).__name__ == 'Device':
# Assume its a pyudev.device.Device
if ('ID_BUS' not in port or port['ID_BUS'] != 'usb' or
'SUBSYSTEM' not in port or port['SUBSYSTEM'] != 'tty'):
return False
usb_id = 'usb vid:pid={}:{}'.format(port['ID_VENDOR_ID'], port['ID_MODEL_ID'])
else:
# Assume its a port from serial.tools.list_ports.comports()
usb_id = port[2].lower()
# We don't check the last digit of the PID since there are 3 possible
# values.
if usb_id.startswith('usb vid:pid=f055:980'):
return True
# Check for Teensy VID:PID
if usb_id.startswith('usb vid:pid=16c0:0483'):
return True
return False
示例2: _find_serial_port
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def _find_serial_port(self, vid, pid, name):
"""Find a serial port by VID, PID and text name
:param vid: USB vendor ID to locate
:param pid: USB product ID to locate
:param name: USB device name to find where VID/PID match fails
"""
check_for = "USB VID:PID={vid:04x}:{pid:04x}".format(vid=vid,pid=pid).upper()
ports = serial.tools.list_ports.comports()
for check_port in ports:
if hasattr(serial.tools,'list_ports_common'):
if (check_port.vid, check_port.pid) == (VID, PID):
return check_port.device
continue
if check_for in check_port[2].upper() or name == check_port[1]:
return check_port[0]
return None
示例3: _find_port
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def _find_port(self):
from serial.tools import list_ports
import serial
import os
all_port_tuples = list_ports.comports()
logging.info("listing serial ports")
all_ports = set()
for ap, _, _ in all_port_tuples:
p = os.path.basename(ap)
print(p)
if p.startswith("ttyUSB") or p.startswith("ttyACM"):
all_ports |= {ap}
logging.info("\t%s", str(ap))
if len(all_ports) == 0:
logging.error("No valid port detected!. Possibly, device not plugged/detected.")
raise NoValidPortError()
elif len(all_ports) > 2:
logging.info("Several port detected, using first one: %s", str(all_ports))
return all_ports.pop()
示例4: listports
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def listports():
"""listports will display a list of all of the serial ports.
"""
detected = False
for port in serial.tools.list_ports.comports():
detected = True
if port.vid:
micropythonPort = ''
if is_micropython_usb_device(port):
micropythonPort = ' *'
print('USB Serial Device {:04x}:{:04x}{} found @{}{}\r'.format(
port.vid, port.pid,
extra_info(port), port.device, micropythonPort))
else:
print('Serial Device:', port.device)
if not detected:
print('No serial devices detected')
示例5: detect_ports
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def detect_ports(self):
ports = list(serial.tools.list_ports.comports())
if(len(ports) == 0):
print("No ports detected")
else:
for i in ports:
print(i.device)
示例6: _find_port
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def _find_port(self):
from serial.tools import list_ports
import serial
import os
all_port_tuples = list_ports.comports()
logging.info("listing serial ports")
all_ports = set()
for ap, _, _ in all_port_tuples:
p = os.path.basename(ap)
print(p)
if p.startswith("ttyUSB") or p.startswith("ttyACM"):
all_ports |= {ap}
logging.info("\t%s", str(ap))
if len(all_ports) == 0:
logging.error("No valid port detected!. Possibly, device not plugged/detected.")
raise NoValidPortError()
elif len(all_ports) > 2:
logging.info("Several port detected, using first one: %s", str(all_ports))
return all_ports.pop()
# for ap in list(all_ports):
# logging.info("trying port %s", str(ap))
#
# try:
# #here we use a recursive strategy to find the good port (ap).
# SimpleLynxMotionInterface(ap)
# return ap
# except (WrongSerialPortError, serial.SerialException):
# warn_str = "Tried to use port %s. Failed." % ap
# logging.warning(warn_str)
# pass
示例7: is_micropython_usb_port
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def is_micropython_usb_port(portName):
"""Checks to see if the indicated portname is a MicroPython device
or not.
"""
for port in serial.tools.list_ports.comports():
if port.device == portName:
return is_micropython_usb_device(port)
return False
示例8: autoscan
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def autoscan():
"""autoscan will check all of the serial ports to see if they have
a matching VID:PID for a MicroPython board.
"""
for port in serial.tools.list_ports.comports():
if is_micropython_usb_device(port):
connect_serial(port[0])
示例9: _find_serial_port
# 需要导入模块: import serial [as 别名]
# 或者: from serial import tools [as 别名]
def _find_serial_port(self):
check_for = "USB VID:PID={vid:04x}:{pid:04x}".format(vid=VID,pid=PID).upper()
ports = serial.tools.list_ports.comports()
for check_port in ports:
if hasattr(serial.tools,'list_ports_common'):
if (check_port.vid, check_port.pid) == (VID, PID):
return check_port.device
continue
if check_for in check_port[2].upper():
return check_port[0]
raise AttributeError(LANG_COULD_NOT_FIND)