MODBUS_MAPPING_NEW_S(3)
β¨ MODBUS_MAPPING_NEW_S(3) β¨
okay bestie, coming to you live from the libmodbus Manual π β this is the page for modbus_mapping_new_start_address, and it's genuinely such a glow-up, no cap π
π NAME π
modbus_mapping_new_start_address β allocate four arrays of bits and registers accessible from their starting addresses
so like... four arrays, each one can start wherever you want. iconic behavior honestly πΈ
π SYNOPSIS π
modbus_mapping_t modbus_mapping_new_start_address(int start_bits, int
nb_bits, int start_input_bits, int nb_input_bits, int start_registers,
int nb_registers, int start_input_registers, int nb_input_registers);*
(yes the * is really there at the end, we don't question the ancients π«Ά)
π DESCRIPTION π
The modbus_mapping_new_start_address() function shall allocate four arrays to store bits, input bits, registers and inputs registers. The pointers are stored in modbus_mapping_t structure. All values of the arrays are initialized to zero.
so you get all four, all zeroed out, and the pointers live in the modbus_mapping_t struct. clean. β¨
The different starting addresses make it possible to place the mapping at any address in each address space. This way, you can give access to values stored at high addresses without allocating memory from the address zero, for eg. to make available registers from 10000 to 10009, you can use:
mb_mapping = modbus_mapping_new_start_address(0, 0, 0, 0, 10000, 10, 0, 0);
With this code, only 10 registers (uint16_t) are allocated.
THE WAY THAT you skip the whole address-zero allocation and just vibe up at 10000 with only 10 registers of actual memory?? that's the resource efficiency bestie, we love to see it πβ¨
If it isn't necessary to allocate an array for a specific type of data, you can pass the zero value in argument, the associated pointer will be NULL.
no data type needed? pass zero, get NULL back. simple as. π
This function is convenient to handle requests in a Modbus server/slave.
and that's the whole point β it's the move for handling requests in a Modbus server/slave π
π¦ RETURN VALUE π¦
The modbus_mapping_new_start_address() function shall return the new allocated structure if successful. Otherwise it shall return NULL and set errno.
got it? success = shiny new struct π, failure = NULL + errno set π
π₯ ERRORS π₯
ENOMEMβ Not enough memory
that's it, that's the one error. your RAM said "not today" π
πΈ EXAMPLE πΈ
/* The first value of each array is accessible at the defined address.
The end address is ADDRESS + NB - 1. */
mb_mapping = modbus_mapping_new_start_address(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;
}
π lowkey the most important thing here: the first value of each array is accessible at the defined address, and the end address isADDRESS + NB - 1. do not forget the- 1, i'm begging you π
and yes bestie, always check for NULL and clean up with modbus_free(ctx) before you return -1 β that's just good manners π«Ά
π SEE ALSO π
π AUTHORS π
The libmodbus documentation was written by StΓ©phane Raimbault <stephane.raimbault@gmail.com>
mwah π that's the whole page bestie β this one's from libmodbus v3.1.6, dated 11/17/2025. go allocate responsibly, ily, bye πβ¨ππ¦
π¦ where this page comes from
- debian bookworm: libmodbus-dev 3.1.6-2.1+deb12u1 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms