From 11e9e3ea22905d85f88515d51635764a9cde9547 Mon Sep 17 00:00:00 2001
From: unknown <asoalexandros@gmail.com>
Date: Fri, 21 Mar 2025 15:16:46 +0100
Subject: [PATCH] ini files part 1

---
 hp4155/Custom_SMU/lib/help.py | 38 ++++++++++++++++++++++
 hp4155/Custom_SMU/main.py     | 60 +++++++++++++++++++++++++++++++++--
 2 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/hp4155/Custom_SMU/lib/help.py b/hp4155/Custom_SMU/lib/help.py
index ac3295b..410d038 100644
--- a/hp4155/Custom_SMU/lib/help.py
+++ b/hp4155/Custom_SMU/lib/help.py
@@ -172,6 +172,44 @@ def check_cons_smu_samp(smus:list):
             cons_smu_numbers.append(i+1) # for the actual number of smus
 
     return cons_smu_numbers
+
+def load_ini():
+    root = tk.Tk()
+    root.withdraw()
+    root.lift() #show window above all other applications
+
+    root.attributes("-topmost", True)#window stays above all other applications
+
+    
+    file = filedialog.askopenfilename(filetypes=[("Ini files","*.ini")],title ='Select ini file')
+    while file.endswith(".ini") == False:
+        #open again filedialog with error message box
+        answer=tk.messagebox.askyesno(message='Do you want to cancel the ini file load?')
+        if answer == True:
+            raise Exception("Ini File Operation aborted!")
+        else:
+            file = filedialog.askopenfilename(filetypes=[("Ini files","*.ini")],title = "Select ini file")
+    root.destroy()
+    return file
+
+
+# Get measurement mode from ini file
+def get_mode(sections):
+    for section in sections:
+        if "MEASUREMENT MODE:" in section:
+            mode = section.split(":")[1]
+            return mode
+
+    raise Exception("Meaurement mode not found ini file!")
+
+def get_integration(sections):
+    for section in sections:
+        if "INTEGRATION TIME:" in section:
+            integration = section.split(":")[1]
+            return integration
+
+    raise Exception("Integration Time not found ini file!")
+    
     
 
     
diff --git a/hp4155/Custom_SMU/main.py b/hp4155/Custom_SMU/main.py
index c93e5bc..084cbfc 100644
--- a/hp4155/Custom_SMU/main.py
+++ b/hp4155/Custom_SMU/main.py
@@ -24,9 +24,10 @@ tab.titles = titles
 display(tab)
 
 start = widgets.Button(description='Start Measurement')
+ini = widgets.Button(description = 'Import from ini.')
 output = widgets.Output()
 
-display(start,output)
+display(widgets.HBox([start,ini]),output)
 
 device = hp4155a.HP4155a('GPIB0::17::INSTR')
 
@@ -34,6 +35,8 @@ def on_start_clicked(b):
     with output:
         clear_output()
         change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+        start.disabled = True
+        ini.disabled = True
 
         # Reset the device
         device.reset()
@@ -194,6 +197,8 @@ def on_start_clicked(b):
             except Exception as e:
                 error_box(e)
                 change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+                start.disabled = False
+                ini.disabled = False
                 return
                 
 
@@ -209,6 +214,8 @@ def on_start_clicked(b):
             if counter>1:
                 error_box(message)
                 change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+                start.disabled = False
+                ini.disabled = False
                 return
             
 
@@ -384,6 +391,8 @@ def on_start_clicked(b):
             except Exception as e:
                 error_box(e)
                 change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+                start.disabled = False
+                ini.disabled = False
                 return
                 
 
@@ -399,6 +408,8 @@ def on_start_clicked(b):
             if counter>1:
                 error_box(message)
                 change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+                start.disabled = False
+                ini.disabled = False
                 return
             
 
@@ -414,7 +425,7 @@ def on_start_clicked(b):
             # Save results
             df = pd.Dataframe(values)
 
-            filename = f"{sample_series}_{field}_{dut}_{measurement_name}_sweep.txt"
+            filename = f"{sample_series}_{field}_{dut}_{measurement_name}_sampling.txt"
             txt_file = create_file(filename)
             ini_file = os.path.splitext(txt_file)[0]+'.ini'
 
@@ -544,6 +555,8 @@ def on_start_clicked(b):
             if counter>1:
                 error_box(message)
                 change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+                start.disabled = False
+                ini.disabled = False
                 return
 
             filename = f"{sample_series}_{field}_{dut}_{measurement_name}_sweep.txt"
@@ -594,8 +607,51 @@ def on_start_clicked(b):
         # End of fuction
         information_box("Measurement finished!")
         change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+        start.disabled = False
+        ini.disabled = False
         return # just to be sure
 
+
+def on_ini_clicked(b):
+    with output:
+        clear_output()
+        change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+        start.disabled = True
+        ini.disabled = True
+
+        #load values to the interface
+        config = configparser.ConfigParser()
+        try:
+            file = load_ini()
+        except Exception as e:
+            error_box(e)
+            change_state(first_page,second_page,third_page,fourth_page,fifth_page)
+            start.disabled = False
+            ini.disabled = False
+            return
+        try:
+            # Now we do exactly the opposite thing dictionaries to widgets
+            #read the values from each section
+            config.read(file)
+
+            # Get the sections 
+            sections = config.sections()
+            # Get the measurement Mode
+            measurement_mode = get_mode(sections)
+
+            # Get the constant smus
+            for j in range(1,5):
+                third_page[21,j].value = config.get(f"CONSTANT SMU{j}",'value') 
+                third_page[22,j].value = config.get(f"CONSTANT SMU{j}",'comp')
+                
+
+            if measurement_mode == 'SWEEP':
+                third_page[0,0].value = measurement_mode
+                third_page[0,1].value = get_integration(sections)
+
+                
+                
+                
             
 
 start.on_click(on_start_clicked)
-- 
GitLab