Skip to content

Commit

Permalink
better MODBUS connect/disconnect reporting and reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
MAKOMO committed Nov 29, 2022
1 parent 90b1970 commit e7f091c
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions src/artisanlib/modbusport.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ def __init__(self,aw):
self.bytesize = 8
self.parity= 'N'
self.stopbits = 1
self.timeout = 0.4 # serial MODBUS timeout
self.timeout = 0.3 # serial MODBUS timeout
self.serial_readRetries = 0 # user configurable, defaults to 0
self.IP_timeout = 0.4 # UDP/TCP MODBUS timeout in seconds
self.IP_timeout = 0.2 # UDP/TCP MODBUS timeout in seconds
self.IP_retries = 1 # UDP/TCP MODBUS retries (max 2)
self.PID_slave_ID = 0
self.PID_SV_register = 0
Expand Down Expand Up @@ -221,6 +221,10 @@ def disconnect(self):
self.master = None
self.clearReadingsCache()

def disconnectOnError(self):
if self.disconnect_on_error and (self.commError or not self.isConnected()):
self.disconnect()

# t a duration between start and end time in seconds to be formatted in a string as ms
@staticmethod
def formatMS(start, end):
Expand Down Expand Up @@ -325,7 +329,7 @@ def connect(self):
time.sleep(.5) # avoid possible hickups on startup
self.aw.sendmessage(QApplication.translate('Message', 'Connected via MODBUS'))
else:
_log.debug('connect(): failed to connect')
self.aw.qmc.adderror(QApplication.translate('Error Message','Modbus Error: failed to connect'))
except Exception as ex: # pylint: disable=broad-except
_log.exception(ex)
_, _, exc_tb = sys.exc_info()
Expand Down Expand Up @@ -480,8 +484,7 @@ def readActiveRegisters(self):

except Exception as ex: # pylint: disable=broad-except
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# import traceback
# traceback.print_exc(file=sys.stdout)
# _, _, exc_tb = sys.exc_info()
Expand All @@ -506,8 +509,7 @@ def writeCoils(self,slave,register,values):
except Exception as ex: # pylint: disable=broad-except
_log.info('writeCoils(%d,%d,%s)', slave, register, values)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# import traceback
# traceback.print_exc(file=sys.stdout)
_, _, exc_tb = sys.exc_info()
Expand All @@ -529,8 +531,7 @@ def writeCoil(self,slave,register,value):
except Exception as ex: # pylint: disable=broad-except
_log.info('writeCoil(%d,%d,%s) failed', slave, register, value)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeCoil() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand Down Expand Up @@ -567,8 +568,7 @@ def writeSingleRegister(self,slave,register,value):
# _logger.debug("writeSingleRegister exception: %s" % str(ex))
# import traceback
# traceback.print_exc(file=sys.stdout)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeSingleRegister() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand All @@ -593,8 +593,7 @@ def maskWriteRegister(self,slave,register,and_mask,or_mask):
_log.debug(ex)
# import traceback
# traceback.print_exc(file=sys.stdout)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeMask() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand Down Expand Up @@ -622,8 +621,7 @@ def writeRegisters(self,slave,register,values):
except Exception as ex: # pylint: disable=broad-except
_log.info('writeRegisters(%d,%d,%s) failed', slave, register, values)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeRegisters() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand All @@ -648,8 +646,7 @@ def writeWord(self,slave,register,value):
except Exception as ex: # pylint: disable=broad-except
_log.info('writeWord(%d,%d,%s) failed', slave, register, value)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeWord() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand All @@ -673,8 +670,7 @@ def writeBCD(self,slave,register,value):
except Exception as ex: # pylint: disable=broad-except
_log.info('writeBCD(%d,%d,%s) failed', slave, register, value)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeWord() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand All @@ -699,8 +695,7 @@ def writeLong(self,slave,register,value):
except Exception as ex: # pylint: disable=broad-except
_log.info('writeLong(%d,%d,%s) failed', slave, register, value)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
_, _, exc_tb = sys.exc_info()
if self.aw.qmc.flagon:
self.aw.qmc.adderror((QApplication.translate('Error Message','Modbus Error:') + ' writeLong() {0}').format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
Expand Down Expand Up @@ -762,12 +757,12 @@ def readFloat(self,slave,register,code=3,force=False):
_log.debug(ex)
# import traceback
# traceback.print_exc(file=sys.stdout)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# _, _, exc_tb = sys.exc_info()
# self.aw.qmc.adderror((QApplication.translate("Error Message","Modbus Error:") + " readFloat() {0}").format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
if self.aw.qmc.flagon:
self.aw.qmc.adderror(QApplication.translate('Error Message','Modbus Communication Error'))
self.commError = True
return None
finally:
if self.COMsemaphore.available() < 1:
Expand Down Expand Up @@ -856,8 +851,7 @@ def readBCD(self,slave,register,code=3,force=False):
_log.debug(ex)
# import traceback
# traceback.print_exc(file=sys.stdout)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# _, _, exc_tb = sys.exc_info()
# self.aw.qmc.adderror((QApplication.translate("Error Message","Modbus Error:") + " readBCD() {0}").format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
if self.aw.qmc.flagon:
Expand Down Expand Up @@ -1005,8 +999,7 @@ def readSingleRegister(self,slave,register,code=3,force=False,signed=False):
except Exception as ex: # pylint: disable=broad-except
_log.info('readSingleRegister(%d,%d,%d,%s) failed', slave, register, code, force)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# import traceback
# traceback.print_exc(file=sys.stdout)
# _, _, exc_tb = sys.exc_info()
Expand Down Expand Up @@ -1106,12 +1099,12 @@ def readInt32(self,slave,register,code=3,force=False,signed=False):
_log.debug(ex)
# import traceback
# traceback.print_exc(file=sys.stdout)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# _, _, exc_tb = sys.exc_info()
# self.aw.qmc.adderror((QApplication.translate("Error Message","Modbus Error:") + " readFloat() {0}").format(str(ex)),getattr(exc_tb, 'tb_lineno', '?'))
if self.aw.qmc.flagon:
self.aw.qmc.adderror(QApplication.translate('Error Message','Modbus Communication Error'))
self.commError = True
return None
finally:
if self.COMsemaphore.available() < 1:
Expand Down Expand Up @@ -1205,8 +1198,7 @@ def readBCDint(self,slave,register,code=3,force=False):
except Exception as ex: # pylint: disable=broad-except
_log.info('readBCDint(%d,%d,%d,%s) failed', slave, register, code, force)
_log.debug(ex)
if self.disconnect_on_error:
self.disconnect()
self.disconnectOnError()
# import traceback
# traceback.print_exc(file=sys.stdout)
# _, _, exc_tb = sys.exc_info()
Expand Down

0 comments on commit e7f091c

Please sign in to comment.