我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用serial.VERSION。
def number_to_device(self, port_number): sys.stderr.write("""\ don't know how to number ttys on this system. ! Use an explicit path (eg /dev/ttyS1) or send this information to ! the author of this module: sys.platform = %r os.name = %r serialposix.py version = %s also add the device name of the serial port and where the counting starts for the first serial port. e.g. 'first serial port: /dev/ttyS0' and with a bit luck you can get this module running... """ % (sys.platform, os.name, serial.VERSION)) raise NotImplementedError('no number-to-device mapping defined on this platform')
def __init__(self, device, baudrate=115200, user='micro', password='python', wait=0): if device and device[0].isdigit() and device[-1].isdigit() and device.count('.') == 3: # device looks like an IP address self.serial = TelnetToSerial(device, user, password, read_timeout=10) else: import serial delayed = False for attempt in range(wait + 1): try: if serial.VERSION == '3.0': self.serial = serial.Serial(device, baudrate=baudrate, inter_byte_timeout=1) else: self.serial = serial.Serial(device, baudrate=baudrate, interCharTimeout=1) break except (OSError, IOError): # Py2 and Py3 have different errors if wait == 0: continue if attempt == 0: sys.stdout.write('Waiting {} seconds for pyboard '.format(wait)) delayed = True time.sleep(1) sys.stdout.write('.') sys.stdout.flush() else: if delayed: print('') raise PyboardError('failed to access ' + device) if delayed: print('')
def run_router_app(): """ Do the probe/check of """ # as of Mat-2016/FW=6.1, PySerial is version 2.6 (2.6-pre1) cs.CSClient().log(APP_NAME, "serial.VERSION = {}.".format(serial.VERSION)) # probe_physical = True, set False to NOT probe real physical serial ports. # On models without physical ports, this setting is ignored. probe_physical = True # probe_usb = True, set False to NOT probe for USB serial ports. probe_usb = True # write_name = True, set False to NOT send out the port name, which is # sent to help you identify between multiple ports. write_name = False # probe_directory(app_base, "/dev") port_list = [] # confirm we are running on an 1100/1150 or 900/950, result should be "IBR1100LPE" result = cs.CSClient().get("status/product_info/product_name").get('data') if "IBR1100" in result or "IBR1150" in result or "IBR900" in result or "IBR950" in result: name = "/dev/ttyS1" cs.CSClient().log(APP_NAME, "Product Model {} has 1 builtin port:{}".format(result, name)) port_list.append(name) elif "IBR300" in result or "IBR350" in result: cs.CSClient().log(APP_NAME, "Product Model {} has no serial support".format(result)) return -1 if probe_physical: # fixed ports - 1100 only? if not IGNORE_TTYS0: # only check S0 if 'requested' to, else ignore name = "/dev/ttyS0" if name not in port_list: port_list.append(name) for port in PORT_LIST_PHYSICAL: name = "/dev/ttyS%d" % port if name not in port_list: port_list.append(name) if probe_usb: # try first 5 USB for port in PORT_LIST_USB: name = "/dev/ttyUSB%d" % port if name not in port_list: port_list.append(name) # cycle through and probe the desired ports for name in port_list: probe_serial(name, write_name) return 0
def openSerial(self): #Set up the relationship between what the user enters and what the API calls for bytedic = {'5':serial.FIVEBITS, '6':serial.SIXBITS, '7':serial.SEVENBITS, '8':serial.EIGHTBITS} bytesize = bytedic[str(self.root.variables['databits'])] paritydict = {'None':serial.PARITY_NONE, 'Even':serial.PARITY_EVEN, 'Odd' :serial.PARITY_ODD, 'Mark':serial.PARITY_MARK, 'Space':serial.PARITY_SPACE} parity=paritydict[self.root.variables['parity']] stopbitsdict = {'1':serial.STOPBITS_ONE, '2':serial.STOPBITS_TWO} stopbits = stopbitsdict[str(self.root.variables['stopbits'])] #Open the serial port given the settings, store under the root if os.name == 'nt': port = self.root.variables['COMport'][0:5].strip() self.root.ser = serial.Serial(\ port=port,\ baudrate=str(self.root.variables['baud']),\ bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=0.5) else: first_space = self.root.variables['COMport'].index(' ') port = self.root.variables['COMport'][0:first_space].strip() # Parameters necessary due to https://github.com/pyserial/pyserial/issues/59 self.root.ser = serial.Serial(\ port=port,\ baudrate=str(self.root.variables['baud']),\ bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=0.5, rtscts=True, dsrdtr=True) io.DEFAULT_BUFFER_SIZE = 5000 #Purge the buffer of any previous data if float(serial.VERSION[0:3]) < 3: #If we're executing with pySerial 2.x serial.Serial.flushInput(self.root.ser) serial.Serial.flushOutput(self.root.ser) else: #Otherwise we're using pySerial 3.x serial.Serial.reset_input_buffer(self.root.ser) serial.Serial.reset_output_buffer(self.root.ser)
def do_repl(self, args): """repl Enter Micropython REPL. """ import serial ver = serial.VERSION.split(".") if int(ver[0]) < 2 or (int(ver[0]) == 2 and int(ver[1]) < 7): self.__error("REPL needs PySerial version >= 2.7, found %s" % serial.VERSION) return if self.__is_open(): if self.repl is None: from mp.term import Term self.repl = Term(self.fe.con) if platform.system() == "Windows": self.repl.exit_character = chr(0x11) else: self.repl.exit_character = chr(0x1d) self.repl.raw = True self.repl.set_rx_encoding('UTF-8') self.repl.set_tx_encoding('UTF-8') else: self.repl.serial = self.fe.con self.fe.teardown() self.repl.start() if self.repl.exit_character == chr(0x11): print("\n*** Exit REPL with Ctrl+Q ***") else: print("\n*** Exit REPL with Ctrl+] ***") try: self.repl.join(True) except Exception: pass self.repl.console.cleanup() self.fe.setup() print("")