from flask import Flask, render_template, request import serial import time app = Flask(__name__) port = '/dev/tty.usbmodem101' baudrate = 9600 ser = serial.Serial(port, baudrate, timeout=1) def send_serial_command(command): try: if not ser.isOpen(): ser.open() ser.write(command.encode('utf-8')) time.sleep(1) response = ser.read(ser.inWaiting()).decode('utf-8') ser.close() return response or "Data transmission failed" except serial.SerialException as e: return f"Serial communication error: {e}" @app.route('/') def index(): return render_template('index.html') @app.route('/send_command', methods=['POST']) def send_command(): command = request.form['command'] response = send_serial_command(command) return response if __name__ == '__main__': app.run(debug=True)