In [14]:
# setup django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'ors.settings'
os.chdir('../')
print os.getcwd()
import django
django.setup()


/home/dvoong/opentrv

In [59]:
import sys # for flushing the stdout
import serial # pyserial
import io
with serial.Serial('/dev/ttyUSB0', 4800) as ser: # stream between pi and the rev2
    n_reads = 0
    max_reads = 15
    while ser.isOpen():
        line = ser.readline()
        print 'line({}): {}'.format(n_reads, line)
        ser.write("hello")
#         sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
#         sio.write(unicode("hello\n"))
#         sio.flush() # it is buffering. required to get the data out *now*
#         hello = sio.readline()
#         if line[0] == '>':
#             print "found a >"
#             ser.write("?")
#             ser.flush()
#             print ser.readline()
#         sys.stdout.flush()
#         flushOutput()¶
        n_reads += 1
        if n_reads >= max_reads:
            break


line(0): 

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-59-9cc297439fb9> in <module>()
     11         sio.write(unicode("hello\n"))
     12         sio.flush() # it is buffering. required to get the data out *now*
---> 13         hello = sio.readline()
     14 #         if line[0] == '>':
     15 #             print "found a >"

/home/dvoong/opentrv/ors/virtualenv/local/lib/python2.7/site-packages/serial/serialutil.pyc in readinto(self, b)
    529     def seekable(self): return False
    530     def readinto(self, b):
--> 531         data = self.read(len(b))
    532         n = len(data)
    533         try:

/home/dvoong/opentrv/ors/virtualenv/local/lib/python2.7/site-packages/serial/serialposix.pyc in read(self, size)
    459         while len(read) < size:
    460             try:
--> 461                 ready,_,_ = select.select([self.fd],[],[], self._timeout)
    462                 # If select was used with a timeout, and the timeout occurs, it
    463                 # returns with empty lists -> thus abort read operation.

KeyboardInterrupt: 

In [62]:
ser.close()

In [ ]:
import serial
import io
ser = serial.serial_for_url('/dev/ttyUSB0', 4800)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))

sio.write(unicode("hello\n"))
sio.flush() # it is buffering. required to get the data out *now*
hello = sio.readline()
print hello == unicode("hello\n")
ser.close()

In [ ]: