Skip to content

Instantly share code, notes, and snippets.

@CreateRemoteThread
Created June 13, 2017 01:54
Show Gist options
  • Select an option

  • Save CreateRemoteThread/672739b1cb4f2dca40c2ea81c31f4b61 to your computer and use it in GitHub Desktop.

Select an option

Save CreateRemoteThread/672739b1cb4f2dca40c2ea81c31f4b61 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import serial
import getopt
port = None
file = None
baudrate = 115200
optlist, args = getopt.getopt(sys.argv[1:],"p:f:b:",["port=","file=","baudrate="])
for (a,v) in optlist:
if a in ("--port","-p"):
port = int(v)
elif a in ("--file","-f"):
file = v
elif a in ("--baudrate","-b"):
baudrate = int(v)
else:
print " > unknown argument: %s" % a
sys.exit(1)
if file is None and len(args) == 1:
file = args[1]
elif file is None:
print " ! no file supplied"
sys.exit(1)
# if port is None:
# print " ! no port. sending to stdout"
try:
f = open(file,"rb")
data = f.read()
f.close()
except:
print " ! cannot open file %s" % file
sys.exit(0)
i = len(data) / 16
r = len(data) % 16
def hexEncode(string):
out = ""
for c in string:
if port is None:
out += "\\\\x%02x" % ord(c)
else:
out += "\\x%02x" % ord(c)
return out
if port is not None:
ser = serial.Serial("/dev/ttyUSB%d" % port,baudrate,timeout=50)
ser.writelines(["touch %s\r" % file])
print "send touch %s" % file
for rowCount in range(0,i):
dataBlock = data[rowCount * 16:(rowCount + 1) * 16]
dataString = "echo -n -e '%s' >> %s" % (hexEncode(dataBlock),file)
print "send %s" % dataString
if port is not None:
ser.writelines(["%s\r" % dataString])
dataBlock = data[i * 16:i * 16 + r]
dataString = "echo -n -e '%s' >> %s" % (hexEncode(dataBlock),file)
print "send %s" % dataString
if port is not None:
ser.writelines(["%s\r" % dataString])
ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment