我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用serial.tools()。
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
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
def comports(): a = serial.tools.list_ports.comports() if len(a) == 0: return [ ] if type(a[0]) == tuple: b = [ ] for aa in a: b.append(aa[0]) return b else: b = [ ] for aa in a: b.append(aa.device) return b # print serial ports and radio types.
def autoscan(): """autoscan will check all of the serial ports to see if they have a matching VID:PID for a MicroPython board. If it matches. """ for port in serial.tools.list_ports.comports(): if is_micropython_usb_device(port): connect_serial(port[0])
def doSparkRc(): f = open (str(firmware_file), "rb") data = f.read() f.close() send_duml_tcp(s, 0x02, 0x1b, 0x40, 0x00, 0x07, bytearray.fromhex(u'00 00 00 00 00 00 00 00 00')) packet_08 = bytearray.fromhex(u'00') packet_08 += struct.pack('<I', len(data)) packet_08 += bytearray.fromhex(u'00 00 00 00 00 00 01 04') send_duml_tcp(s, 0x02, 0x1b, 0x40, 0x00, 0x08, packet_08) for i in range(0, int(len(data)//1000)): packet_09 = bytearray.fromhex(u'00') packet_09 += struct.pack('<I', i) packet_09 += struct.pack('<H', 1000) packet_09 += data[i * 1000:(i + 1) * 1000] send_duml_tcp(s, 0x02, 0x1b, 0x00, 0x00, 0x09, packet_09) i += 1 remain = len(data) % 1000 packet_09 = bytearray.fromhex(u'00') packet_09 += struct.pack('<I', i) packet_09 += struct.pack('<H', remain) packet_09 += data[i * 1000:] send_duml_tcp(s, 0x02, 0x1b, 0x00, 0x00, 0x09, packet_09) filehash = hashlib.md5() filehash.update(data) filehash = filehash.digest() packet_0a = bytearray.fromhex(u'00') packet_0a += filehash send_duml_tcp(s, 0x02, 0x1b, 0x40, 0x00, 0x0a, packet_0a) # from comm_serial2pcap.py # https://github.com/mefistotelis/phantom-firmware-tools/issues/25#issuecomment-306052129