Skip to content

Instantly share code, notes, and snippets.

@jkent
Created June 9, 2013 10:05
Show Gist options
  • Save jkent/5743024 to your computer and use it in GitHub Desktop.
Save jkent/5743024 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf8 -*-
# vim: set ts=4 et
from serial import Serial
import sys
from threading import Thread, Condition
class Reader(Thread):
def __init__(self, ser):
Thread.__init__(self)
self.ser = ser
self.running = True
self.next = Condition()
def run(self):
while True:
s = raw_input('> ')
line = 'at+excxxx=((' + s + '; echo _%_DONE_%_) > /dev/ttyS1)& #_%_COMMAND_%_\r\n'
self.ser.write(line)
self.next.acquire()
self.next.wait()
self.next.release()
def stop(self):
self._Thread__stop()
if __name__ == '__main__':
ser = Serial(sys.argv[1], 115200)
reader = Reader(ser)
reader.start()
try:
buf = ''
while True:
buf += ser.read()
if '\n' in buf:
i = buf.index('\n')
line, buf = buf[:i], buf[i+1:]
if '_%_COMMAND_%_' in line:
i = line.index('_%_COMMAND_%_')
line = line[i+13:]
if line.endswith('\r'):
pass
elif line == '_%_DONE_%_':
reader.next.acquire()
reader.next.notify()
reader.next.release()
else:
print line
except KeyboardInterrupt:
reader.stop()
except:
reader.stop()
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment