You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
837 B

5 years ago
  1. import serial
  2. from time import sleep
  3. import os
  4. ser = serial.Serial(
  5. port='/dev/ttyUSB0',\
  6. baudrate=9600,\
  7. parity=serial.PARITY_NONE,\
  8. stopbits=serial.STOPBITS_ONE,\
  9. bytesize=serial.EIGHTBITS,\
  10. #timeout=None) #doesn't work
  11. timeout=0)
  12. print("connected to: " + ser.portstr)
  13. substring ="Shutdown"
  14. # credit to https://eli.thegreenplace.net/2009/07/30/setting-up-python-to-work-with-the-serial-port
  15. # for a working read example
  16. while True:
  17. data = ser.read(9999)
  18. if len(data) > 0:
  19. # print 'Got:', data
  20. print data
  21. #https://www.agnosticdev.com/content/how-find-substring-string-python
  22. #boycott s(tack)exchange
  23. # if substring in data:
  24. # print ("I found the substring")
  25. if substring in data:
  26. os.system("reboot") #This doesn't work as user
  27. sleep(0.5)
  28. # print 'not blocked'
  29. ser.close()