Page 1 of 1

Write modbus register only when I need

PostPosted: Fri Nov 01, 2024 9:51 am
by Doncis
I have bunch of registers, eg. Vfd vf parameters for heat control and energy saving ( low torq high speed engraving, high power milling), SVC on off, servo drive gains, IO expanders and etc. How do i set registers one time only when i need it? For now all registers are constantly writing in loop, even if value is the same, and no need to constantly writing on top.

Re: Write modbus register only when I need

PostPosted: Thu Nov 14, 2024 2:03 am
by densa
Keep a local cache (in memory) of the current values of each register.
Before writing to the register, compare the current value you want to write with the cached value.
If the value has changed, write the new value to the register and update the local cache.
If the value is the same, skip the write operation.
Code: Select all
# Dictionary to hold the last known values of the registers
local_cache = {}

def update_register(register, new_value):
    # Check if the register value has changed
    if register not in local_cache or local_cache[register] != new_value:
        # Update the hardware register if the value has changed
        write_to_hardware_register(register, new_value)
        # Update the local cache with the new value
        local_cache[register] = new_value