-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoLab.py
912 lines (695 loc) · 31.9 KB
/
AutoLab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# -*- coding: utf-8 -*-
"""
Created on Thu May 5 11:20:16 2022
@author: eenmv
GUI for automating measurements with python.
Currently using Matplotlib to generate graphs might be too slow!
Measurements scripts written in python.
These inherit the instrument objects from the main program
Task:
- More documentation
- More Comments
- Add missing error handling
- Make Resource manager fully multiprocessing compatible
- Add more graph settings in utility tab
"""
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import font as tkFont
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from matplotlib import ticker
import re
import numpy as np
import sys
import os
import datetime
import time
import importlib
from pathlib import Path
from multiprocessing import Process, Queue, Pipe
import pyvisa
#Autolab liberies
import Instruments
import Utility
#import Workers
#Main class and window
class Window(tk.Frame):
"""
Main window for AutoLab
This class should handle GUI
"""
Data = []
def __init__(self, master,Resources):
"""
Initial setup of widgets and the general window position
"""
super().__init__(master)
#self.pack()
#Setup multiprocesing
self.DataStream = Queue()
#generate the pipes for sending and receiving data (Faster thatn Queues)
self.PipeRecv, self.PipeSend = Pipe(duplex=True)
self.Measure = None
self.MeasureActive = False
self.Resources = Resources
###################################
########## Setup Frames #########
###################################
# Find either Setup.txt or SetupExample.txt
if os.path.isfile(os.getcwd()+"\\Setup.txt"):
setupFilename = "Setup.txt"
else:
print("\n\n WARNING: Defaulting to the example setup file! \n\n")
setupFilename = "SetupExample.txt"
####### menu frame #############
self.MenuFrameSetup()
######### Utility frame and Tabs ###########
self.UtilFrame = tk.Frame()
self.UtilFrame.grid(column=1, row=4, columnspan=1, rowspan=1)
self.UtilFrame['borderwidth'] = 2
self.UtilFrame['relief'] = 'raised'
self.UtilFrame['padx'] = 5
self.UtilFrame['pady'] = 5
#self.UtilFrame['bg'] = "purple"
#Create untility tabs
self.UtilTabs = ttk.Notebook(self.UtilFrame,height = 100,width = 595)
self.UtilTabs.pack(side="bottom")
self.utilTabModules = {}
#Add tabs to the utility tab wigdet
self.FileUtiltab = Utility.FileUtil.Util(self.UtilTabs)
self.utilTabModules[self.FileUtiltab.name] = self.FileUtiltab
#self.CreateFileUtilTab()
self.GraphUtilTab = Utility.GraphUtil.Util(self.UtilTabs)
self.utilTabModules[self.GraphUtilTab.name] = self.GraphUtilTab
#self.CreateGraphUtilTab()
self.SetupUtilTabs(setupFilename)
############## File frame ##################
self.fileSysFrame = tk.Frame()
self.fileSysFrame.grid(column=0, row=1, columnspan=1, rowspan=1)
self.fileSysFrame['borderwidth'] = 2
self.fileSysFrame['relief'] = 'raised'
self.fileSysFrame['padx'] = 5
self.fileSysFrame['pady'] = 5
#self.fileSysFrame['bg'] = "green"
#PathLabel = tk.Label(self.fileSysFrame,text="Path")
#PathLabel.grid(column=0,row=0,sticky="w")
self.pathInput = tk.Entry(self.fileSysFrame,width = 80)
self.pathInput.grid(column=0,row=0)
self.path = tk.StringVar()
#Defaults to current directory
self.path.set(os.getcwd())
self.pathInput["textvariable"] = self.path
#filenameLabel = tk.Label(self.fileSysFrame,text="Filename")
#filenameLabel.grid(column=0,row=1,sticky="w")
self.filenameInput = tk.Entry(self.fileSysFrame,width = 80)
self.filenameInput.grid(column=0,row=1)
self.filename = tk.StringVar()
#Defaults to test.txt
self.filename.set(r"test.txt")
self.filenameInput["textvariable"] = self.filename
self.headerInput = tk.Text(self.fileSysFrame,width = 60,height = 4)
self.headerInput.grid(column=0,row=2)
header = "Measuremented started at {}, this should get filled in when saved"
self.headerInput.insert("1.0",header)
############## Graph frame + Graph utility ##################
self.GraphFrame = tk.Frame()
self.GraphFrame.grid(column=1, row=1, columnspan=1, rowspan=3)
self.GraphFrame['borderwidth'] = 2
self.GraphFrame['relief'] = 'raised'
self.GraphFrame['padx'] = 5
self.GraphFrame['pady'] = 5
self.CreateGraph()
############## Measurement frame ###################
self.MeasFrame = tk.Frame()
self.MeasFrame.grid(column=0, row=2, columnspan=1, rowspan=2)
self.MeasFrame['borderwidth'] = 2
self.MeasFrame['relief'] = 'raised'
self.MeasFrame['padx'] = 5
self.MeasFrame['pady'] = 5
#self.MeasFrame['bg'] = "blue"
MeasLabel = tk.Label(self.MeasFrame,text="Measurement Setup")
MeasLabel.pack(side="top")
self.MeasTabs = ttk.Notebook(self.MeasFrame,height = 330,width = 480)
self.MeasTabs.pack(side="bottom")
self.CreateMeasTab()
self.SetupInstruments(setupFilename)
############# Start/stop/busy frame ######################
self.RunFrame = tk.Frame()
self.RunFrame.grid(column=0, row=4, columnspan=1, rowspan=1)
self.RunFrame['borderwidth'] = 2
self.RunFrame['relief'] = 'raised'
self.RunFrame['padx'] = 5
self.RunFrame['pady'] = 5
#self.RunFrame['bg'] = "purple"
self.RunWorkerButton = tk.Button(self.RunFrame,
text = "Run",
bg = "green",
command = self.RunMeasure,
font=tkFont.Font(size=30)
)
self.RunWorkerButton.grid(column=0, row=0,padx=40)
self.StopWorkerButton = tk.Button(self.RunFrame,
text = "Stop",
bg = "red",
command = self.StopMeasure,
font=tkFont.Font(size=30)
)
self.StopWorkerButton.grid(column=1, row=0,padx=40)
self.Icons = {}
self.Icons["IDLE"] = tk.PhotoImage(file=r"Icons\Indicator_IDLE_small.png")
self.Icons["ACTIVE"] = tk.PhotoImage(file=r"Icons\Indicator_ACTIVE_small.png")
self.Icons["ACTIVE_DARK"] = tk.PhotoImage(file=r"Icons\Indicator_ACTIVE_DARK_small.png")
self.Icons["ERROR"] = tk.PhotoImage(file=r"Icons\Indicator_ERROR_small.png")
self.IndicatorLabel = tk.Label(self.RunFrame)
self.IndicatorLabel["image"] = self.Icons["IDLE"]
self.IndicatorLabel.grid(column=2, row=0)
self.IndicatorFlashState = 0
#self.UtilLabel = tk.Label(self.UtilFrame,text="Utilities")
#self.UtilLabel.pack(side="top")
self.UpdateWindow()
############################################
####### Updating Window and graph ##########
############################################
def UpdateWindow(self):
"""
This is the general window that updates anything that needs updating.
Avoid running long process here, must be quick as possible
"""
if self.MeasureActive:
# Update measurement stuff
# flash the active icon
FlashCycles = 4 # Number of cycles to switch flash on
#TODO: Grey-Out things in the top-bar while measurement is running.
if self.IndicatorFlashState<FlashCycles:
self.IndicatorLabel["image"] = self.Icons["ACTIVE"]
else:
self.IndicatorLabel["image"] = self.Icons["ACTIVE_DARK"]
self.IndicatorFlashState += 1
if self.IndicatorFlashState>FlashCycles*2:
self.IndicatorFlashState = 0
# Get data from the worker by reading the que
self.GetData()
Finished = self.CheckMeasureFinished()
if Finished:
print("Measurement finished with exitcode {}".format(self.MeasHandler.Worker.exitcode))
self.MeasureFinished()
self.MeasureActive = False
else:
self.IndicatorLabel["image"] = self.Icons["IDLE"]
if self.GraphUtilTab.CurrentGraph == self.GraphUtilTab.GraphSelectOption.get():
self.UpdateGraph()
else:
#re create graph is new graph type
self.GraphUtilTab.CurrentGraph = self.GraphUtilTab.GraphSelectOption.get()
self.CreateGraph()
self.after(250,self.UpdateWindow)
#########################################
####### Setup utilites section ##########
#########################################
def MenuFrameSetup(self):
"""
Setup menu options at the top of the window. add extra functionality here.
"""
menuFrame = tk.Frame()
menuFrame.grid(column=0, row=0, columnspan=2,sticky="w")
menuFrame['relief'] = 'raised'
menuFrame.grid_columnconfigure(0, weight=1)
self.LoadMeasWorkerButton = tk.Button(menuFrame,text = "Load Script",command = self.LoadMeasWorker)
self.LoadMeasWorkerButton.grid(column=0, row=0,sticky="w")
self.refreshButton=tk.Button(menuFrame,text="Refresh Comms",command=self.refresh_Address_list)
self.refreshButton.grid(column=5,row=0,sticky="e",padx=20)#button to re-poll rm.list_resources
self.rescriptButton=tk.Button(menuFrame,text="Refresh Scripts",command=self.refresh_Workers)
self.rescriptButton.grid(column=1,row=0,padx=20)#button to re-fresh the loaded script
def SetupUtilTabs(self,SetupFile):
print("Loading Utilities")
rm=pyvisa.ResourceManager()
self.address_list=rm.list_resources()
rm.close()#Create a list of Valid GPIB/COMS ports. Useful for passing to workers/utils
file = open(SetupFile,"r")
instSection = False
for line in file:
#Check if we have entered the instrument section of the setup file
if line.startswith("#"):
if line.startswith("#Utilities"):
instSection = True
else:
instSection = False
# If in the instrument section of setup file try to load each instrument
elif instSection:
#try to find instrument in Instruments module
#Load that instrument with given channel and name
try:
tabStr = line.replace("\n","") # remove end of charactor
print(tabStr)
utilTabModule = getattr(Utility, tabStr)
utilTabInst = utilTabModule.Util(self.UtilTabs,parent=self)
self.utilTabModules[utilTabInst.name] = utilTabInst
except Exception as e:
print(e)
file.close()
####################################################
####### Setup instruments from setup file ##########
####################################################
def SetupInstruments(self,SetupFile):
print("Loading instruments")
file = open(SetupFile,"r")
instSection = False
for line in file:
#Check if we have entered the instrument section of the setup file
if line.startswith("#"):
if line.startswith("#Instruments"):
instSection = True
else:
instSection = False
# If in the instrument section of setup file try to load each instrument
elif instSection:
#try to find instrument in Instruments module
#Load that instrument with given channel and name
try:
self.Resources.LoadInst(line)
except Exception as e:
print(e)
file.close()
def refresh_Address_list(self):
"""
quick routine to re-poll the resource list.
TODO: Test this on a live rig
"""
rm=pyvisa.ResourceManager()
self.address_list=rm.list_resources()
rm.close()
def refresh_Workers(self):
"""
Re-build the Measurement GUI by calling importlib.reload, and then Re-creating the WorkerBook and Handler
"""
try:
self.WorkerBook.destroy()
importlib.reload(self.MeasWorkerScript)
self.WorkerBook = ttk.Notebook(self.MeasTabs,height = 330,width = 480)
#as before, creates a holder-frame that can be destroyed while keeping the overall archetecture intact
self.WorkerBook.pack(side="bottom")
self.MeasHandler = self.MeasWorkerScript.Handler(self.WorkerBook,self)
#rebuild Handler Gui/Backend with refreshed Script
self.MeasWorker = self.MeasWorkerScript.Worker
#now refresh the ancilliary things in the GraphUtilTab
try:
self.GraphUtilTab.Initialise_Dropdowns(self.MeasHandler.Header_List)
except AttributeError:
#if tehre is no Header list, you're stuck with the old Textboxes, enjoy!
self.GraphUtilTab.Initialise_Textboxes()
print("Refreshed Current Worker Script!")
except AttributeError:
print("Cannot Refresh an Empty Script!")
############################################
####### Matplotlib Graphing stuff ##########
############################################
def CreateGraph(self,width=6,Height=4.62,dpi=50):
"""
Create a matplotlib figure inside the graphing frame for ploting data
"""
#self.Data = [[0,0e-5,0e-5],[1,1e-5,1e-5],[2,2e-5,4e-5],[3,3e-5,9e-5],[4,4e-5,16e-5]]
#Create figure to place in widget
self.GraphUtilTab.CreateGraph()
self.fig = self.GraphUtilTab.fig
# self.Plot1 = self.GraphUtilTab.Plot1
# self.Plot2 = self.GraphUtilTab.Plot2
# self.ax = self.GraphUtilTab.ax
# self.axtwin = self.GraphUtilTab.axtwin
try:
self.GraphFrameInner.destroy()
self.update()
except:
pass
self.GraphFrameInner = tk.Frame(self.GraphFrame)
self.GraphFrameInner.pack()
self.canvas = FigureCanvasTkAgg(self.fig, master=self.GraphFrameInner) # A tk.DrawingArea.
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(self.canvas, self.GraphFrameInner)
toolbar.update()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def UpdateGraph(self):
"""Update the graph widget with the latest data, using the selected settings
"""
self.GraphUtilTab.UpdateGraph(self.Data)
try:
self.fig.canvas.draw()
except:
print("Couldn't update graph")
########################################
####### Setup Measure section ##########
########################################
def CreateMeasTab(self):
"""Simply generates the measurement tab and add pages to it
"""
#self.MeasWorkerFrame = tk.Frame(self.MeasTabs)
#self.MeasTabs.add(self.MeasWorkerFrame,text="Main Script")
def LoadMeasWorker(self):
"""
Tries to load the expirement script from worker and
assign it to self.MeasWorkerScript and similar.
"""
print("loading main script")
#Get filename of Expirement GUI and worker
filename = fd.askopenfile(initialdir = os.getcwd()+"\\Workers") #Open dialog box at desired folder
if filename==None:
print("Please Select a Script, dangus.")
return
else: #now pressing the Cancel button shouldnt do anything, rather than breaking the script
#DESTROY this previous wokerframe if it exist
try:
self.WorkerBook.destroy()
importlib.reload(self.MeasWorkerScript)
#this will apply any changes made to the scripts as they are loaded.
self.update()
except AttributeError as e:
#should only really throw attribute error on initial bootup, as WorkerBook is not initialised
print(e)
pass
self.WorkerBook = ttk.Notebook(self.MeasTabs,height = 330,width = 480)
#as before, creates a holder-frame that can be destroyed while keeping the overall archetecture intact
self.WorkerBook.pack(side="bottom")
file_path = filename.name #this now contains the path to the relevant script
filename = filename.name[( len(os.getcwd())+1 ):-3]
Module_ID = filename.replace("/",".")
#importlib.import_module requires an ID in the format Package.subpackage.module, this supplies this nicely
self.MeasWorkerScript = importlib.import_module(Module_ID,file_path)
#Only the Module we need has been loaded now, rather than everything in Workers
#also means we can call importlib.reload on it to apply changes
# for module in modules[1:]:
# MeasWorkerScript = getattr(MeasWorkerScript, module)
self.MeasHandler = self.MeasWorkerScript.Handler(self.WorkerBook,self)
try:
self.GraphUtilTab.Initialise_Dropdowns(self.MeasHandler.Header_List)
except AttributeError:
#if tehre is no Header list, you're stuck with the old Textboxes, enjoy!
self.GraphUtilTab.Initialise_Textboxes()
self.MeasWorker = self.MeasWorkerScript.Worker
print("Finished loading worker")
return
################################################################
##### Starting, stopping and moditoring measure section ########
################################################################
def RunMeasure(self):
"""Starts the measurement Worker
Calls the MeasHandler.Start and gives it the Pipe to send data back and forth
"""
#Reset all of graphing data lists
self.Data = []
self.xData = []
self.y1Data = []
self.y2Data = []
try:
filename = self.filenameInput.get()
if self.CreateFile(filename):
print("File created")
else:
raise()
except Exception as e:
print(e)
print("Failed to create file")
return
# try to start the measurement worker, raise error if it failed
try:
if self.MeasHandler.Start(self.PipeSend):
print("Measurement Started")
else:
raise Exception("Failed to start measurement")
return
except Exception as e:
print(e)
self.CloseSaveFile()
return
self.MeasureActive = True
def GetData(self):
"""
Check if the pipe has anything in it
Loads data from the pipe and adds it to the datasets
"""
#start timeout timer for reading the pipe contents, 5sec max
Start_T = time.time()
while(True):
# Timeout if it spends too long reading the que, likely because is very full
if time.time()-Start_T>5:
print("Timeout, Queue might be too large!")
break
#Check if there is something to recieve in the pipe
if not self.PipeRecv.poll():
break
#get data from pipe
Data = self.PipeRecv.recv()#this should be a short list of data to be plotted/written to file
#TODO make flags for drawning to graph
#TODO unwraping multiple points
#If data isn't a string append to rawdata list for plotting
try:#
types=[type(ob) for ob in Data]
if str not in types:
self.Data.append(Data)
#This is the Data to be PLotted On-Screen, not what is saved to the save-file
# TODO add more key work commands, NewFile, ClearGraph,
elif Data=="Esc":
self.MeasureFinished()
break
elif Data=="ClearGraph":
self.Data = []
self.xData = []
self.y1Data = []
self.y2Data = []
continue
elif Data=="NewFile":
self.CloseSaveFile()
self.CreateFile(self.filenameInput.get())
#now have new save file but want the plotted data to reflect whats in the file,
#so duplicate the above fn.
self.Data = []
self.xData = []
self.y1Data = []
self.y2Data = []
continue
except TypeError as e:
#if Data is a single int/float, the types generator wont work.
#because strings are lists of string characters, strings work fine.
#Not sure WHY you'd use a single data point, but Handle it and abort to prevent indexing problems w. graph
print(e)
print("Received Data that couldnt be iterated over, not sure what you're doing! Aborting.")
print(Data)
self.MeasureFinished()
break
##### Save data to save file ####
#Convert Data list to string and remove the brackets. This is what gets saved, so strings are OK.
if type(Data)==list:
#assume by this point data Has been sanitised, so can pass to routines w. impunity
try:
self.MeasHandler.Update(Data)
#pass Data to the measurement Handler in order to update GUI elements within the Worker
except AttributeError:
pass
#Handle the case where there is no Update routine
save_Data = str(Data)
save_Data = save_Data.replace(", ",self.delimiterOption)
save_Data = save_Data[1:-1]+"\n" #removes brackets on either end and ameks new line
#write Data to string
self.file.write( str(save_Data) )
def CheckMeasureFinished(self):
"""
Checks if the measurement has finished without disturding it, in theory
"""
try:
self.MeasHandler.Worker.join(timeout=0.1)
Alive = self.MeasHandler.Worker.is_alive()
if not Alive:
return True
else:
return False
except:
return True
def MeasureFinished(self):
"""
Handles when measurement is finished
"""
print("Measurement finished")
try:
self.MeasHandler.Worker.join()
self.MeasHandler.Stop()
except Exception as e:
print("Could not close worker")
print(e)
self.MeasureActive = False
self.CloseSaveFile()
def StopMeasure(self):
"""
Force stop the measurement
"""
#Send stop command
#Call the stop function in the worker in case it needs to do anything after
self.PipeRecv.send("STOP")
HasStopped = self.MeasHandler.Stop()
if HasStopped:
print("Measurement stopped succesfully")
#Dump queue contents
print("Dumping contents of Pipes")
print("PipeRecv:")
while self.PipeRecv.poll():
Data = self.PipeRecv.recv()
print(Data)
print("PipeSend:")
while self.PipeSend.poll():
Data = self.PipeSend.recv()
print(Data)
print("Boths pipes have been emptied")
self.MeasureActive = False
self.CloseSaveFile()
else:
print("Failed to stop process! It's out of control!")
##################################################
####### Save filename managemant section ##########
##################################################
def CreateFile(self,filename):
"""
Creates and opens file with filename inputted
TODO:
- Change this to ask if you want to overide file if trying to.
"""
path = self.pathInput.get()
header = self.headerInput.get("1.0",tk.END)
#Makes the directory if it doesn't exist
Path(path).mkdir(parents=True, exist_ok=True)
filenamePath = path + "\\" + filename
try:
Overide = self.FileUtiltab.fileMakeOption.get()
if Overide=="O":
Overide = True
AutoEnum = False
else:
Overide = False
AutoEnum = True
except Exception as e:
print(e)
print("failed to read make file options checkbox value")
return False
# CHeck delimiter option from file ultility window
try:
self.delimiterOption = self.FileUtiltab.delimiterOption.get()
except Exception as e:
print(e)
print("Failed to get delimiterOption")
if Overide:
#Make/replaces file
self.file = open(filenamePath,"w")
self.file.seek(0)
self.file.truncate()
else:
#Check if file exist before
fileExist = os.path.isfile(filenamePath)
if (AutoEnum==False) & (fileExist==True):
print("File already exist! Overide off and AutoEnumatation is also off")
print("Not sure what you expected me to do about it!")
#Return False for failed file creation,
# TODO change this to error handling
# TODO create dialog box to ask to overide current file
return False
elif (AutoEnum==False) & (fileExist==False):
# Make file
self.file = open(filenamePath,"w")
self.file.seek(0)
self.file.truncate()
elif AutoEnum==True:
fileExist = True
N = 0
enumFilename = filenamePath
while(fileExist):
N += 1
EnumStr = str(N).zfill(3)
enumFilename = filenamePath[:-4] +"_"+ EnumStr + filenamePath[-4:]
fileExist = os.path.isfile(enumFilename)
if N>999:
print("timeout finding Enumeration")
return False
filenamePath = enumFilename
print("made file with number: {}".format(N))
self.file = open(filenamePath,"w")
self.file.seek(0)
self.file.truncate()
#Inset header file here
#Add current date and time if needed
self.file.write(header.format( str(datetime.datetime.now() ).split(".")[0] ) )
self.file.write("\n#HeaderEnd\n")
#write metadata
for key in self.WorkerBook.children.keys():
try:
#iterate through all children of the main WorkerBook and attempt to call Export_MetaData
#Export_MetaData should return a dictionary
#currently just write dictionary to file.
#TODO: Have metadata written to some other config file to be loaded on bootup
if re.search("frame", key)==None:
#children includes the tk frames and the scripts within.
#we just want the scripts
try:
self.file.write(str(self.WorkerBook.children[key].Export_MetaData()))
self.file.write("\n")
except AttributeError:#Handle the case were no Export_Metadata method exists
pass
except Exception as e:
print(e)
self.file.write("\n#MetadataEnd\n\n")
#return True for succesful file creation
return True
def SaveToFile(self):
pass
def CloseSaveFile(self):
"""Closes Save file
"""
self.file.close()
print("Save file has been closed")
class ResourcesObj(object):
"""
This handles all the communication for the instruments and external resources.
Better to have one singular object as serial ports and comms don't like multiple
callers.
"""
def __init__(self):
"""
Takes a setup file
"""
self.rm = pyvisa.ResourceManager()
self.insts = {}
def LoadInst(self,line):
"""
Load Instrument
"""
try:
line = line.replace("\n","")
splt = line.split(",")
InstClass = getattr(Instruments,splt[0])
#Every instrument class should take a ResourceManager object and a channel name
Inst = InstClass(self.rm, int(splt[1]) )
self.AddInst(Inst,splt[2])
print("Succesfully connected to "+splt[2])
except Exception as e:
raise e
def AddInst(self, inst, name):
"""
Takes Name and Instument object.
Adds to Dict of Instuments.
"""
self.insts[name] = inst
def CloseAll(self):
while( len(self.insts)>0 ):
Name, inst = self.insts.popitem()
try:
inst.__del__()
except:
print("Failed to close {}".format(Name))
if __name__=="__main__":
#Stores all the pyvisa connections
Resources = ResourcesObj()
#Make and start main window
root = tk.Tk()
Experiment = Window(root,Resources)
root.title("Autolab")
Experiment.mainloop()