MODBUS_MAPPING_NEW(3)

πŸ“¦
version 3.1.6 Β· 3 versions
, in debian bookworm

πŸ’– MODBUS_MAPPING_NEW πŸ’–

okay bestie, welcome to the libmodbus Manual β€” this is the MODBUS_MAPPING_NEW(3) page and it's literally the moment your server stops being a fake and starts holding actual data. sit down, grab a strawberry πŸ“, we're doing this.


✨ NAME ✨

modbus_mapping_new β€” allocate four arrays of bits and registers πŸŽ€


🌸 SYNOPSIS 🌸

modbus_mapping_t modbus_mapping_new(int nb_bits, int nb_input_bits, int
nb_registers, int nb_input_registers);*

πŸ¦‹ DESCRIPTION πŸ¦‹

so the way that modbus_mapping_new() works is: it allocates four arrays, and they're for bits, input bits, registers and input registers. the pointers to all of them get stored in the modbus_mapping_t structure. and every single value in those arrays? initialized to zero. clean slate, no crumbs, periodt.

this function is equivalent to calling modbus_mapping_new_start_address(3) with all the start addresses set to 0. like, same energy, just the shortcut version. πŸ’…

and listen β€” if you don't actually need an array for one specific type of data, you can just pass the zero value in that argument and the associated pointer will be NULL. no drama, no wasted memory, we love a minimalist queen. ✨

this function is convenient to handle requests in a Modbus server/slave. that's the whole vibe. it's the thing you call so your server can actually answer people instead of ghosting them. πŸ™ˆ


πŸ‘‘ RETURN VALUE πŸ‘‘

if it slays, the function shall return the new allocated structure. 🫢

if it does not slay? it shall return NULL and set errno. that's your cue to go check what went wrong, bestie. 😭


πŸ’€ ERRORS πŸ’€

  • ENOMEM β€” Not enough memory. there is no more room in the club, the bouncer said no. πŸ’€

πŸ”₯ EXAMPLE πŸ”₯

/* The first value of each array is accessible from the 0 address. */
mb_mapping = modbus_mapping_new(BITS_ADDRESS + BITS_NB,
                                INPUT_BITS_ADDRESS + INPUT_BITS_NB,
                                REGISTERS_ADDRESS + REGISTERS_NB,
                                INPUT_REGISTERS_ADDRESS + INPUT_REGISTERS_NB);
if (mb_mapping == NULL) {
    fprintf(stderr, "Failed to allocate the mapping: %s\n",
            modbus_strerror(errno));
    modbus_free(ctx);
    return -1;
}

like look at that error handling, we check for NULL and everything. main character energy. 🌈


πŸŽ€ SEE ALSO πŸŽ€

go read those too, iykyk. πŸ’œ


πŸ’– AUTHORS πŸ’–

The libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com> β€” thank you for the lore, icon. πŸ‘‘


and that's the page fr fr πŸ’… this was libmodbus v3.1.6, dated 11/17/2025, and it's the MODBUS_MAPPING_NEW(3) page β€” go allocate something iconic, bestie. πŸ¦‹βœ¨πŸŒΈ
πŸ“¦ where this page comes from