Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def verify_readonly_register(instr):
ADDRESS_FIRMWARE_VERSION = 0x102F
NEW_FIRMWARE_VERSION = 300
_print_out("Verify detecting a READONLY register")
did_report_error = False
try:
instr.write_register(ADDRESS_FIRMWARE_VERSION, NEW_FIRMWARE_VERSION)
except minimalmodbus.SlaveReportedException:
did_report_error = True
if not did_report_error:
raise ValueError("Failed to detect READONLY register")
_print_out("Passed test for READONLY register\n")
The response is in RTU format, but the checksum might be one or two bytes
depending on whether it was sent in RTU or ASCII mode.
Checking of type and length of the response should be done before calling
this functions.
Raises:
SlaveReportedException or subclass
"""
NON_ERRORS = [5]
SLAVE_ERRORS = {
1: IllegalRequestError("Slave reported illegal function"),
2: IllegalRequestError("Slave reported illegal data address"),
3: IllegalRequestError("Slave reported illegal data value"),
4: SlaveReportedException("Slave reported device failure"),
6: SlaveDeviceBusyError("Slave reported device busy"),
7: NegativeAcknowledgeError("Slave reported negative acknowledge"),
8: SlaveReportedException("Slave reported memory parity error"),
10: SlaveReportedException("Slave reported gateway path unavailable"),
11: SlaveReportedException(
"Slave reported gateway target device failed to respond"
),
}
if len(response) < _BYTEPOSITION_FOR_SLAVE_ERROR_CODE + 1:
return # This check is also done before calling, do not raise exception here.
received_functioncode = ord(response[_BYTEPOSITION_FOR_FUNCTIONCODE])
if _check_bit(received_functioncode, _BITNUMBER_FUNCTIONCODE_ERRORINDICATION):
slave_error_code = ord(response[_BYTEPOSITION_FOR_SLAVE_ERROR_CODE])
}
if len(response) < _BYTEPOSITION_FOR_SLAVE_ERROR_CODE + 1:
return # This check is also done before calling, do not raise exception here.
received_functioncode = ord(response[_BYTEPOSITION_FOR_FUNCTIONCODE])
if _check_bit(received_functioncode, _BITNUMBER_FUNCTIONCODE_ERRORINDICATION):
slave_error_code = ord(response[_BYTEPOSITION_FOR_SLAVE_ERROR_CODE])
if slave_error_code in NON_ERRORS:
return
error = SLAVE_ERRORS.get(
slave_error_code,
SlaveReportedException(
"Slave reported error code " + str(slave_error_code)
),
)
raise error
this functions.
Raises:
SlaveReportedException or subclass
"""
NON_ERRORS = [5]
SLAVE_ERRORS = {
1: IllegalRequestError("Slave reported illegal function"),
2: IllegalRequestError("Slave reported illegal data address"),
3: IllegalRequestError("Slave reported illegal data value"),
4: SlaveReportedException("Slave reported device failure"),
6: SlaveDeviceBusyError("Slave reported device busy"),
7: NegativeAcknowledgeError("Slave reported negative acknowledge"),
8: SlaveReportedException("Slave reported memory parity error"),
10: SlaveReportedException("Slave reported gateway path unavailable"),
11: SlaveReportedException(
"Slave reported gateway target device failed to respond"
),
}
if len(response) < _BYTEPOSITION_FOR_SLAVE_ERROR_CODE + 1:
return # This check is also done before calling, do not raise exception here.
received_functioncode = ord(response[_BYTEPOSITION_FOR_FUNCTIONCODE])
if _check_bit(received_functioncode, _BITNUMBER_FUNCTIONCODE_ERRORINDICATION):
slave_error_code = ord(response[_BYTEPOSITION_FOR_SLAVE_ERROR_CODE])
if slave_error_code in NON_ERRORS:
return
Checking of type and length of the response should be done before calling
this functions.
Raises:
SlaveReportedException or subclass
"""
NON_ERRORS = [5]
SLAVE_ERRORS = {
1: IllegalRequestError("Slave reported illegal function"),
2: IllegalRequestError("Slave reported illegal data address"),
3: IllegalRequestError("Slave reported illegal data value"),
4: SlaveReportedException("Slave reported device failure"),
6: SlaveDeviceBusyError("Slave reported device busy"),
7: NegativeAcknowledgeError("Slave reported negative acknowledge"),
8: SlaveReportedException("Slave reported memory parity error"),
10: SlaveReportedException("Slave reported gateway path unavailable"),
11: SlaveReportedException(
"Slave reported gateway target device failed to respond"
),
}
if len(response) < _BYTEPOSITION_FOR_SLAVE_ERROR_CODE + 1:
return # This check is also done before calling, do not raise exception here.
received_functioncode = ord(response[_BYTEPOSITION_FOR_FUNCTIONCODE])
if _check_bit(received_functioncode, _BITNUMBER_FUNCTIONCODE_ERRORINDICATION):
slave_error_code = ord(response[_BYTEPOSITION_FOR_SLAVE_ERROR_CODE])
if slave_error_code in NON_ERRORS:
return
Raises:
SlaveReportedException or subclass
"""
NON_ERRORS = [5]
SLAVE_ERRORS = {
1: IllegalRequestError("Slave reported illegal function"),
2: IllegalRequestError("Slave reported illegal data address"),
3: IllegalRequestError("Slave reported illegal data value"),
4: SlaveReportedException("Slave reported device failure"),
6: SlaveDeviceBusyError("Slave reported device busy"),
7: NegativeAcknowledgeError("Slave reported negative acknowledge"),
8: SlaveReportedException("Slave reported memory parity error"),
10: SlaveReportedException("Slave reported gateway path unavailable"),
11: SlaveReportedException(
"Slave reported gateway target device failed to respond"
),
}
if len(response) < _BYTEPOSITION_FOR_SLAVE_ERROR_CODE + 1:
return # This check is also done before calling, do not raise exception here.
received_functioncode = ord(response[_BYTEPOSITION_FOR_FUNCTIONCODE])
if _check_bit(received_functioncode, _BITNUMBER_FUNCTIONCODE_ERRORINDICATION):
slave_error_code = ord(response[_BYTEPOSITION_FOR_SLAVE_ERROR_CODE])
if slave_error_code in NON_ERRORS:
return
error = SLAVE_ERRORS.get(
# Exceptions #
# ########## #
class ModbusException(IOError):
"""Base class for Modbus communication exceptions.
Inherits from IOError, which is an alias for OSError in Python3.
"""
class SlaveReportedException(ModbusException):
"""Base class for exceptions that the slave (instrument) reports."""
class SlaveDeviceBusyError(SlaveReportedException):
"""The slave is busy processing some command."""
class NegativeAcknowledgeError(SlaveReportedException):
"""The slave can not fulfil the programming request.
This typically happens when using function code 13 or 14 decimal.
"""
class IllegalRequestError(SlaveReportedException):
"""The slave has received an illegal request."""
class MasterReportedException(ModbusException):
"""Base class for exceptions that the master (computer) detects."""
class ModbusException(IOError):
"""Base class for Modbus communication exceptions.
Inherits from IOError, which is an alias for OSError in Python3.
"""
class SlaveReportedException(ModbusException):
"""Base class for exceptions that the slave (instrument) reports."""
class SlaveDeviceBusyError(SlaveReportedException):
"""The slave is busy processing some command."""
class NegativeAcknowledgeError(SlaveReportedException):
"""The slave can not fulfil the programming request.
This typically happens when using function code 13 or 14 decimal.
"""
class IllegalRequestError(SlaveReportedException):
"""The slave has received an illegal request."""
class MasterReportedException(ModbusException):
"""Base class for exceptions that the master (computer) detects."""
class NoResponseError(MasterReportedException):
"""No response from the slave."""
class SlaveReportedException(ModbusException):
"""Base class for exceptions that the slave (instrument) reports."""
class SlaveDeviceBusyError(SlaveReportedException):
"""The slave is busy processing some command."""
class NegativeAcknowledgeError(SlaveReportedException):
"""The slave can not fulfil the programming request.
This typically happens when using function code 13 or 14 decimal.
"""
class IllegalRequestError(SlaveReportedException):
"""The slave has received an illegal request."""
class MasterReportedException(ModbusException):
"""Base class for exceptions that the master (computer) detects."""
class NoResponseError(MasterReportedException):
"""No response from the slave."""
class LocalEchoError(MasterReportedException):
"""There is some problem with the local echo."""
class InvalidResponseError(MasterReportedException):