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

#!/usr/bin/env python
import serial
from time import sleep
import os
ser = serial.Serial(
port='/dev/ttyUSB0',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
#timeout=None) #doesn't work
timeout=0)
print("connected to: " + ser.portstr)
substring ="Shutdown"
# credit to https://eli.thegreenplace.net/2009/07/30/setting-up-python-to-work-with-the-serial-port
# for a working read example
while True:
data = ser.read(9999)
if len(data) > 0:
# print 'Got:', data
print data
#https://www.agnosticdev.com/content/how-find-substring-string-python
#boycott s(tack)exchange
# if substring in data:
# print ("I found the substring")
if substring in data:
os.system("poweroff") #This doesn't work as user, must be root
#reboot also works
#shutdown however, requires arguments
sleep(0.5)
# print 'not blocked'
ser.close()