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.

38 lines
933 B

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