def _write(self, cmdString): try: writeString = cmdString + self.DELIM bytesWritten = self.ser.write(writeString) if len(writeString) != bytesWritten: raise dae_RelayBoard_Common.Denkovi_Exception( "Error during write") except (serial.SerialException, serial.SerialTimeoutException): raise dae_RelayBoard_Common.Denkovi_Exception( "Error while writing")
def _read(self, numBytes): try: retString = self.ser.read(size=numBytes) if len(retString) < numBytes: raise dae_RelayBoard_Common.Denkovi_Exception( "Timeout waiting for response") return retString except (serial.SerialException, serial.SerialTimeoutException): raise dae_RelayBoard_Common.Denkovi_Exception( "Error while writing")
def getState(self, relay): self._checkInit() if relay > self.NUMRELAYS or relay < 1: raise dae_RelayBoard_Common.Denkovi_Exception( "Relay Index out of bounds") stateByte = self._readByte() return self._getState(stateByte, relay)
def initialise(self, deviceID, baudRate, mask, bitMode): try: for dev in self.driver.list_devices(): if re.search(deviceID + '.+', dev[2]): deviceID = dev[2] print('Found device ' + deviceID) break if deviceID is None: raise dae_RelayBoard_Common.Denkovi_Exception( 'No board connected') self.bb = BitBangDevice(deviceID) self.bb.direction = mask self.bb.open() except Exception as e: raise dae_RelayBoard_Common.Denkovi_Exception( 'Could not connect to relay board: %s: %s' % (e.__class__, e))
def _prepForCmd(self): if self.ser == None: raise dae_RelayBoard_Common.Denkovi_Exception( "Board Not Initialised") else: time.sleep(self.cmdDelay) self.ser.flushInput() self.ser.flushOutput()
def getState(self, relay): if relay > self.NUMRELAYS or relay < 1: raise dae_RelayBoard_Common.Denkovi_Exception( "Relay Index out of bound") else: self._prepForCmd() stateString = self._getStateString() return self._getState(relay, stateString)
def setState(self, relay, on): self._checkInit() if relay > self.NUMRELAYS or relay < 1: raise dae_RelayBoard_Common.Denkovi_Exception( "Relay Index out of bounds") stateByte = self._readByte() toggle = 1 << self._getBitIndex(relay) if on: relState = stateByte | toggle else: relState = stateByte & ~toggle self._writeByte(relState)
def setState(self, relay, on): if relay > self.NUMRELAYS or relay < 1: raise dae_RelayBoard_Common.Denkovi_Exception( "Relay Index out of bound") else: self._prepForCmd() if on: stateString = "+" else: stateString = "-" numString = "%02d" % relay self._write(numString + stateString)
def _connectToSerial(self): try: return serial.Serial( port=self.portstring, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, xonxoff=0, # no software flow control rtscts=0, # no RTS/CTS flow control timeout=self.TIMEOUT, writeTimeout=self.TIMEOUT) except serial.SerialException: raise dae_RelayBoard_Common.Denkovi_Exception( "Could not connect to relay board")
def readByte(self): if self.bb is None: raise dae_RelayBoard_Common.Denkovi_Exception( 'Board non initialized') return self.bb.port
def writeByte(self, byte): if self.bb is None: raise dae_RelayBoard_Common.Denkovi_Exception( 'Board non initialized') self.bb.port = byte
def _checkInit(self): if self.FTD2XX is None: raise dae_RelayBoard_Common.Denkovi_Exception( "FTD2XX module not initialised")
def _handleRet(self, ret): if ret[0] != FTD2XXDllWrap.FT_OK: raise dae_RelayBoard_Common.Denkovi_Exception( self._getErrString(ret)) return ret
def _checkInit(self): if self.handle is None: raise dae_RelayBoard_Common.Denkovi_Exception( "FTD2XXWindows device not initialised")
def _checkInit(self): if self.relayHandler is None: raise dae_RelayBoard_Common.Denkovi_Exception( "No supported board has been successfully initialised")