Stormer97

Pyserial for Arduino - Python end

May 6th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # should send 'foobar~' to the arduino over serial. The corresponding arduino code will echo all the characters back, # this program will then reconstitute them as characters, then stop listening and print the joined result on
  2. # receiving '#'
  3.  
  4. import serial
  5. import io
  6. from time import sleep
  7.  
  8. ser = serial.Serial("/dev/ttyACM0", 9600, timeout=1)
  9. sleep(2)
  10. print ser.portstr
  11. ser.write("foobar~")
  12. value = None
  13. rawVal = ''
  14. chrValue = ''
  15. received = ''
  16. esc = False
  17. while esc == False:
  18.     rawVal = ser.readline()
  19.     print "raw data received: ", rawVal
  20.     try:   
  21.         value = int(float(rawVal))
  22.         chrValue = chr(value)
  23.     except ValueError:
  24.         chrValue = rawVal
  25.     received = received + chrValue
  26.     valBuf = chrValue.rstrip('\n')
  27.     valBuf = valBuf.rstrip('\r')
  28.     valBuf = valBuf.rstrip(' ')
  29.     if valBuf == '~':
  30.         print "escape character detected"
  31.         esc = True
  32. print "joined data: ", received
  33. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment