def write_registers(self, start_register_address, write_values): """ Modbus command : Write data to multiple registers (function code = 16) Argument : start_register_address (int16) : Start address where to write a data write_values (int16) : Write data(s) """ # Enable byte system of modbus to be signed-value type if self.signed_type == True: conf.SIGNED_VALUES = True else: conf.SIGNED_VALUES = False # Generate modbus TCP message try: message = modbus_tcp.write_multiple_registers( slave_id=self.device_id, starting_address=start_register_address, values=write_values) except Exception as e: print("Error during generate modbus message.") print( "\tMaybe, write value is less than 0 as signed integer and .signed_type is set to 'False'." ) # Send message via TCP socket try: self._connect() response = modbus_tcp.send_message(message, self.tcp_socket) print("response={}".format(response)) self._disconnect() except Exception as e: print("Error during send modbus message.") print(e)
def write_holding_registers(self, starting_address, values): with self.__lock: message = tcp.write_multiple_registers( slave_id=1, starting_address=starting_address - 1, values=values) self.__send_modbus_message(message)
def write(self): message = tcp.write_multiple_registers(slave_id=1, starting_address=0, values=self._output_regs) # time.sleep(.5) while True: try: print(self._output_regs) print(message) try: response = tcp.send_message(message, self.client) except ValueError as ve: print(response) break except OSError as e: print('No he poodido leer. Error {e}') while True: try: self.client.connect((self.ip, self.port)) print('Socket conectado') break except OSError as e: print('No he poodido conectar. Error {e}') time.sleep(5) continue os.system('clear') print(Text.from_markup(str(self)))
def write_multiple_reg(self, starting_addr, values, slave_id=None): slv_id = self.default_slave_id if slave_id is None else slave_id for starting_idx, ending_idx in self.get_chunk_indices(values, 123): request_adu = tcp.write_multiple_registers( slv_id, starting_addr + starting_idx, values[starting_idx:ending_idx]) tcp.send_message(request_adu, self.socket)
def gateway_config_write_read(self,add, value): config_id = 4 message = tcp.write_multiple_registers(slave_id=config_id, starting_address=add, values=[value]) response = tcp.send_message(message, self.sock) time.sleep(0.1) message = tcp.read_holding_registers(config_id, add, 1) response = tcp.send_message(message, self.sock) print("..") if response[0] != value: raise ValueError("Value not writen!")
def write_hr(self, new_value): if self.bit is not None: message = tcp.read_holding_registers(slave_id=self.slave_id, starting_address=self.addr, quantity=1) response = self.server.send_message(message) current_value = response[0] if new_value: new_value = current_value | 1 << self.bit else: new_value = current_value & ~(1 << self.bit) message = tcp.write_multiple_registers(slave_id=self.slave_id, starting_address=self.addr, values=[int(new_value)]) response = self.server.send_message(message) return response
def write_registers(self, address, values=None): """ Low level register access. Performs a modbus write registers request to the MAQ20 :param address: starting address. :param values: list(int) [-32767, 32767] or a str :return: modbus response. """ ints = [] if type(values) is str: for c in values: ints.append(ord(c)) else: ints = [signed16_to_unsigned16(x) for x in values] if MODBUS_BACKEND == pymodbus: return self._client.write_registers(address, ints) if MODBUS_BACKEND == UMODBUS: message = tcp.write_multiple_registers(slave_id=0, starting_address=address, values=ints) return tcp.send_message(message, self._sock)
def writeWords(self,address, values): message = tcp.write_multiple_registers(self.slaveNbr, address, values) response = self.__doRequest(message) return response
async def write(self, client): message = tcp.write_multiple_registers(slave_id=1, starting_address=0, values=self._output_regs) response = tcp.send_message(message, client)