def ComDisconnect(ser,comPort,timeout): printPrefix(7,"SERIAL", 4, "Closing COM{} ...".format(comPort)) try: ser.close() except: printError(2,"Could not close port COM{}".format(comPort)) return 1 printPrefix(7,"SERIAL", 4, "COM{} is closed".format(comPort)) return 0
def UserSendCommand(ser, key): for i, val in enumerate(lCommand): # Is it a known command if key == val[0]: function = eval(val[1]) # eval is "evil" care break else: function = "N/A" if function == "N/A": printError(1, "write 'help' to print the available command or 'quit' to exit") else: function(ser)
def ComConnect(comPort,timeout): printPrefix(7,"SERIAL", 4, "Connection to port COM{} ...".format(comPort)) try: ser = serial.Serial() ser.port = 'COM'+str(comPort) ser.timeout = timeout ser.open() except: try: ser.close() except: pass printError(2,"Could not open port COM{}".format(comPort)) sys.exit(1) return ser
def CommandBright(ser): szInput = input("{:15}".format("> Brightness")) try: listInput = list(map(int, szInput.strip().split(" "))) if len(listInput) != 1: printError(2, "Enter only 1 value for the percentage of brightness") else: tmp = True for i, val in enumerate(listInput): if 0 > val or val > 100: printError(2, "Enter percentage between 0-100") tmp = False break if tmp: sendPacket(ser, createPacket(OPCODE_BRIGTH, listInput)) except ValueError: printError(2, "Enter percentage value 0-100")
def CommandRGB(ser): szInput = input("{:15}".format("> [R][G][B]")) try: listInput = list(map(int, szInput.strip().split(" "))) if len(listInput) != 3: printError(2, "You must enter 3 int value for each component") else: tmp = True for i, val in enumerate(listInput): if 0 > val or val > 255: printError(2, "Enter value between 0-255") tmp = False break if tmp: sendPacket(ser, createPacket(OPCODE_RGB, listInput)) except ValueError: printError(2, "Enter int value 0-255")