req = input(">>>") if "exit()".upper() in req: return req = req.encode() device.write(req + b'\r\n') output = b'' time.sleep(0.5) # set at half second, if device doesnt res increment while device.inWaiting() > 0: output = device.read(1) if output != '': print(output.decode(), end='') def transmitstr(device: Serial, msg: str): return device.write(msg.encode()) def listen(device): while 1: output = device.inWaiting() print(device.read(output).decode()) if __name__ == "__main__": device = Serial("COM11") device.baud = 115200 terminal(device) #device.baud = 3000000 #print(device.write(b'0'))
except: print 'read error!' return False #--------------------------------------------------- #--------------------------------------------------- #MAIN #--------------------------------------------------- #serial communication parameters serialRobot = Serial() #new object #Linux #serialRobot.port = '/dev/ttyUSB0' #Port number or ID #Windows serialRobot.port = 'COM3' #Port number or ID serialRobot.baud = 9600 #baud rate #--------------------------------------------------- #opens the serial port serialRobot.open() #--------------------------------------------------- #decides which action should be sent to the robot action = input('0: set position, 1: get position, 4: LED on, 5: LED off | ') #---------------------------------------------------------------------------------- #ACTION 1: Set position of a servo if (action == AX_SETPOSITION): #inputs servoId = input('which servo: ') #servo ID initialPosition = input( 'insert an initial position: ') #initial position (P0) steps = input( 'insert how many steps: ') #how many steps from P0 should be taken