Skip to content

Instantly share code, notes, and snippets.

@TylerOderkirk
Last active August 29, 2015 14:23
Show Gist options
  • Save TylerOderkirk/c8510292cc86648074b5 to your computer and use it in GitHub Desktop.
Save TylerOderkirk/c8510292cc86648074b5 to your computer and use it in GitHub Desktop.
[dirt simple comms] read bytes from stdin and write them to a named pipe. read bytes from another named pipe and write them to stdout.
#!/usr/bin/env python
import thread, sys, time
if sys.argv[1] == "other":
fifo_w = open('/tmp/south', 'wb')
fifo_r = open('/tmp/north', 'rb')
log = open('/tmp/serial_log_southbound', 'wb')
else:
fifo_r = open('/tmp/south', 'rb')
fifo_w = open('/tmp/north', 'wb')
log = open('/tmp/serial_log_northbound', 'wb')
def read_from_stdin():
f = sys.stdin
while True:
b = f.read(1)
if len(b) == 1:
fifo_w.write(b)
fifo_w.flush()
log.write(b)
log.flush()
def write_to_stdout():
while True:
b = fifo_r.read(1)
if len(b) == 1:
sys.stdout.write(b) # instead of print() to avoid printing CRLF
sys.stdout.flush()
thread.start_new_thread(read_from_stdin, ())
thread.start_new_thread(write_to_stdout, ())
time.sleep(120) # TODO: replace w/ threading module and join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment