Skip to content

Instantly share code, notes, and snippets.

@bassdread
Created October 7, 2015 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassdread/d56af71fe6e80d3f07e1 to your computer and use it in GitHub Desktop.
Save bassdread/d56af71fe6e80d3f07e1 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import serial
import json
def main(device, load_json=False):
device_path = "/dev/{0}".format(device)
print "Opening {}".format(device_path)
serial_connection = serial.Serial(device_path, 9600, timeout=1)
print "Starting to read from serial... ctrl-c to stop"
while (True):
json_input = serial_connection.readline()
if json_input:
try:
print json_input
if load_json:
print "JSON Version: ",
print json.loads(json_input)
except Exception as e:
print e.message
if __name__ == "__main__":
device = "ttyUSB0"
load_json = False
if len(sys.argv) == 2:
device = sys.argv[1]
if len(sys.argv) == 3:
load_json = sys.argv[2]
main(device, load_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment