class DictPropertyTester(object): def __init__(self): self.test = {1: 'a'} self._test = {2: 'b'} self.__test = {3: 'c'} l1 = dict_property(lambda s: s.test, 1) l2 = dict_property(lambda s: s._test, 2) l3 = dict_property(lambda s: s.__test, 3) s1 = dict_property('test', 1) s2 = dict_property('_test', 2) g1 = dict_property(_test_master, 4)
class ModbusCountersHandler(object): """ This is a helper class to simplify the properties for the counters:: 0x0B 1 Return Bus Message Count Quantity of messages that the remote device has detected on the communications system since its last restart, clear counters operation, or power-up. Messages with bad CRC are not taken into account. 0x0C 2 Return Bus Communication Error Count Quantity of CRC errors encountered by the remote device since its last restart, clear counters operation, or power-up. In case of an error detected on the character level, (overrun, parity error), or in case of a message length < 3 bytes, the receiving device is not able to calculate the CRC. In such cases, this counter is also incremented. 0x0D 3 Return Slave Exception Error Count Quantity of MODBUS exception error detected by the remote device since its last restart, clear counters operation, or power-up. It comprises also the error detected in broadcast messages even if an exception message is not returned in this case. Exception errors are described and listed in "MODBUS Application Protocol Specification" document. 0xOE 4 Return Slave Message Count Quantity of messages addressed to the remote device, including broadcast messages, that the remote device has processed since its last restart, clear counters operation, or power-up. 0x0F 5 Return Slave No Response Count Quantity of messages received by the remote device for which it returned no response (neither a normal response nor an exception response), since its last restart, clear counters operation, or power-up. Then, this counter counts the number of broadcast messages it has received. 0x10 6 Return Slave NAK Count Quantity of messages addressed to the remote device for which it returned a Negative Acknowledge (NAK) exception response, since its last restart, clear counters operation, or power-up. Exception responses are described and listed in "MODBUS Application Protocol Specification" document. 0x11 7 Return Slave Busy Count Quantity of messages addressed to the remote device for which it returned a Slave Device Busy exception response, since its last restart, clear counters operation, or power-up. Exception responses are described and listed in "MODBUS Application Protocol Specification" document. 0x12 8 Return Bus Character Overrun Count Quantity of messages addressed to the remote device that it could not handle due to a character overrun condition, since its last restart, clear counters operation, or power-up. A character overrun is caused by data characters arriving at the port faster than they can. .. note:: I threw the event counter in here for convenience """ __data = dict([(i, 0x0000) for i in range(9)]) __names = [ 'BusMessage', 'BusCommunicationError', 'SlaveExceptionError', 'SlaveMessage', 'SlaveNoResponse', 'SlaveNAK', 'SlaveBusy', 'BusCharacterOverrun' 'Event ' ] def __iter__(self): """ Iterate over the device counters :returns: An iterator of the device counters """ return zip(self.__names, self.__data.values()) def update(self, values): """ Update the values of this identity using another identify as the value :param values: The value to copy values from """ for k, v in values.items(): v += self.__getattribute__(k) self.__setattr__(k, v) def reset(self): """ This clears all of the system counters """ self.__data = dict([(i, 0x0000) for i in range(9)]) def summary(self): """ Returns a summary of the counters current status :returns: A byte with each bit representing each counter """ count, result = 0x01, 0x00 for i in self.__data.values(): if i != 0x00: result |= count count <<= 1 return result # region Properties BusMessage = dict_property(lambda s: s.__data, 0) BusCommunicationError = dict_property(lambda s: s.__data, 1) BusExceptionError = dict_property(lambda s: s.__data, 2) SlaveMessage = dict_property(lambda s: s.__data, 3) SlaveNoResponse = dict_property(lambda s: s.__data, 4) SlaveNAK = dict_property(lambda s: s.__data, 5) SlaveBusy = dict_property(lambda s: s.__data, 6) BusCharacterOverrun = dict_property(lambda s: s.__data, 7) Event = dict_property(lambda s: s.__data, 8) # endregion pass
class ModbusDeviceIdentification(object): """ This is used to supply the device identification for the readDeviceIdentification function For more information read section 6.21 of the modbus application protocol. """ __data = { 0x00: '', # VendorName 0x01: '', # ProductCode 0x02: '', # MajorMinorRevision 0x03: '', # VendorUrl 0x04: '', # ProductName 0x05: '', # ModelName 0x06: '', # UserApplicationName 0x07: '', # reserved 0x08: '', # reserved # 0x80 -> 0xFF are private } __names = [ 'VendorName', 'ProductCode', 'MajorMinorRevision', 'VendorUrl', 'ProductName', 'ModelName', 'UserApplicationName', ] def __init__(self, info=None): """ Initialize the datastore with the elements you need. (note acceptable range is [0x00-0x06,0x80-0xFF] inclusive) :param info: A dictionary of {int:string} of values """ if isinstance(info, dict): for key in info: if (0x06 >= key >= 0x00) or (0x80 > key > 0x08): self.__data[key] = info[key] def __iter__(self): """ Iterate over the device information :returns: An iterator of the device information """ return self.__data.items() def summary(self): """ Return a summary of the main items :returns: An dictionary of the main items """ return dict(zip(self.__names, self.__data.values())) def update(self, value): """ Update the values of this identity using another identify as the value :param value: The value to copy values from """ self.__data.update(value) def __setitem__(self, key, value): """ Wrapper used to access the device information :param key: The register to set :param value: The new value for referenced register """ if key not in [0x07, 0x08]: self.__data[key] = value def __getitem__(self, key): """ Wrapper used to access the device information :param key: The register to read """ return self.__data.setdefault(key, '') def __str__(self): """ Build a representation of the device :returns: A string representation of the device """ return 'DeviceIdentity' # region Properties VendorName = dict_property(lambda s: s.__data, 0) ProductCode = dict_property(lambda s: s.__data, 1) MajorMinorRevision = dict_property(lambda s: s.__data, 2) VendorUrl = dict_property(lambda s: s.__data, 3) ProductName = dict_property(lambda s: s.__data, 4) ModelName = dict_property(lambda s: s.__data, 5) UserApplicationName = dict_property(lambda s: s.__data, 6) # endregion pass