buyit.py (2874B)
1 import serial 2 import time 3 4 def send_command(ser, command): 5 ser.write(command.encode()) 6 ser.write(b'\n') # Assuming commands are terminated with a newline character 7 8 def main(): 9 # Open serial port 10 ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1) 11 12 if not ser.is_open: 13 print("Failed to open serial port.") 14 return 15 16 print("Serial port opened successfully.") 17 18 try: 19 while True: 20 # Initiate animation sequence 21 send_command(ser, "1sc") # Move motor 1 to position 90 22 send_command(ser, "1si") 23 time.sleep(0.25) 24 send_command(ser, "@2m3") 25 send_command(ser, "@2s9") 26 send_command(ser, "1sj") 27 send_command(ser, "1s3") 28 time.sleep(0.25) 29 send_command(ser, "@2s2") 30 send_command(ser, "1m5") 31 send_command(ser, "1si") 32 time.sleep(0.25) 33 send_command(ser, "@2s3") 34 send_command(ser, "1sj") 35 send_command(ser, "1se") 36 send_command(ser, "1s4") 37 send_command(ser, "1s7") 38 time.sleep(0.25) 39 send_command(ser, "@4m3b") 40 send_command(ser, "@2s6") 41 send_command(ser, "1si") 42 time.sleep(0.25) 43 send_command(ser, "@4m1b") 44 send_command(ser, "@2s7") 45 send_command(ser, "1sj") 46 time.sleep(0.25) 47 send_command(ser, "@4m3a") 48 send_command(ser, "1si") 49 send_command(ser, "1s8") 50 send_command(ser, "1sf") 51 time.sleep(0.25) 52 send_command(ser, "@4m1a") 53 send_command(ser, "@2s8") 54 send_command(ser, "1sj") 55 time.sleep(0.25) 56 send_command(ser, "@2s5") 57 send_command(ser, "1si") 58 send_command(ser, "1se") 59 time.sleep(0.25) 60 send_command(ser, "@2s4") 61 send_command(ser, "1m6") 62 time.sleep(0.25) 63 send_command(ser, "@2s1") 64 send_command(ser, "1sj") 65 time.sleep(0.25) 66 send_command(ser, "@2sa") 67 send_command(ser, "1s9") 68 time.sleep(0.25) 69 send_command(ser, "@2m4") 70 time.sleep(0.75) 71 72 except KeyboardInterrupt: 73 print("Program terminated by user.") 74 75 # Close serial port 76 ser.close() 77 print("Serial port closed.") 78 79 if __name__ == "__main__": 80 main() 81