Modbus flex getter + buffer parser = true

Managed to make a subflow that combines modbus read requests with the brilliant buffer parser!


Which means every read request can now be simplified to this format:

msg.payload = {
  fc: 4,
  value: 0, // optional, default 0
  byteswap: [], // optional, default []
  quantity: 1, // optional, default is automatically calculated from addresses
  startAddressModifier: -1, // optional, default 0
  address: [
    [
      { name: "dc_voltage_1",                 type: "int16be",  address: 3022, scale: 0.1,  },
      { name: "dc_current_1",                 type: "int16be",  address: 3023, scale: 0.1,  },
      { name: "dc_voltage_2",                 type: "int16be",  address: 3024, scale: 0.1,  },
      { name: "dc_current_2",                 type: "int16be",  address: 3025, scale: 0.1,  },
    ],
    [
      { name: "fault_code_01",                type: "int16be",  address: 3096,              },
      { name: "fault_code_02",                type: "int16be",  address: 3097,              },
    ]
  ]
};

Some optional args here that is best to skip unless needed, so all I really need to make a modbus read request is this:

msg.payload = {
  fc: 4,
  startAddressModifier: -1, // optional, default 0
  address: [
    [
      { name: "dc_voltage_1",                 type: "int16be",  address: 3022, scale: 0.1,  },
      { name: "dc_current_1",                 type: "int16be",  address: 3023, scale: 0.1,  },
      { name: "dc_voltage_2",                 type: "int16be",  address: 3024, scale: 0.1,  },
      { name: "dc_current_2",                 type: "int16be",  address: 3025, scale: 0.1,  },
    ],
    [
      { name: "fault_code_01",                type: "int16be",  address: 3096,              },
      { name: "fault_code_02",                type: "int16be",  address: 3097,              },
    ]
  ]
};

The payload above will result in 2 separate queries, everything not specified here will be handled automatically. Such as quantities and offsets. Finally, all successfull responses are merged into a single object, so you end up with something like this:

msg.payload: {
  dc_voltage_1: 12,
  dc_current_1: 34,
  dc_voltage_2: 56,
  dc_current_2: 78,
  fault_code_01: 1,
  fault_code_02: 2,
}

Now I'm not a modbus expert, but the standard way of going about specifying start address, quantity, then get the list of bytes to be sent to buffer parser, only to again deal with offset seems much more complicated. I think the original modbus flex-getter would benefit greatly from incorporating the buffer parser. Perhaps there is a need to keep them separated, but so far all of my usage can be summarized by the examples shown here.

1 Like

Glad you like it. I'm rather proud of it. It's not perfect but it is flexible. I think this is one of the first posts I've seen where it's being used programmatically. Cool.

1 Like

Would it be possible to get a list of how many bytes each type is?

Noticed that buffer parser doesn't support scale. It will be something I add on top, so if type is bigint, and scale is not default "1", then I move scale to scaleBig. Reset scale to "1". And after buffer parser, check if scaleBig exists, then do scale there. With the normal cautions for bigint.