From a2e16a16b065540c2b40ee0c69a03b78d0d034f4 Mon Sep 17 00:00:00 2001 From: Hannu Vuolasaho Date: Tue, 10 Aug 2010 08:26:47 +0200 Subject: [PATCH] Replace MODBUS_MAX_* by MODBUS_MAX_[READ|WRITE]_* --- src/modbus.c | 28 ++++++++++++++-------------- src/modbus.h | 17 +++++++++++++---- tests/bandwidth-client.c | 12 ++++++------ tests/bandwidth-server-many-up.c | 4 ++-- tests/bandwidth-server-one.c | 4 ++-- tests/unit-test-client.c | 12 ++++++------ 6 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index f2b35d8..1d7b137 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1186,11 +1186,11 @@ int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *data_dest) { int rc; - if (nb > MODBUS_MAX_BITS) { + if (nb > MODBUS_MAX_READ_BITS) { if (ctx->debug) { fprintf(stderr, "ERROR Too many bits requested (%d > %d)\n", - nb, MODBUS_MAX_BITS); + nb, MODBUS_MAX_READ_BITS); } errno = EMBMDATA; return -1; @@ -1210,11 +1210,11 @@ int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *data_dest) { int rc; - if (nb > MODBUS_MAX_BITS) { + if (nb > MODBUS_MAX_READ_BITS) { if (ctx->debug) { fprintf(stderr, "ERROR Too many discrete inputs requested (%d > %d)\n", - nb, MODBUS_MAX_BITS); + nb, MODBUS_MAX_READ_BITS); } errno = EMBMDATA; return -1; @@ -1237,11 +1237,11 @@ static int read_registers(modbus_t *ctx, int function, int addr, int nb, uint8_t req[MIN_REQ_LENGTH]; uint8_t rsp[MAX_MESSAGE_LENGTH]; - if (nb > MODBUS_MAX_REGISTERS) { + if (nb > MODBUS_MAX_READ_REGISTERS) { if (ctx->debug) { fprintf(stderr, "ERROR Too many registers requested (%d > %d)\n", - nb, MODBUS_MAX_REGISTERS); + nb, MODBUS_MAX_READ_REGISTERS); } errno = EMBMDATA; return -1; @@ -1275,11 +1275,11 @@ int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *data_dest) { int status; - if (nb > MODBUS_MAX_REGISTERS) { + if (nb > MODBUS_MAX_READ_REGISTERS) { if (ctx->debug) { fprintf(stderr, "ERROR Too many registers requested (%d > %d)\n", - nb, MODBUS_MAX_REGISTERS); + nb, MODBUS_MAX_READ_REGISTERS); } errno = EMBMDATA; return -1; @@ -1296,10 +1296,10 @@ int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, { int status; - if (nb > MODBUS_MAX_REGISTERS) { + if (nb > MODBUS_MAX_READ_REGISTERS) { fprintf(stderr, "ERROR Too many input registers requested (%d > %d)\n", - nb, MODBUS_MAX_REGISTERS); + nb, MODBUS_MAX_READ_REGISTERS); errno = EMBMDATA; return -1; } @@ -1365,10 +1365,10 @@ int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data_src) uint8_t req[MAX_MESSAGE_LENGTH]; - if (nb > MODBUS_MAX_BITS) { + if (nb > MODBUS_MAX_WRITE_BITS) { if (ctx->debug) { fprintf(stderr, "ERROR Writing too many bits (%d > %d)\n", - nb, MODBUS_MAX_BITS); + nb, MODBUS_MAX_WRITE_BITS); } errno = EMBMDATA; return -1; @@ -1416,11 +1416,11 @@ int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data uint8_t req[MAX_MESSAGE_LENGTH]; - if (nb > MODBUS_MAX_REGISTERS) { + if (nb > MODBUS_MAX_WRITE_REGISTERS) { if (ctx->debug) { fprintf(stderr, "ERROR Trying to write to too many registers (%d > %d)\n", - nb, MODBUS_MAX_REGISTERS); + nb, MODBUS_MAX_WRITE_REGISTERS); } errno = EMBMDATA; return -1; diff --git a/src/modbus.h b/src/modbus.h index 367d973..852b231 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -78,14 +78,23 @@ MODBUS_BEGIN_DECLS #define MODBUS_MAX_ADU_LENGTH_TCP 260 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12) - * Quantity of Coils (2 bytes): 1 to 2000 (0x7D0) + * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0) + * (chapter 6 section 11 page 29) + * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0) */ -#define MODBUS_MAX_BITS 2000 +#define MODBUS_MAX_READ_BITS 2000 +#define MODBUS_MAX_WRITE_BITS 1968 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15) - * Quantity of Registers (2 bytes): 1 to 125 (0x7D) + * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D) + * (chapter 6 section 12 page 31) + * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B) + * (chapter 6 section 17 page 38) + * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79) */ -#define MODBUS_MAX_REGISTERS 125 +#define MODBUS_MAX_READ_REGISTERS 125 +#define MODBUS_MAX_WRITE_REGISTERS 123 +#define MODBUS_MAX_RW_WRITE_REGISTERS 121 /* Random number to avoid errno conflicts */ #define MODBUS_ENOBASE 112345678 diff --git a/tests/bandwidth-client.c b/tests/bandwidth-client.c index f1216e2..f562018 100644 --- a/tests/bandwidth-client.c +++ b/tests/bandwidth-client.c @@ -62,16 +62,16 @@ int main(void) } /* Allocate and initialize the memory to store the status */ - tab_bit = (uint8_t *) malloc(MODBUS_MAX_BITS * sizeof(uint8_t)); - memset(tab_bit, 0, MODBUS_MAX_BITS * sizeof(uint8_t)); + tab_bit = (uint8_t *) malloc(MODBUS_MAX_READ_BITS * sizeof(uint8_t)); + memset(tab_bit, 0, MODBUS_MAX_READ_BITS * sizeof(uint8_t)); /* Allocate and initialize the memory to store the registers */ - tab_reg = (uint16_t *) malloc(MODBUS_MAX_REGISTERS * sizeof(uint16_t)); - memset(tab_reg, 0, MODBUS_MAX_REGISTERS * sizeof(uint16_t)); + tab_reg = (uint16_t *) malloc(MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t)); + memset(tab_reg, 0, MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t)); printf("READ BITS\n\n"); - nb_points = MODBUS_MAX_BITS; + nb_points = MODBUS_MAX_READ_BITS; start = gettime_ms(); for (i=0; i