Created
March 6, 2015 15:08
-
-
Save whostolemyhat/76a0cf250bfc37a5008b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set power on: write('@') | |
# set power off: write('!') | |
# get status: write('~'); readline() | |
# example python script | |
import re | |
import time | |
import serial | |
def doRead(ser,term): | |
matcher = re.compile(term) #gives you the ability to search for anything | |
buff = "" | |
tic = time.clock() | |
buff += ser.read(128) | |
# time in seconds | |
print('Waiting 3sec for response') | |
while ((time.clock() - tic) < 3) and (not matcher.search(buff)): | |
buff += ser.read(128) | |
return buff | |
def find_device(vendor_id, product_id): | |
ports = serial.tools.list_ports.comports() | |
for p in ports: | |
# p is tuple containing 3 strings (port, description, id) | |
# eg ('COM6', 'USB Serial Port (COM6)', 'USB VID:PID=04D8:000A SNR=5') | |
if '{}:{}'.format(vendor_id, product_id) in p[2]: | |
return p[0] | |
else: | |
raise DeviceNotFoundError('Power control device with correct ID was not found') | |
if __name__ == "__main__": | |
port = find_device('04D8', '000A') | |
ser = serial.Serial(port) | |
print('Switching on') | |
ser.write('@') # on | |
print doRead(ser, term='\n') | |
print('Getting status') | |
ser.write('~') | |
print doRead(ser, term='\n') | |
print('Switching off') | |
ser.write('!') | |
print doRead(ser, term='\n') | |
print('Getting status') | |
ser.write('~') | |
print doRead(ser, term='\n') | |
print('Sending #') | |
ser.write('#') | |
print doRead(ser, term='\n') | |
print('Sending $') | |
ser.write('$') | |
print doRead(ser, term='\n') | |
print('Sending %') # Temp | |
ser.write('%') | |
print doRead(ser, term='\n') | |
print('Sending ^') | |
ser.write('^') | |
print doRead(ser, term='\n') | |
print('Sending &') | |
ser.write('&') | |
print doRead(ser, term='\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment