def Run(self): ''' Run the method ''' fp = file(self.methodFileName, 'r') savedFile = pickle.load(fp) actions = savedFile.list cfg.LogFromOtherThread('--- ' + self.methodFileName + ' ---') for idx in range(self.repeat): if self.repeat > 1: cfg.LogFromOtherThread('Run no. ' + str(idx + 1)) for action in actions: action.Run() if cfg.userStopped: return
def saveFile(self, event, message, wildcard, extension, listCtrl): ''' File saving helper function ''' dlg = wx.FileDialog(self.mainFrame, message=message, defaultDir=cfg.UserFilesPath(), defaultFile="", wildcard=wildcard, style=wx.SAVE) if dlg.ShowModal() == wx.ID_OK: try: path = dlg.GetPath() name = os.path.splitext(path)[0] with open(name + extension, 'w') as fp: pickle.dump( SavedFile(self.system.GetSystemUid(), listCtrl.getDataItemsList()), fp) cfg.LogFromOtherThread('File saved: ' + path) fp.close() except (IOError, EOFError): errDlg = wx.MessageDialog(self.mainFrame, 'Error saving the file', 'Error', wx.OK | wx.ICON_ERROR) errDlg.ShowModal() errDlg.Destroy() dlg.Destroy()
def loadFile(self, event, message, wildcard, listCtrl): ''' File loading helper function ''' dlg = wx.FileDialog(self.mainFrame, message=message, defaultDir=cfg.UserFilesPath(), defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: try: filePath = dlg.GetPaths()[0] with open(filePath, 'r') as fp: dlg.Destroy() savedFile = pickle.load(fp) if savedFile.systemUid == self.system.GetSystemUid(): listCtrl.populateList(savedFile.list) cfg.LogFromOtherThread('File loaded: ' + filePath) else: errDlg = wx.MessageDialog( self.mainFrame, 'File incompatible with this system', 'Error', wx.OK | wx.ICON_ERROR) errDlg.ShowModal() errDlg.Destroy() except (IOError, EOFError): errDlg = wx.MessageDialog(self.mainFrame, 'Error loading the file', 'Error', wx.OK | wx.ICON_ERROR) errDlg.ShowModal() errDlg.Destroy()
def Connect(self, port): ''' port - the name of the serial port connected to the Arduino. This works on unix and windows systems, given the appropriate name ''' try: self.serial = serial.Serial( port, self.baudrate, writeTimeout=self.serialWriteTimeoutSec, timeout=self.serialTimeoutSec) except: cfg.LogFromOtherThread('Arduino did not respond', True) return None time.sleep(2) if self.serial == None or self._sendData('Read A0') == None: return None # start reading timer self.timer = wx.Timer(cfg.app) cfg.app.Bind(wx.EVT_TIMER, self.CacheUpdate, self.timer) self.timer.Start(self.cacheReadDelayMilisec) return True
def _sendData(self, txData, addLineBreak=True, lock=True, wait=False, log=False): ''' This function should be called only in this class Send a command (txData) to the Arduino and wait for acknowledgment. Failure of the acknowledgment to arrive logs the event and raises an exception writeTimeoutSec - time in seconds to wait for a software serial response addLineBreak - add a line break after txData (to tell the Arduino to execute the command) lock - lock access to Arduino during this function wait - wait until the Arduino is unlocked. This is used for critical commands such as write commands log - log the communication to the screen. mostly for debug purposes Returns: the data received (if any) ''' if self.serial == None: return None if lock == True: if self.accessSemaphore.acquire(False) == False: if wait: while self.accessSemaphore.acquire(False) == False: pass else: return None if log: print 'we say: ' + txData if addLineBreak == True: txData = txData + '\r' try: self.serial.write(txData) rxData = self._getData() except: rxData = '' if lock == True: self.accessSemaphore.release() if log: print 'Arduino says: ' + rxData answerEnd = rxData.rfind("done") if answerEnd == -1: self.nonResponsiveCounter += 1 if wait == True or self.nonResponsiveCounter > self.maxNonResponseAllowed: cfg.LogFromOtherThread( 'Arduino did not respond %d times' % (self.nonResponsiveCounter), True) return None self.nonResponsiveCounter = 0 return rxData[0:answerEnd].strip()
def run(self): ''' Run the list's items ''' cfg.UpdateControlsFromOtherThread(True) cfg.LogFromOtherThread(self.runStartString) dataItemsAndItems = self.getDataItemsAndItems() for dataItem, item in dataItemsAndItems: self.list.Select(item, False) for dataItem, item in dataItemsAndItems: self.list.Select(item, True) dataItem.Run() if cfg.userStopped: cfg.userStopped = False break self.list.Select(item, False) cfg.UpdateControlsFromOtherThread(False) cfg.LogFromOtherThread('')
def Run(self, Log=True): ''' Run the action ''' if Log: logMsg = datetime.datetime.now().strftime('%X ') logMsg += self.name for idx, param in enumerate(self.params): if idx == 0: logMsg += ': ' else: logMsg += ', ' logMsg += param.name + '=' + str(param.value) cfg.LogFromOtherThread(logMsg) # Call the sub-class' command function self.Command()