Skip to content

Commit

Permalink
Make internal buffer size configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Kober authored and lammermann committed Aug 14, 2023
1 parent 8dafc72 commit 4b1d5a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions nanomodbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,14 +803,14 @@ static nmbs_error handle_read_registers(nmbs_t* nmbs,
return err;

if (!nmbs->msg.ignored) {
if (quantity < 1 || quantity > 125)
if (quantity < 1 || quantity > NMBS_INTERAL_MAX_REGISTER_QUANTITY)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_VALUE);

if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
return send_exception_msg(nmbs, NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS);

if (callback) {
uint16_t regs[125] = {0};
uint16_t regs[NMBS_INTERAL_MAX_REGISTER_QUANTITY] = {0};
err = callback(address, quantity, regs, nmbs->msg.unit_id, nmbs->platform.arg);
if (err != NMBS_ERROR_NONE) {
if (nmbs_error_is_exception(err))
Expand Down Expand Up @@ -1508,7 +1508,7 @@ nmbs_error nmbs_read_discrete_inputs(nmbs_t* nmbs, uint16_t address, uint16_t qu
}

static nmbs_error read_registers(nmbs_t* nmbs, uint8_t fc, uint16_t address, uint16_t quantity, uint16_t* registers) {
if (quantity < 1 || quantity > 125)
if (quantity < 1 || quantity > NMBS_INTERAL_MAX_REGISTER_QUANTITY)
return NMBS_ERROR_INVALID_ARGUMENT;

if ((uint32_t) address + (uint32_t) quantity > ((uint32_t) 0xFFFF) + 1)
Expand Down
7 changes: 6 additions & 1 deletion nanomodbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,18 @@ typedef struct nmbs_callbacks {
char _nonempty; // Struct may become empty, which is undefined behavior
} nmbs_callbacks;

#ifndef NMBS_INTERNAL_BUFFER_SIZE
#define NMBS_INTERNAL_BUFFER_SIZE 260
#endif

#define NMBS_INTERAL_MAX_REGISTER_QUANTITY ((NMBS_INTERNAL_BUFFER_SIZE - 7) / 2) // 7 Extra bytes for MODBUS (tcp) needed

/**
* nanoMODBUS client/server instance type. All struct members are to be considered private, it is not advisable to read/write them directly.
*/
typedef struct nmbs_t {
struct {
uint8_t buf[260];
uint8_t buf[NMBS_INTERNAL_BUFFER_SIZE];
uint16_t buf_idx;

uint8_t unit_id;
Expand Down

0 comments on commit 4b1d5a3

Please sign in to comment.