So i've been watching your video, and i notice there might be a way of reducing the need for the user to manually get things up and going.
For example, we could create a small script, and run it on a cron schedule that could find, and test connection to the correct arduino, then update the correct com port information for that arduino to arduino-connector script.
Example:
#!/usr/bin/env python3
import serial
import serial.tools.list_ports
import time
import re
# ==========================================
# CONFIGURATION SECTION
# ==========================================
# Just put the full path and filename here
FULL_FILE_PATH = "/usr/bin/arduino-connector"
TARGET_MSG = "E0:0"
BAUDRATE = 115200
# ==========================================
def update_config_file(file_path, new_port):
pattern = r"(connection\s*=\s*['\"])([^'\"]+)(['\"])"
replacement = rf"\1{new_port}\3"
try:
with open(file_path, 'r') as f:
content = f.read()
if re.search(pattern, content):
new_content = re.sub(pattern, replacement, content)
with open(file_path, 'w') as f:
f.write(new_content)
print(f"Successfully updated: {file_path}")
else:
print(f"Error: Could not find 'connection =' in {file_path}")
except FileNotFoundError:
print(f"Error: File not found at {file_path}")
def connect_to_correct_arduino():
ports = list(serial.tools.list_ports.comports())
print(f"Scanning {len(ports)} ports for signal '{TARGET_MSG}'...")
for p in ports:
print(f"Testing {p.device}...")
try:
# Short timeout to keep the scan moving
ser = serial.Serial(p.device, BAUDRATE, timeout=3)
# Wait for Arduino reboot
time.sleep(2)
ser.reset_input_buffer()
incoming = ser.readline().decode('utf-8').strip()
if incoming == TARGET_MSG:
print(f"Verified! Match found on {p.device}")
# Call the update function using our config variables
update_config_file(FULL_FILE_PATH, p.device)
return ser
else:
print(f" -> Wrong device (received: '{incoming}')")
ser.close()
except Exception as e:
print(f" -> Connection error on {p.device}: {e}")
continue
return None
if __name__ == "__main__":
arduino = connect_to_correct_arduino()
if arduino:
print("\nSetup Complete. Arduino-Connector has been updated and is ready.")
else:
print("\nFailed to Arduino-Connector. No changes were made.")
Since this runs at startup, or on a cron timer, the correct com port should be nearly always up to date, without any user intervention, and if the script cannot connect to the device because it's already in use, nothing needs to change.
Taking that assumptive approach, could one also simply run the arduino-connect script as part of the startup process for LinuxCNC? If so, this would eliminate the user having to adjust these variables manually, as well as remove the need for the user to start the process from within LinuxCNC as well.
On the other side of the coin, rather than running an entire other script, you could incorporate something similar into your arduino-connector.py, and when it's run at startup, it could simply "find" the correct port without the user having to do anything at all, then you can simply run arduino-connector when LinuxCNC starts up. .
I believe you can add a line to your INI file to launch the script for you
[APPLICATIONS]
APP = python3 /usr/bin/arduino-connect
So i've been watching your video, and i notice there might be a way of reducing the need for the user to manually get things up and going.
For example, we could create a small script, and run it on a cron schedule that could find, and test connection to the correct arduino, then update the correct com port information for that arduino to arduino-connector script.
Example:
Since this runs at startup, or on a cron timer, the correct com port should be nearly always up to date, without any user intervention, and if the script cannot connect to the device because it's already in use, nothing needs to change.
Taking that assumptive approach, could one also simply run the arduino-connect script as part of the startup process for LinuxCNC? If so, this would eliminate the user having to adjust these variables manually, as well as remove the need for the user to start the process from within LinuxCNC as well.
On the other side of the coin, rather than running an entire other script, you could incorporate something similar into your arduino-connector.py, and when it's run at startup, it could simply "find" the correct port without the user having to do anything at all, then you can simply run arduino-connector when LinuxCNC starts up. .
I believe you can add a line to your INI file to launch the script for you