| 1 | # modbus_mapping_new |
| 2 | |
| 3 | ## Name |
| 4 | |
| 5 | modbus_mapping_new - allocate four arrays of bits and registers |
| 6 | |
| 7 | ## Synopsis |
| 8 | |
| 9 | ```c |
| 10 | modbus_mapping_t* modbus_mapping_new(int nb_bits, int nb_input_bits, int nb_registers, int nb_input_registers); |
| 11 | ``` |
| 12 | |
| 13 | ## Description |
| 14 | |
| 15 | The *modbus_mapping_new()* function shall allocate four arrays to store bits, |
| 16 | input bits, registers and inputs registers. The pointers are stored in |
| 17 | *modbus_mapping_t* structure. All values of the arrays are initialized to zero. |
| 18 | |
| 19 | This function is equivalent to a call of the |
| 20 | [modbus_mapping_new_start_address](modbus_mapping_new_start_address.md) function |
| 21 | with all start addresses to `0`. |
| 22 | |
| 23 | If it isn't necessary to allocate an array for a specific type of data, you can |
| 24 | pass the zero value in argument, the associated pointer will be NULL. |
| 25 | |
| 26 | This function is convenient to handle requests in a Modbus server/slave. |
| 27 | |
| 28 | ## Return value |
| 29 | |
| 30 | The function shall return the new allocated structure if successful. Otherwise |
| 31 | it shall return NULL and set errno. |
| 32 | |
| 33 | ## Errors |
| 34 | |
| 35 | - *EINVAL*, one of the requested array dimensions exceeds 65536 entries. |
| 36 | - *ENOMEM*, not enough memory. |
| 37 | |
| 38 | ## Example |
| 39 | |
| 40 | ```c |
| 41 | /* The first value of each array is accessible from the 0 address. */ |
| 42 | mb_mapping = modbus_mapping_new( |
| 43 | BITS_ADDRESS + BITS_NB, |
| 44 | INPUT_BITS_ADDRESS + INPUT_BITS_NB, |
| 45 | REGISTERS_ADDRESS + REGISTERS_NB, |
| 46 | INPUT_REGISTERS_ADDRESS + INPUT_REGISTERS_NB |
| 47 | ); |
| 48 | if (mb_mapping == NULL) { |
| 49 | fprintf( |
| 50 | stderr, "Failed to allocate the mapping: %s\n", |
| 51 | modbus_strerror(errno) |
| 52 | ); |
| 53 | modbus_free(ctx); |
| 54 | return -1; |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ## See also |
| 59 | |
| 60 | - [modbus_mapping_free](modbus_mapping_free.md) |
| 61 | - [modbus_mapping_new_start_address](modbus_mapping_new_start_address.md) |