diff options
author | Walter Bender <walter.bender@gmail.com> | 2011-10-05 12:21:23 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2011-10-05 12:21:23 (GMT) |
commit | 0996701c39695e24fbb4992471ba5298f52a4fe6 (patch) | |
tree | 72aa6bffa4eb8adfb56a8ffa3e2426220969153b | |
parent | 24ad35e67021663c431552bfcf969dd85d30cf4b (diff) |
adding new example code for using serial device
-rw-r--r-- | pysamples/serial.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/pysamples/serial.py b/pysamples/serial.py new file mode 100644 index 0000000..153bf90 --- /dev/null +++ b/pysamples/serial.py @@ -0,0 +1,21 @@ +#Copyright (c) 2010-11, Walter Bender, Tony Forster +# +# This Python block writes serial output to a USB port and pushes +# serial input to the heap. +# +# To use this block: +# (1) import this file into a Python Block; +# (2) pass text strings as an argument +# (3) use a Pop Block to retrieve any strings input from serial device. + + +def myblock(tw, x): # x is the string to transmit + import serial # you may need to install this library + + # serial device on USB, 9600 baud + ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) + + ser.write(str(x)) #send string x + st = ser.read(1000) #read up to 1000 bytes + tw.lc.heap.append(st) #append to heap + ser.close() |