diff --git a/rshell/main.py b/rshell/main.py index 7a810d9..6678463 100755 --- a/rshell/main.py +++ b/rshell/main.py @@ -506,6 +506,9 @@ def process_pattern(fn): def resolve_path(path): """Resolves path and converts it into an absolute path.""" + # if path containes ':' it is a remote path, do not resolve + if ':' in path: + return path if path[0] == '~': # ~ or ~user path = os.path.expanduser(path) @@ -547,6 +550,19 @@ def get_dev_and_path(filename): If the file is not associated with the remote device, then the dev portion of the returned tuple will be None. """ + + # Check if filename can be split by ':' into a device name and a path. + # If so, then we assume that the file is associated with the named device. + # if device name is empty then use the default device. + if ':' in filename: + dev_name, filename = filename.split(':', 1) + if dev_name == '': + return (DEFAULT_DEV, filename) + with DEV_LOCK: + for dev in DEVS: + if dev.name == dev_name: + return (dev, filename) + if DEFAULT_DEV: if DEFAULT_DEV.is_root_path(filename): return (DEFAULT_DEV, filename) diff --git a/rshell/pyboard.py b/rshell/pyboard.py index 3e35c0b..c2ebc59 100644 --- a/rshell/pyboard.py +++ b/rshell/pyboard.py @@ -124,38 +124,69 @@ def __init__(self, device, baudrate=115200, user='micro', password='python', wai 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) + elif device.startswith('rfc2217://'): + self.openRfc2217Serial(device, baudrate, wait, rts, dtr) + else: + self.openSerial(device, baudrate, wait, rts, dtr) + + def openSerial(self, device, baudrate=115200, wait=0, rts='', dtr=''): + import serial + delayed = False + if serial.VERSION == '3.0': + self.serial = serial.Serial(baudrate=baudrate, inter_byte_timeout=1) + else: + self.serial = serial.Serial(baudrate=baudrate, interCharTimeout=1) + if rts != '': + self.serial.rts = parse_bool(rts) + if dtr != '': + self.serial.dtr = parse_bool(dtr) + self.serial.port = device + for attempt in range(wait + 1): + try: + # Assigning the port attribute will attempt to open the port + self.serial.open() + 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 openRfc2217Serial(self, device, baudrate=115200, wait=0, rts='', dtr=''): + delayed = False + for attempt in range(wait + 1): + try: + # Try to open rfc2217 serial port + import serial + self.serial = serial.serial_for_url(device, baudrate=baudrate, rtscts=parse_bool(rts), dsrdtr=parse_bool(dtr)) + break + except (BrokenPipeError, OSError, IOError): # Py2 and Py3 have different errors + if wait == 0: + continue + if attempt == 0: + sys.stdout.write('Waiting {} seconds for rfc2217 pipe to be open '.format(wait)) + delayed = True + time.sleep(1) + sys.stdout.write('.') + sys.stdout.flush() else: - import serial - delayed = False - if serial.VERSION == '3.0': - self.serial = serial.Serial(baudrate=baudrate, inter_byte_timeout=1) - else: - self.serial = serial.Serial(baudrate=baudrate, interCharTimeout=1) - if rts != '': - self.serial.rts = parse_bool(rts) - if dtr != '': - self.serial.dtr = parse_bool(dtr) - self.serial.port = device - for attempt in range(wait + 1): - try: - # Assigning the port attribute will attempt to open the port - self.serial.open() - 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('') + raise PyboardError('failed to access ' + device) + if delayed: + print('') + + def close(self): self.serial.close()