33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
import serial.tools.list_ports
|
|
import sys, getopt
|
|
import time
|
|
|
|
|
|
def main(argv):
|
|
cmd = 'GETSTATE'
|
|
print("USBCaseControl starting up...")
|
|
try:
|
|
opts, args = getopt.getopt(argv,"hc:",["cmd="])
|
|
except getopt.GetoptError as err:
|
|
print(err)
|
|
sys.exit(2)
|
|
for opt, arg in opts:
|
|
if opt == '-h':
|
|
print("USBCaseControl.py --cmd <command>")
|
|
sys.exit()
|
|
elif opt in ("-c", "--cmd"):
|
|
cmd = arg
|
|
ports = serial.tools.list_ports.comports()
|
|
for port, desc, hwid in sorted(ports):
|
|
if "Arduino Leonardo" in desc:
|
|
print("{}: {} [{}]".format(port, desc, hwid))
|
|
finalport=port
|
|
ser = serial.Serial(finalport, 9600, timeout=0,parity=serial.PARITY_EVEN, rtscts=1)
|
|
print("Calling command: {} on Port {}".format(cmd,port))
|
|
ser.write(cmd.encode('utf_8'))
|
|
time.sleep(2)
|
|
out = ser.read(100)
|
|
print("Command return: {}".format(out.decode(encoding="utf_8", errors="ignore")))
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv[1:]) |