def get_request(self, protocol=None): """ :param protocol: the supported protocol :return: """ # sets self[KEY_REQ_RAW] and self[KEY_REQ_PROTOCOL] super().get_request(protocol) if protocol is None: # will throw exception, if neither DST or SRC set protocol = self.get_protocol() if protocol == transaction.IA_PROTOCOL_MBASC: result = self._mbasc_get_request() elif protocol == transaction.IA_PROTOCOL_MBRTU: result = self._mbrtu_get_request() elif protocol == transaction.IA_PROTOCOL_MBTCP: result = self._mbtcp_get_request() else: raise transaction.IaBadProtocol( "Unknown IA Protocol:{}".format(protocol)) return result
def set_response(self, data, protocol=None): """ :param bytes data: :param protocol: the supported protocol :return: """ # sets self[KEY_REQ_RAW] and self[KEY_REQ_PROTOCOL] super().set_response(data, protocol) if protocol == transaction.IA_PROTOCOL_MBASC: result = self._mbasc_set_response(data) max_data = 252 elif protocol == transaction.IA_PROTOCOL_MBRTU: result = self._mbrtu_set_response(data) max_data = 252 elif protocol == transaction.IA_PROTOCOL_MBTCP: result = self._mbtcp_set_response(data) max_data = 255 else: raise transaction.IaBadProtocol( "Unknown IA Protocol:{}".format(protocol)) if len(self[self.KEY_RSP]) > max_data: raise ModbusBadForm( "data length () is too long".format(len(self[self.KEY_RSP]))) return result
def get_no_response_error(self, protocol=None): """ return the correct response for a time-out/no answer :param protocol: the supported protocol :return: """ # super().get_response(protocol) if protocol is None: # will throw exception, if neither DST or SRC set protocol = self.get_protocol() if protocol == transaction.IA_PROTOCOL_MBASC: # Modbus/ASCII returns nothing result = None elif protocol == transaction.IA_PROTOCOL_MBRTU: # Modbus/RTU returns nothing result = None elif protocol == transaction.IA_PROTOCOL_MBTCP: # Modbus/TCP, we want the exception 0x0B, so in effect create a # 'fake' self[self.KEY_RSP] # start with the command, set the MSB and append the err code value = [self[self.KEY_REQ][0] | 0x80, 0x0B] self[self.KEY_RSP] = bytes(value) result = self._mbtcp_get_response() else: raise transaction.IaBadProtocol( "Unknown IA Protocol:{}".format(protocol)) return result