Mihao

RaspberryPi Serial test

Feb 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import time
  2. import serial
  3. print "Starting program"
  4. ser = serial.Serial('/dev/ttyAMA0', baudrate=9600,
  5. parity=serial.PARITY_NONE,
  6. stopbits=serial.STOPBITS_ONE,
  7. bytesize=serial.EIGHTBITS
  8. )
  9. time.sleep(1)
  10. try:
  11. ser.write('Hello World\r\n')
  12. ser.write('Serial Communication Using Raspberry Pi\r\n')
  13. ser.write('By: Embedded Laboratory\r\n')
  14. print 'Data Echo Mode Enabled'
  15. while True:
  16. if ser.inWaiting() > 0:
  17. data = ser.read()
  18. print data
  19.  
  20. except KeyboardInterrupt:
  21. print "Exiting Program"
  22.  
  23. except:
  24. print "Error Occurs, Exiting Program"
  25.  
  26. finally:
  27. ser.close()
  28. pass
Add Comment
Please, Sign In to add comment