Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions serio.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ def put(self, source, destination):
dpart = ''

while j < self.BYTES_PER_LINE and i < data_size:
dpart += '\\x%.2X' % int(ord(data[i]))
dpart += '\\x%.2X' % (data[i])
j+=1
i+=1

self.write('\necho -ne "%s" >> %s\n' % (dpart, destination))

# Show upload status
if not self.quiet:
print "%d / %d" % (i, data_size)
print("%d / %d" % (i, data_size))

return i

def write(self, data):
self.s.write(data)
self.s.write(data.encode())
if data.endswith('\n'):
# Have to give the target system time for disk/flash I/O
time.sleep(self.IO_TIME)
Expand All @@ -64,29 +64,31 @@ def __init__(self, host, port, login=None, passwd=None, time=None, quiet=None):
self.s = telnetlib.Telnet(host, port, timeout=10)
# We're not interested in matching input, just interested
# in consuming it, until it stops
DONT_MATCH = "\xff\xff\xff"
DONT_MATCH = b"\xff\xff\xff"
if login:
print(self.s.read_until(DONT_MATCH, 0.5))
self.s.write(login + "\n")
login += "\n"
self.s.write(login.encode())
print(self.s.read_until(DONT_MATCH, 0.5))
self.s.write(passwd + "\n")
passwd += "\n"
self.s.write(passwd.encode())
# Skip shell banner
print(self.s.read_until(DONT_MATCH, self.IO_TIME))


def usage():
print '\nUsage: %s [OPTIONS]\n' % sys.argv[0]
print '\t-s, --source=<local file> Path to local file'
print '\t-d, --destination=<remote file> Path to remote file'
print '\t --telnet=<host> Upload via telnet instead of serial'
print '\t-p, --port=<port> Serial port to use [/dev/ttyUSB0] or telnet port [23]'
print '\t-b, --baudrate=<baud> Serial port baud rate [115200]'
print '\t-t, --time=<seconds> Time to wait between echo commands [0.1]'
print '\t --login=<username> Login name for telnet'
print '\t --pass=<passwd> Password for telnet'
print '\t-q, --quiet Supress status messages'
print '\t-h, --help Show help'
print ''
print('\nUsage: %s [OPTIONS]\n' % sys.argv[0])
print('\t-s, --source=<local file> Path to local file')
print('\t-d, --destination=<remote file> Path to remote file')
print('\t --telnet=<host> Upload via telnet instead of serial')
print('\t-p, --port=<port> Serial port to use [/dev/ttyUSB0] or telnet port [23]')
print('\t-b, --baudrate=<baud> Serial port baud rate [115200]')
print('\t-t, --time=<seconds> Time to wait between echo commands [0.1]')
print('\t --login=<username> Login name for telnet')
print('\t --pass=<passwd> Password for telnet')
print('\t-q, --quiet Supress status messages')
print('\t-h, --help Show help')
print('')
sys.exit(1)

def main():
Expand All @@ -104,8 +106,8 @@ def main():
try:
opts, args = GetOpt(sys.argv[1:],'p:b:s:d:t:qh', ['port=', 'baudrate=',
'source=', 'destination=', 'time=', 'quiet', 'help', 'telnet=', 'login=', 'pass='])
except GetoptError, e:
print 'Usage error:', e
except GetoptError as e:
print('Usage error:', e)
usage()

for opt, arg in opts:
Expand All @@ -131,7 +133,7 @@ def main():
usage()

if not source or not destination:
print 'Usage error: must specify -s and -d options'
print('Usage error: must specify -s and -d options')
usage()

try:
Expand All @@ -151,10 +153,9 @@ def main():
size = sftp.put(source, destination)
sftp.close()

print 'Uploaded %d bytes from %s to %s' % (size, source, destination)
except Exception, e:
print "ERROR:", e

print('Uploaded %d bytes from %s to %s' % (size, source, destination))
except Exception as e:
print("ERROR:", e)

if __name__ == '__main__':
main()