class Tops: def __init__(self, parent): #init user's preferences config file object self.upref = UserPrefs() buttons_width = 8 imb_buttonx = "2m" imb_buttony = "1m" imb_buttons_framex = "3m" imb_buttons_framey = "2m" imb_int_buttons_framex = "3m" imb_int_buttons_framey = "1m" self.myParent = parent self.main_frame = Frame(parent) ### self.main_frame.pack(expand=YES, fill=BOTH) #MENU self.menubar = Menu(self.myParent) self.myParent.config(menu=self.menubar) topsmenu = Menu(self.menubar, tearoff=0) topsmenu.add_command(label=_("Connect"), command=self.connect) topsmenu.add_command(label=_("Process data"), command=self.process) topsmenu.add_separator() topsmenu.add_command(label=_("Quit"), command=self.on_app_close) filemenu = Menu(self.menubar, tearoff=0) filemenu.add_command(label=_("Open file"), command=self.open_a_file) filemenu.add_command(label=_("Save raw data"), command=self.save_a_file) helpmenu = Menu(self.menubar, tearoff=0) helpmenu.add_command(label=_("About TOPS"), command=self.about) languagemenu = Menu(self.menubar, tearoff=0) language = StringVar() languagemenu.add_radiobutton(label="English",variable=language,value="en") languagemenu.add_radiobutton(label="Italiano",variable=language,value="it") self.menubar.add_cascade(label="Total Station", menu=topsmenu) self.menubar.add_cascade(label="File", menu=filemenu) #self.menubar.add_cascade(label="Language", menu=languagemenu) self.menubar.add_cascade(label="Help", menu=helpmenu) self.upper_frame = Frame(self.main_frame) ### self.upper_frame.pack(side=TOP, expand=NO, padx=10, pady=5, ipadx=5, ipady=5) self.logo_frame = Frame(self.upper_frame) self.logo_frame.pack(side=LEFT, expand=NO) self.logo_data = logo_data self.logo = PhotoImage(data=self.logo_data) self.logo_canvas = Label(self.logo_frame, image=self.logo) self.logo_canvas.pack(side=LEFT, expand=NO, padx=5, pady=5) self.header_frame = Frame(self.upper_frame) self.header_frame.pack(side=LEFT, expand=NO, pady=5) self.buttons_frame = Frame(self.header_frame) self.buttons_frame.pack( side=TOP, expand=NO, fill=Y, ipadx=5, ipady=5) # default control panel self.control_panel0 = Frame(self.header_frame) self.control_panel0.pack( side=TOP, expand=YES, fill=Y, padx=5, pady=5) # control panel for custom serial connection self.control_panel = Frame(self.header_frame) # option 1 : serial port self.option1_frame = Frame(self.control_panel0, relief=RIDGE, bd=1) self.option1_frame.pack(side=TOP) self.option1_label = Label(self.option1_frame, text=_("Port"), width=30) self.option1_label.pack(side=LEFT) self.option1_value = StringVar() self.option1_value.set(self.upref.getvalue('port')) # Leave this Entry uncommented to enter port as a string, or ... # self.option1_entry = Entry(self.option1_frame, textvariable=self.option1_value, width=20) # ... comment out this Menubutton if you want to use the scan() output # # self.option1_entry = Menubutton(self.option1_frame, # text="choose a value", # textvariable=self.option1_value, # relief=RAISED, # width=24) # self.option1_entry.menu = Menu( self.option1_entry, tearoff=0 ) # self.option1_entry["menu"] = self.option1_entry.menu # for n,s in scan(): # self.option1_entry.menu.add_radiobutton ( label=s, # variable=self.option1_value, # value = s) self.option1_entry.pack(side=LEFT, anchor=W) # option MODEL substitutes all connection parameters for better # user experience self.optionMODEL_frame = Frame( self.control_panel0, relief=RIDGE, bd=1) self.optionMODEL_frame.pack(side=TOP) self.optionMODEL_label = Label(self.optionMODEL_frame, text=_("Total Station"), justify=LEFT, width=30) self.optionMODEL_label.pack(side=LEFT, anchor=E) self.optionMODEL_value = StringVar() self.optionMODEL_value.set(self.upref.getvalue('model')) self.optionMODEL_entry = Menubutton(self.optionMODEL_frame, text="choose a model", textvariable=self.optionMODEL_value, relief=RAISED, width=20) self.optionMODEL_entry.menu = Menu(self.optionMODEL_entry, tearoff=0) self.optionMODEL_entry["menu"] = self.optionMODEL_entry.menu for k, (l, m, n) in list(BUILTIN_MODELS.items()): self.optionMODEL_entry.menu.add_radiobutton( label=n, variable=self.optionMODEL_value, value=k, command=self.print_model) self.optionMODEL_entry.pack(side=LEFT, anchor=W) # option 2 : baudrate self.option2_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option2_frame.pack(side=TOP) self.option2_label = Label(self.option2_frame, text="Baudrate", width=30) self.option2_label.pack(side=LEFT) self.option2_value = IntVar() try: assert serial.baudrate_constants assert serial.baudrate_constants is not {} except (AttributeError, AssertionError): self.option2_entry = Entry(self.option2_frame, textvariable=self.option2_value, width=20) else: self.option2_entry = Menubutton(self.option2_frame, text=_("choose a value"), textvariable=self.option2_value, relief=RAISED, width=20) self.option2_entry.menu = Menu(self.option2_entry, tearoff=0) self.option2_entry["menu"] = self.option2_entry.menu for key in sorted(serial.baudrate_constants.keys()): # dynamic list self.option2_entry.menu.add_radiobutton( label="%s" % key, variable=self.option2_value, value=key, ) self.option2_entry.pack(side=LEFT, anchor=W) # option 3 : bytesize self.option3_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option3_frame.pack(side=TOP) self.option3_label = Label(self.option3_frame, text=_("Bytesize"), justify=LEFT, width=30) self.option3_label.pack(side=LEFT, anchor=E) self.option3_value = IntVar() self.option3_entry = Menubutton(self.option3_frame, text=_("choose a value"), textvariable=self.option3_value, relief=RAISED, width=20) self.option3_entry.menu = Menu(self.option3_entry, tearoff=0) self.option3_entry["menu"] = self.option3_entry.menu for v in [8,7,6,5]: self.option3_entry.menu.add_radiobutton(label=str(v), variable=self.option3_value, value=v) self.option3_entry.pack(side=LEFT, anchor=W) # option 4 : parity self.option4_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option4_frame.pack(side=TOP) self.option4_label = Label(self.option4_frame, text=_("Parity setting"), justify=LEFT, width=30) self.option4_label.pack(side=LEFT, anchor=E) self.option4_value = StringVar() self.option4_entry = Menubutton(self.option4_frame, text=_("choose a value"), textvariable=self.option4_value, relief=RAISED, width=20) self.option4_entry.menu = Menu(self.option4_entry, tearoff=0) self.option4_entry["menu"] = self.option4_entry.menu for v in ['Even', 'None', 'Odd']: self.option4_entry.menu.add_radiobutton(label=_(v), variable=self.option4_value, value=v[0]) self.option4_entry.pack(side=LEFT, anchor=W) # option 5 : stop bit self.option5_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option5_frame.pack(side=TOP) self.option5_label = Label(self.option5_frame, text=_("Stop bit"), justify=LEFT, width=30) self.option5_label.pack(side=LEFT, anchor=E) self.option5_value = IntVar() self.option5_entry = Menubutton(self.option5_frame, text=_("choose a value"), textvariable=self.option5_value, relief=RAISED, width=20) self.option5_entry.menu = Menu(self.option5_entry, tearoff=0) self.option5_entry["menu"] = self.option5_entry.menu for v in [1, 2]: self.option5_entry.menu.add_radiobutton(label=str(v), variable=self.option5_value, value=v) self.option5_entry.pack(side=LEFT, anchor=W) # option 6 : time lapse between data packets self.option6_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option6_frame.pack(side=TOP) self.option6_label = Label(self.option6_frame, text=_("Time lapse between data packets"), justify=LEFT, width=30) self.option6_label.pack(side=LEFT, anchor=E) self.option6_value = DoubleVar() self.option6_value.set(self.upref.getvalue('sleeptime')) self.option6_entry = Entry(self.option6_frame, textvariable=self.option6_value, relief=RAISED, justify=RIGHT, width=20) self.option6_entry.pack(side=LEFT, anchor=W) # control buttons self.connect_button = Button(self.buttons_frame, text=_("Connect"), padx=imb_buttonx, pady=imb_buttony) self.connect_button.pack(side=LEFT, anchor=S) self.connect_button.bind("<Button-1>", self.connect_action) self.connect_button.bind("<Return>", self.connect_action) self.save_button = Button(self.buttons_frame, text=_("Save raw data"), padx=imb_buttonx, pady=imb_buttony) self.save_button.pack(side=LEFT, anchor=S) self.save_button.bind("<Button-1>", self.save_action) self.save_button.bind("<Return>", self.save_action) self.process_button = Button(self.buttons_frame, text=_("Process data"), padx=imb_buttonx, pady=imb_buttony) self.process_button.pack(side=LEFT, anchor=S) self.process_button.bind("<Button-1>", self.process_action) self.process_button.bind("<Return>", self.process_action) self.status = StatusBar(self.main_frame) self.status.set('Welcome to Total Open Station') self.status.pack(side=BOTTOM, fill=X) # text frame self.text_frame = Frame(self.main_frame) self.text_frame.pack(side=BOTTOM, expand=YES, fill=BOTH) self.text_area = Text(self.text_frame, width=80) self.text_area.insert(END, _("Welcome.\nTurn your device on.")) self.text_area.pack(side=LEFT, expand=YES, fill=Y) self.scrollY = Scrollbar(self.text_frame, orient=VERTICAL, command=self.text_area.yview) self.text_area['yscrollcommand'] = self.scrollY.set self.scrollY.pack(side=RIGHT, expand=YES, fill=Y, anchor=W) # init stuff self.myParent.title("Total Open Station") self.myParent.protocol("WM_DELETE_WINDOW", self.on_app_close) self.print_model() self.myParent.mainloop() def on_click_language(self): ''' open select language dialog ''' pass def on_app_close(self): '''Callback function to ask for confirmation before quitting the application.''' if askokcancel("Quit","Do you really want to quit application ?"): self.myParent.destroy() def exit_action(self, event): self.on_app_close() def print_model(self): model = self.optionMODEL_value.get() if model != 'custom': self.control_panel.forget() self.option2_value.set(0) self.option3_value.set(0) self.option4_value.set('') self.option5_value.set(0) else: self.option2_value.set(9600) self.option3_value.set(8) self.option4_value.set('N') self.option5_value.set(1) self.control_panel.pack(side=TOP, expand=YES, fill=Y, ipadx=5, ipady=5) def connect(self): chosen_model = self.optionMODEL_value.get() chosen_port = self.option1_value.get() # do nothing if input is empty if not (chosen_model == '' or chosen_port == ''): if chosen_model == 'custom': # dictionary for passing options to Serial self.options = {'baudrate': self.option2_value.get(), 'bytesize': self.option3_value.get(), 'parity': self.option4_value.get(), 'stopbits': self.option5_value.get()} else: self.options = {} modelclass = BUILTIN_MODELS[chosen_model] if isinstance(modelclass, tuple): try: # builtin model builder mod, cls, name = modelclass modelclass = getattr( __import__('totalopenstation.models.' + mod, None, None, [cls]), cls) except ImportError as msg: showwarning(_('Import error'), _('Error loading the required model module: %s' % msg)) mc = modelclass(chosen_port, **self.options) try: mc.close() # sometimes the port will be already open for no reason mc.open() except serial.SerialException as detail: e = ErrorDialog(self.myParent, detail) else: st = DownloadDialog(self.myParent) sleeptime = float(self.option6_value.get()) if st.result: self.status.set(_("Waiting for data: Please start the transfer from your total station menu.")) while mc.inWaiting() == 0: sleep(sleeptime) n = mc.inWaiting() result = mc.read(n) self.replace_text(result.decode()) sleep(sleeptime) while mc.inWaiting() > 0: newdata = mc.read(mc.inWaiting()) result += newdata self.status.set(_('Downloaded %d bytes'), len(result)) self.replace_text(result.decode()) sleep(sleeptime) # TODO determine sleep time from baudrate mc.close() showinfo(_('Success!'), _('Download finished!\nYou have %d bytes of data.') % len(result)) def connect_action(self, event): self.connect() def open_a_file(self): try: d = tkinter.filedialog.askopenfilename() of = open(d, 'r') oc = of.read() self.replace_text(oc) except: pass def open_action(self, event): self.open_a_file() def process(self): data = self.text_area.get("1.0", END) d = ProcessDialog(self.myParent, data) def process_action(self, event): self.process() def save_a_file(self): try: sd = tkinter.filedialog.asksaveasfilename(defaultextension='.tops') data = self.text_area.get("1.0", END) of = open(sd, 'w') oc = of.write(data) except: pass def save_action(self, event): self.save_a_file() def about(self): d = AboutDialog(self.myParent) def about_action(self, event): self.about() def replace_text(self, text): self.text_area.delete("1.0", END) self.text_area.insert(END, text.replace('\r', '')) self.text_area.yview_moveto(1.0) self.text_area.update_idletasks()
class Tops: def __init__(self, parent): #init user's preferences config file object self.upref = UserPrefs() buttons_width = 8 imb_buttonx = "2m" imb_buttony = "1m" imb_buttons_framex = "3m" imb_buttons_framey = "2m" imb_int_buttons_framex = "3m" imb_int_buttons_framey = "1m" self.myParent = parent self.main_frame = Frame(parent) ### self.main_frame.pack(expand=YES, fill=BOTH) #MENU self.menubar = Menu(self.myParent) self.myParent.config(menu=self.menubar) topsmenu = Menu(self.menubar, tearoff=0) topsmenu.add_command(label=_("Connect"), command=self.connect) topsmenu.add_command(label=_("Process data"), command=self.process) topsmenu.add_separator() topsmenu.add_command(label=_("Quit"), command=self.on_app_close) filemenu = Menu(self.menubar, tearoff=0) filemenu.add_command(label=_("Open file"), command=self.open_a_file) filemenu.add_command(label=_("Save raw data"), command=self.save_a_file) helpmenu = Menu(self.menubar, tearoff=0) helpmenu.add_command(label=_("About TOPS"), command=self.about) languagemenu = Menu(self.menubar, tearoff=0) language = StringVar() languagemenu.add_radiobutton(label="English",variable=language,value="en") languagemenu.add_radiobutton(label="Italiano",variable=language,value="it") self.menubar.add_cascade(label="Total Station", menu=topsmenu) self.menubar.add_cascade(label="File", menu=filemenu) #self.menubar.add_cascade(label="Language", menu=languagemenu) self.menubar.add_cascade(label="Help", menu=helpmenu) self.upper_frame = Frame(self.main_frame) ### self.upper_frame.pack(side=TOP, expand=NO, padx=10, pady=5, ipadx=5, ipady=5) self.logo_frame = Frame(self.upper_frame) self.logo_frame.pack(side=LEFT, expand=NO) self.logo_data = logo_data self.logo = PhotoImage(data=self.logo_data) self.logo_canvas = Label(self.logo_frame, image=self.logo) self.logo_canvas.pack(side=LEFT, expand=NO, padx=5, pady=5) self.header_frame = Frame(self.upper_frame) self.header_frame.pack(side=LEFT, expand=NO, pady=5) self.buttons_frame = Frame(self.header_frame) self.buttons_frame.pack( side=TOP, expand=NO, fill=Y, ipadx=5, ipady=5) # default control panel self.control_panel0 = Frame(self.header_frame) self.control_panel0.pack( side=TOP, expand=YES, fill=Y, padx=5, pady=5) # control panel for custom serial connection self.control_panel = Frame(self.header_frame) # option 1 : serial port self.option1_frame = Frame(self.control_panel0, relief=RIDGE, bd=1) self.option1_frame.pack(side=TOP) self.option1_label = Label(self.option1_frame, text=_("Port"), width=30) self.option1_label.pack(side=LEFT) self.option1_value = StringVar() self.option1_value.set(self.upref.getvalue('port')) # Leave this Entry uncommented to enter port as a string, or ... # self.option1_entry = Entry(self.option1_frame, textvariable=self.option1_value, width=20) # ... comment out this Menubutton if you want to use the scan() output # # self.option1_entry = Menubutton(self.option1_frame, # text="choose a value", # textvariable=self.option1_value, # relief=RAISED, # width=24) # self.option1_entry.menu = Menu( self.option1_entry, tearoff=0 ) # self.option1_entry["menu"] = self.option1_entry.menu # for n,s in scan(): # self.option1_entry.menu.add_radiobutton ( label=s, # variable=self.option1_value, # value = s) self.option1_entry.pack(side=LEFT, anchor=W) # option MODEL substitutes all connection parameters for better # user experience self.optionMODEL_frame = Frame( self.control_panel0, relief=RIDGE, bd=1) self.optionMODEL_frame.pack(side=TOP) self.optionMODEL_label = Label(self.optionMODEL_frame, text=_("Total Station"), justify=LEFT, width=30) self.optionMODEL_label.pack(side=LEFT, anchor=E) self.optionMODEL_value = StringVar() self.optionMODEL_value.set(self.upref.getvalue('model')) self.optionMODEL_entry = Menubutton(self.optionMODEL_frame, text="choose a model", textvariable=self.optionMODEL_value, relief=RAISED, width=20) self.optionMODEL_entry.menu = Menu(self.optionMODEL_entry, tearoff=0) self.optionMODEL_entry["menu"] = self.optionMODEL_entry.menu for k, (l, m, n) in BUILTIN_MODELS.items(): self.optionMODEL_entry.menu.add_radiobutton( label=n, variable=self.optionMODEL_value, value=k, command=self.print_model) self.optionMODEL_entry.pack(side=LEFT, anchor=W) # option 2 : baudrate self.option2_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option2_frame.pack(side=TOP) self.option2_label = Label(self.option2_frame, text="Baudrate", width=30) self.option2_label.pack(side=LEFT) self.option2_value = IntVar() try: assert serial.baudrate_constants assert serial.baudrate_constants is not {} except (AttributeError, AssertionError): self.option2_entry = Entry(self.option2_frame, textvariable=self.option2_value, width=20) else: self.option2_entry = Menubutton(self.option2_frame, text=_("choose a value"), textvariable=self.option2_value, relief=RAISED, width=20) self.option2_entry.menu = Menu(self.option2_entry, tearoff=0) self.option2_entry["menu"] = self.option2_entry.menu for key in sorted(serial.baudrate_constants.keys()): # dynamic list self.option2_entry.menu.add_radiobutton( label="%s" % key, variable=self.option2_value, value=key, ) self.option2_entry.pack(side=LEFT, anchor=W) # option 3 : bytesize self.option3_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option3_frame.pack(side=TOP) self.option3_label = Label(self.option3_frame, text=_("Bytesize"), justify=LEFT, width=30) self.option3_label.pack(side=LEFT, anchor=E) self.option3_value = IntVar() self.option3_entry = Menubutton(self.option3_frame, text=_("choose a value"), textvariable=self.option3_value, relief=RAISED, width=20) self.option3_entry.menu = Menu(self.option3_entry, tearoff=0) self.option3_entry["menu"] = self.option3_entry.menu for v in [8,7,6,5]: self.option3_entry.menu.add_radiobutton(label=str(v), variable=self.option3_value, value=v) self.option3_entry.pack(side=LEFT, anchor=W) # option 4 : parity self.option4_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option4_frame.pack(side=TOP) self.option4_label = Label(self.option4_frame, text=_("Parity setting"), justify=LEFT, width=30) self.option4_label.pack(side=LEFT, anchor=E) self.option4_value = StringVar() self.option4_entry = Menubutton(self.option4_frame, text=_("choose a value"), textvariable=self.option4_value, relief=RAISED, width=20) self.option4_entry.menu = Menu(self.option4_entry, tearoff=0) self.option4_entry["menu"] = self.option4_entry.menu for v in ['Even', 'None', 'Odd']: self.option4_entry.menu.add_radiobutton(label=_(v), variable=self.option4_value, value=v[0]) self.option4_entry.pack(side=LEFT, anchor=W) # option 5 : stop bit self.option5_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option5_frame.pack(side=TOP) self.option5_label = Label(self.option5_frame, text=_("Stop bit"), justify=LEFT, width=30) self.option5_label.pack(side=LEFT, anchor=E) self.option5_value = IntVar() self.option5_entry = Menubutton(self.option5_frame, text=_("choose a value"), textvariable=self.option5_value, relief=RAISED, width=20) self.option5_entry.menu = Menu(self.option5_entry, tearoff=0) self.option5_entry["menu"] = self.option5_entry.menu for v in [1, 2]: self.option5_entry.menu.add_radiobutton(label=str(v), variable=self.option5_value, value=v) self.option5_entry.pack(side=LEFT, anchor=W) # option 6 : time lapse between data packets self.option6_frame = Frame(self.control_panel, relief=RIDGE, bd=1) self.option6_frame.pack(side=TOP) self.option6_label = Label(self.option6_frame, text=_("Time lapse between data packets"), justify=LEFT, width=30) self.option6_label.pack(side=LEFT, anchor=E) self.option6_value = DoubleVar() self.option6_value.set(self.upref.getvalue('sleeptime')) self.option6_entry = Entry(self.option6_frame, textvariable=self.option6_value, relief=RAISED, justify=RIGHT, width=20) self.option6_entry.pack(side=LEFT, anchor=W) # control buttons self.connect_button = Button(self.buttons_frame, text=_("Connect"), padx=imb_buttonx, pady=imb_buttony) self.connect_button.pack(side=LEFT, anchor=S) self.connect_button.bind("<Button-1>", self.connect_action) self.connect_button.bind("<Return>", self.connect_action) self.save_button = Button(self.buttons_frame, text=_("Save raw data"), padx=imb_buttonx, pady=imb_buttony) self.save_button.pack(side=LEFT, anchor=S) self.save_button.bind("<Button-1>", self.save_action) self.save_button.bind("<Return>", self.save_action) self.process_button = Button(self.buttons_frame, text=_("Process data"), padx=imb_buttonx, pady=imb_buttony) self.process_button.pack(side=LEFT, anchor=S) self.process_button.bind("<Button-1>", self.process_action) self.process_button.bind("<Return>", self.process_action) self.status = StatusBar(self.main_frame) self.status.set('Welcome to Total Open Station') self.status.pack(side=BOTTOM, fill=X) # text frame self.text_frame = Frame(self.main_frame) self.text_frame.pack(side=BOTTOM, expand=YES, fill=BOTH) self.text_area = Text(self.text_frame, width=80) self.text_area.insert(END, _("Welcome.\nTurn your device on.")) self.text_area.pack(side=LEFT, expand=YES, fill=Y) self.scrollY = Scrollbar(self.text_frame, orient=VERTICAL, command=self.text_area.yview) self.text_area['yscrollcommand'] = self.scrollY.set self.scrollY.pack(side=RIGHT, expand=YES, fill=Y, anchor=W) # init stuff self.myParent.title("Total Open Station") self.myParent.protocol("WM_DELETE_WINDOW", self.on_app_close) self.print_model() self.myParent.mainloop() def on_click_language(self): ''' open select language dialog ''' pass def on_app_close(self): '''Callback function to ask confirmation before quitting the application.''' if askokcancel("Quit","Do you really want to quit application ?"): self.myParent.destroy() def exit_action(self, event): self.on_app_close() def print_model(self): model = self.optionMODEL_value.get() if model != 'custom': self.control_panel.forget() self.option2_value.set(0) self.option3_value.set(0) self.option4_value.set('') self.option5_value.set(0) else: self.option2_value.set(9600) self.option3_value.set(8) self.option4_value.set('N') self.option5_value.set(1) self.control_panel.pack(side=TOP, expand=YES, fill=Y, ipadx=5, ipady=5) def connect(self): chosen_model = self.optionMODEL_value.get() chosen_port = self.option1_value.get() # do nothing if input is empty if not (chosen_model == '' or chosen_port == ''): if chosen_model == 'custom': # dictionary for passing options to Serial self.options = {'baudrate': self.option2_value.get(), 'bytesize': self.option3_value.get(), 'parity': self.option4_value.get(), 'stopbits': self.option5_value.get()} else: self.options = {} modelclass = BUILTIN_MODELS[chosen_model] if isinstance(modelclass, tuple): try: # builtin model builder mod, cls, name = modelclass modelclass = getattr( __import__('totalopenstation.models.' + mod, None, None, [cls]), cls) except ImportError, msg: showwarning(_('Import error'), _('Error loading the required model module: %s' % msg)) mc = modelclass(chosen_port, **self.options) try: mc.close() # sometimes the port will be already open for no reason mc.open() except serial.SerialException, detail: e = ErrorDialog(self.myParent, detail) else: st = DownloadDialog(self.myParent) sleeptime = self.option6_value.get() if st.result: self.status.set(_("Waiting for data: please start transfer from your total station menu.")) while mc.inWaiting() == 0: sleep(sleeptime) n = mc.inWaiting() result = mc.read(n) self.replace_text(str(result)) sleep(sleeptime) while mc.inWaiting() > 0: newdata = mc.read(mc.inWaiting()) result += newdata self.status.set(_('Downloaded %d bytes'), len(result)) self.replace_text(str(result)) sleep(sleeptime) # TODO determine sleep time from baudrate mc.close() showinfo(_('Success!'), _('Download finished!\nYou have %d bytes of data.') % len(result))
class Connector(serial.Serial, Thread): def __init__(self, port=None, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0, writeTimeout=None, dsrdtr=None): self.upref = UserPrefs() sleeptime = float(self.upref.getvalue('sleeptime')) Thread.__init__(self) self.dl_started = Event() self.dl_finished = Event() serial.Serial.__init__(self, port=port, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=timeout, xonxoff=xonxoff, rtscts=rtscts, writeTimeout=writeTimeout, dsrdtr=dsrdtr) def open(self): serial.Serial.open(self) def download(self): '''Download method for user interfaces. First the class must be instantiated, then the port is open and the transfer from the device can start. Once the transfer is finished the user interface should call this method.''' n = self.inWaiting() result = self.read(n) # looks like there is a maximum buffer of 4096 characters, so we have # to wait for a short time and iterate the process until finished sleep(sleeptime) while self.inWaiting() > 0: result = result + self.read(self.inWaiting()) sleep(sleeptime) self.result = result def fast_download(self): '''Implement a `fast' download method that requires less user input. Inside, it calls download() itself, just wrapping it inside another loop that checks whether there's input coming from the serial port: when data become to appear, download() can start. ''' while self.inWaiting() == 0: sleep(sleeptime) self.dl_started.set() try: self.download() except threading.exceptions.KeyboardInterrupt: sys.exit() else: self.dl_finished.set() def run(self): self.fast_download()
class Connector(serial.Serial, Thread): '''Connect to a total station. For more information : `Pyserial documentation <https://pythonhosted.org/pyserial/pyserial_api.html#native-ports>`_ Args: port: Device name or :const:`None`. baudrate (int): Baud rate such as 9600 or 115200 etc. bytesize: Number of data bits. parity: Enable parity checking. stopbits: Number of stop bits. timeout (float): Set a read timeout value. xonxoff (bool): Enable software flow control. rtscts (bool): Enable hardware (RTS/CTS) flow control. bool dsrdtr (bool): Enable hardware (DSR/DTR) flow control. writeTimeout (float): Set a write timeout value. ''' def __init__(self, port=None, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0, writeTimeout=None, dsrdtr=None): self.upref = UserPrefs() sleeptime = float(self.upref.getvalue('sleeptime')) Thread.__init__(self) self.dl_started = Event() self.dl_finished = Event() serial.Serial.__init__(self, port=port, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=timeout, xonxoff=xonxoff, rtscts=rtscts, writeTimeout=writeTimeout, dsrdtr=dsrdtr) def open(self): '''Open the serial link. ''' serial.Serial.open(self) def download(self): '''Download method for user interfaces. First the class must be instantiated, then the port is open and the transfer from the device can start. Once the transfer is finished the user interface should call this method.''' n = self.inWaiting() result = self.read(n) # looks like there is a maximum buffer of 4096 characters, so we have # to wait for a short time and iterate the process until finished sleep(sleeptime) while self.inWaiting() > 0: result = result + self.read(self.inWaiting()) sleep(sleeptime) self.result = result def fast_download(self): '''Implement a *fast* download method that requires less user input. Inside, it calls download() itself, just wrapping it inside another loop that checks whether there's input coming from the serial port: when data become to appear, download() can start. ''' while self.inWaiting() == 0: sleep(sleeptime) self.dl_started.set() try: self.download() except threading.exceptions.KeyboardInterrupt: sys.exit() else: self.dl_finished.set() def run(self): self.fast_download()
class Connector(serial.Serial, Thread): def __init__(self, port=None, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0, writeTimeout=None, dsrdtr=None): self.upref = UserPrefs() sleeptime = self.upref.getvalue('sleeptime') Thread.__init__(self) self.dl_started = Event() self.dl_finished = Event() serial.Serial.__init__(self, port=port, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits, timeout=timeout, xonxoff=xonxoff, rtscts=rtscts, writeTimeout=writeTimeout, dsrdtr=dsrdtr) def open(self): serial.Serial.open(self) def download(self): '''Download method for user interfaces. First the class must be instantiated, then the port is open and the transfer from the device can start. Once the transfer is finished the user interface should call this method.''' n = self.inWaiting() result = self.read(n) # looks like there is a maximum buffer of 4096 characters, so we have # to wait for a short time and iterate the process until finished sleep(sleeptime) while self.inWaiting() > 0: result = result + self.read(self.inWaiting()) sleep(sleeptime) self.result = result def fast_download(self): '''Implement a `fast' download method that requires less user input. Inside, it calls download() itself, just wrapping it inside another loop that checks whether there's input coming from the serial port: when data become to appear, download() can start. ''' while self.inWaiting() == 0: sleep(sleeptime) self.dl_started.set() try: self.download() except threading.exceptions.KeyboardInterrupt: sys.exit() else: self.dl_finished.set() def run(self): self.fast_download()