`# Set up serial connection
arduino_port = 'COM5'
baud_rate = 250000 # Baud rate must match the rate defined in Arduino code
# Global stop flag to manage threading
global stop_flag
stop_flag = False
# Carbo Offset for better visualization
CARBO_OFFSET = 10
CARB_DROP = -200 # Set the threshold for significant carbo drop
def get_unique_filename(folder_path, username, testname):
base_filename = f"TPC_output_{username}_{testname}"
filename = f"{folder_path}/{base_filename}.csv"
counter = 1
while os.path.exists(filename):
filename = f"{folder_path}/{base_filename}_{counter}.csv"
counter += 1
return filename
def plot_csv_data(filename, log_number, line_styles):
# Read the CSV data into a DataFrame
df = pd.read_csv(filename,
names=["millis", "psi", "temperature C", "accumulated pressure", "froth", "accumulated froth",
"carbo", "accumulated carbo", "Cadc", "froth accumulation paused?"], skip_blank_lines=True)
# Find indices of "LOGGING STARTED" entries to separate sessions
log_starts = df[df.apply(lambda x: x.str.contains("--------------- LOGGING STARTED ----------------------").any(),
axis=1)].index
# Iterate over each logging session and generate a plot
for i in range(len(log_starts)):
start_idx = log_starts[i] + 1
end_idx = log_starts[i + 1] if i + 1 < len(log_starts) else len(df)
session_df = df.iloc[start_idx:end_idx]
# Check if the session contains more than just the header row
if len(session_df)
Подробнее здесь: [url]https://stackoverflow.com/questions/79190364/python-script-that-reads-arduino-outputs-throws-random-serial-port-error[/url]
У меня есть этот скрипт Python для работы, который считывает выходные данные датчиков с Arduino. В основном он работает, но иногда выдает ошибку: [code]SerialException ocurred: ClearCommError failed (PermissionError(13, 'Access is denied.' , None, 5)) Serial port COM3 closed[/code] Другие программы не обращаются к этому порту, и насколько я могу судить, ошибка возникает тем чаще, чем дольше выполняется тест. Вот код: [code]`# Set up serial connection arduino_port = 'COM5' baud_rate = 250000 # Baud rate must match the rate defined in Arduino code
# Global stop flag to manage threading global stop_flag stop_flag = False
# Carbo Offset for better visualization CARBO_OFFSET = 10 CARB_DROP = -200 # Set the threshold for significant carbo drop
def plot_csv_data(filename, log_number, line_styles): # Read the CSV data into a DataFrame df = pd.read_csv(filename, names=["millis", "psi", "temperature C", "accumulated pressure", "froth", "accumulated froth", "carbo", "accumulated carbo", "Cadc", "froth accumulation paused?"], skip_blank_lines=True)
# Find indices of "LOGGING STARTED" entries to separate sessions log_starts = df[df.apply(lambda x: x.str.contains("--------------- LOGGING STARTED ----------------------").any(), axis=1)].index
# Iterate over each logging session and generate a plot for i in range(len(log_starts)): start_idx = log_starts[i] + 1 end_idx = log_starts[i + 1] if i + 1 < len(log_starts) else len(df) session_df = df.iloc[start_idx:end_idx]
# Check if the session contains more than just the header row if len(session_df)