Kaynağa Gözat

persist serial connection across session

Tuan Nguyen 1 yıl önce
ebeveyn
işleme
a34918a07d
3 değiştirilmiş dosya ile 33 ekleme ve 3 silme
  1. 14 2
      app.py
  2. 1 1
      arduino_code/arduino_code.ino
  3. 18 0
      templates/index.html

+ 14 - 2
app.py

@@ -15,6 +15,7 @@ os.makedirs(THETA_RHO_DIR, exist_ok=True)
 
 # Serial connection (default None, will be set by user)
 ser = None
+ser_port = None  # Global variable to store the serial port name
 stop_requested = False
 
 
@@ -25,18 +26,20 @@ def list_serial_ports():
 
 def connect_to_serial(port, baudrate=115200):
     """Connect to the specified serial port."""
-    global ser
+    global ser, ser_port
     if ser and ser.is_open:
         ser.close()
     ser = serial.Serial(port, baudrate)
+    ser_port = port  # Store the connected port globally
     time.sleep(2)  # Allow time for the connection to establish
 
 def disconnect_serial():
     """Disconnect the current serial connection."""
-    global ser
+    global ser, ser_port
     if ser and ser.is_open:
         ser.close()
         ser = None
+    ser_port = None  # Reset the port name
 
 def restart_serial(port, baudrate=115200):
     """Restart the serial connection."""
@@ -188,6 +191,7 @@ def restart():
 @app.route('/list_theta_rho_files', methods=['GET'])
 def list_theta_rho_files():
     files = os.listdir(THETA_RHO_DIR)
+    files = sorted([file for file in files if files not in ['clear_from_in.thr', 'clear_from_out.thr']])
     return jsonify(sorted(files))
 
 @app.route('/upload_theta_rho', methods=['POST'])
@@ -358,6 +362,14 @@ def download_file(filename):
     """Download a file from the theta-rho directory."""
     return send_from_directory(THETA_RHO_DIR, filename)
 
+@app.route('/serial_status', methods=['GET'])
+def serial_status():
+    global ser, ser_port
+    return jsonify({
+        'connected': ser.is_open if ser else False,
+        'port': ser_port  # Include the port name
+    })
+
 if __name__ == '__main__':
     # Start the thread for reading Arduino responses
     threading.Thread(target=read_serial_responses, daemon=True).start()

+ 1 - 1
arduino_code/arduino_code.ino

@@ -163,7 +163,7 @@ void loop()
                 interpolatePath(
                     startTheta, startRho,
                     buffer[i][0], buffer[i][1],
-                    0.001 // Step size
+                    0.0009 // Step size
                 );
               }
             // Update the starting point for the next segment

+ 18 - 0
templates/index.html

@@ -459,6 +459,24 @@
             logMessage('Pattern preview rendered at full screen.');
         }
 
+        async function checkSerialStatus() {
+            const response = await fetch('/serial_status');
+            const status = await response.json();
+            const statusElement = document.getElementById('serial_status');
+            
+            if (status.connected) {
+                const port = status.port || 'Unknown'; // Fallback if port is undefined
+                statusElement.textContent = `Status: Connected to ${port}`;
+                logMessage(`Reconnected to serial port: ${port}`);
+            } else {
+                statusElement.textContent = 'Status: Not connected';
+                logMessage('No active serial connection.');
+            }
+        }
+        
+        // Call this function on page load
+        checkSerialStatus();
+
         // Initial load of serial ports and Theta-Rho files
         loadSerialPorts();
         loadThetaRhoFiles();