1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
import serial import time import sys
def init_send(): ser = serial.Serial('COM4', 115200, timeout=1) return ser def send_values(ser, servo_value, motor_value): time.sleep(0.1) ser.write(bytes([servo_value, motor_value])) while ser.in_waiting: arduino_feedback = ser.readline().decode() print("Arduino:", arduino_feedback) if __name__ == '__main__': ser = init_send() try: while True: for i in range(60, 120, 1): send_values(ser, i, 50) for i in range(120, 60, -1): send_values(ser, i, 50) except KeyboardInterrupt: ser.close() sys.exit()
|