technologic.py (2324B)
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, "1s3") # Move motor 1 to position 90 22 send_command(ser, "1m5") 23 send_command(ser, "1sc") 24 send_command(ser, "1sj") 25 time.sleep(0.25) # Wait for 0.5 second 26 send_command(ser, "1si") 27 time.sleep(0.25) 28 send_command(ser, "1s9") # Move motor 2 to position 120 29 send_command(ser, "1se") 30 time.sleep(0.35) 31 send_command(ser, "1sc") 32 time.sleep(0.25) 33 send_command(ser, "1sf") 34 send_command(ser, "1m6") 35 time.sleep(0.25) 36 #send_command(ser, "1s3") # Move motor 1 to position 90 37 time.sleep(0.25) 38 send_command(ser, "1s9") # Move motor 2 to position 120 39 time.sleep(0.5) # Wait for 1 second 40 send_command(ser, "1m1") 41 send_command(ser, "1sj") 42 send_command(ser, "1s1") # Move motor 3 to position 60 43 time.sleep(0.20) # Wait for 1 second 44 send_command(ser, "1sd") 45 send_command(ser, "1si") 46 time.sleep(0.25) 47 send_command(ser, "1se") 48 time.sleep(0.25) 49 send_command(ser, "1sf") 50 time.sleep(0.30) 51 send_command(ser, "1s9") # Move motor 3 to position 60 52 send_command(ser, "1m2") 53 time.sleep(1.10) # Wait for 1 second 54 # Add more commands for your animation sequence here... 55 56 except KeyboardInterrupt: 57 print("Program terminated by user.") 58 59 # Close serial port 60 ser.close() 61 print("Serial port closed.") 62 63 if __name__ == "__main__": 64 main() 65