Commit a2e16a16b065540c2b40ee0c69a03b78d0d034f4

Authored by Hannu Vuolasaho
Committed by Stéphane Raimbault
1 parent f33e5b9f

Replace MODBUS_MAX_* by MODBUS_MAX_[READ|WRITE]_*

src/modbus.c
@@ -1186,11 +1186,11 @@ int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *data_dest) @@ -1186,11 +1186,11 @@ int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *data_dest)
1186 { 1186 {
1187 int rc; 1187 int rc;
1188 1188
1189 - if (nb > MODBUS_MAX_BITS) { 1189 + if (nb > MODBUS_MAX_READ_BITS) {
1190 if (ctx->debug) { 1190 if (ctx->debug) {
1191 fprintf(stderr, 1191 fprintf(stderr,
1192 "ERROR Too many bits requested (%d > %d)\n", 1192 "ERROR Too many bits requested (%d > %d)\n",
1193 - nb, MODBUS_MAX_BITS); 1193 + nb, MODBUS_MAX_READ_BITS);
1194 } 1194 }
1195 errno = EMBMDATA; 1195 errno = EMBMDATA;
1196 return -1; 1196 return -1;
@@ -1210,11 +1210,11 @@ int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *data_dest) @@ -1210,11 +1210,11 @@ int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *data_dest)
1210 { 1210 {
1211 int rc; 1211 int rc;
1212 1212
1213 - if (nb > MODBUS_MAX_BITS) { 1213 + if (nb > MODBUS_MAX_READ_BITS) {
1214 if (ctx->debug) { 1214 if (ctx->debug) {
1215 fprintf(stderr, 1215 fprintf(stderr,
1216 "ERROR Too many discrete inputs requested (%d > %d)\n", 1216 "ERROR Too many discrete inputs requested (%d > %d)\n",
1217 - nb, MODBUS_MAX_BITS); 1217 + nb, MODBUS_MAX_READ_BITS);
1218 } 1218 }
1219 errno = EMBMDATA; 1219 errno = EMBMDATA;
1220 return -1; 1220 return -1;
@@ -1237,11 +1237,11 @@ static int read_registers(modbus_t *ctx, int function, int addr, int nb, @@ -1237,11 +1237,11 @@ static int read_registers(modbus_t *ctx, int function, int addr, int nb,
1237 uint8_t req[MIN_REQ_LENGTH]; 1237 uint8_t req[MIN_REQ_LENGTH];
1238 uint8_t rsp[MAX_MESSAGE_LENGTH]; 1238 uint8_t rsp[MAX_MESSAGE_LENGTH];
1239 1239
1240 - if (nb > MODBUS_MAX_REGISTERS) { 1240 + if (nb > MODBUS_MAX_READ_REGISTERS) {
1241 if (ctx->debug) { 1241 if (ctx->debug) {
1242 fprintf(stderr, 1242 fprintf(stderr,
1243 "ERROR Too many registers requested (%d > %d)\n", 1243 "ERROR Too many registers requested (%d > %d)\n",
1244 - nb, MODBUS_MAX_REGISTERS); 1244 + nb, MODBUS_MAX_READ_REGISTERS);
1245 } 1245 }
1246 errno = EMBMDATA; 1246 errno = EMBMDATA;
1247 return -1; 1247 return -1;
@@ -1275,11 +1275,11 @@ int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *data_dest) @@ -1275,11 +1275,11 @@ int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *data_dest)
1275 { 1275 {
1276 int status; 1276 int status;
1277 1277
1278 - if (nb > MODBUS_MAX_REGISTERS) { 1278 + if (nb > MODBUS_MAX_READ_REGISTERS) {
1279 if (ctx->debug) { 1279 if (ctx->debug) {
1280 fprintf(stderr, 1280 fprintf(stderr,
1281 "ERROR Too many registers requested (%d > %d)\n", 1281 "ERROR Too many registers requested (%d > %d)\n",
1282 - nb, MODBUS_MAX_REGISTERS); 1282 + nb, MODBUS_MAX_READ_REGISTERS);
1283 } 1283 }
1284 errno = EMBMDATA; 1284 errno = EMBMDATA;
1285 return -1; 1285 return -1;
@@ -1296,10 +1296,10 @@ int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, @@ -1296,10 +1296,10 @@ int modbus_read_input_registers(modbus_t *ctx, int addr, int nb,
1296 { 1296 {
1297 int status; 1297 int status;
1298 1298
1299 - if (nb > MODBUS_MAX_REGISTERS) { 1299 + if (nb > MODBUS_MAX_READ_REGISTERS) {
1300 fprintf(stderr, 1300 fprintf(stderr,
1301 "ERROR Too many input registers requested (%d > %d)\n", 1301 "ERROR Too many input registers requested (%d > %d)\n",
1302 - nb, MODBUS_MAX_REGISTERS); 1302 + nb, MODBUS_MAX_READ_REGISTERS);
1303 errno = EMBMDATA; 1303 errno = EMBMDATA;
1304 return -1; 1304 return -1;
1305 } 1305 }
@@ -1365,10 +1365,10 @@ int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data_src) @@ -1365,10 +1365,10 @@ int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data_src)
1365 1365
1366 uint8_t req[MAX_MESSAGE_LENGTH]; 1366 uint8_t req[MAX_MESSAGE_LENGTH];
1367 1367
1368 - if (nb > MODBUS_MAX_BITS) { 1368 + if (nb > MODBUS_MAX_WRITE_BITS) {
1369 if (ctx->debug) { 1369 if (ctx->debug) {
1370 fprintf(stderr, "ERROR Writing too many bits (%d > %d)\n", 1370 fprintf(stderr, "ERROR Writing too many bits (%d > %d)\n",
1371 - nb, MODBUS_MAX_BITS); 1371 + nb, MODBUS_MAX_WRITE_BITS);
1372 } 1372 }
1373 errno = EMBMDATA; 1373 errno = EMBMDATA;
1374 return -1; 1374 return -1;
@@ -1416,11 +1416,11 @@ int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data @@ -1416,11 +1416,11 @@ int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data
1416 1416
1417 uint8_t req[MAX_MESSAGE_LENGTH]; 1417 uint8_t req[MAX_MESSAGE_LENGTH];
1418 1418
1419 - if (nb > MODBUS_MAX_REGISTERS) { 1419 + if (nb > MODBUS_MAX_WRITE_REGISTERS) {
1420 if (ctx->debug) { 1420 if (ctx->debug) {
1421 fprintf(stderr, 1421 fprintf(stderr,
1422 "ERROR Trying to write to too many registers (%d > %d)\n", 1422 "ERROR Trying to write to too many registers (%d > %d)\n",
1423 - nb, MODBUS_MAX_REGISTERS); 1423 + nb, MODBUS_MAX_WRITE_REGISTERS);
1424 } 1424 }
1425 errno = EMBMDATA; 1425 errno = EMBMDATA;
1426 return -1; 1426 return -1;
src/modbus.h
@@ -78,14 +78,23 @@ MODBUS_BEGIN_DECLS @@ -78,14 +78,23 @@ MODBUS_BEGIN_DECLS
78 #define MODBUS_MAX_ADU_LENGTH_TCP 260 78 #define MODBUS_MAX_ADU_LENGTH_TCP 260
79 79
80 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12) 80 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
81 - * Quantity of Coils (2 bytes): 1 to 2000 (0x7D0) 81 + * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
  82 + * (chapter 6 section 11 page 29)
  83 + * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
82 */ 84 */
83 -#define MODBUS_MAX_BITS 2000 85 +#define MODBUS_MAX_READ_BITS 2000
  86 +#define MODBUS_MAX_WRITE_BITS 1968
84 87
85 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15) 88 /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
86 - * Quantity of Registers (2 bytes): 1 to 125 (0x7D) 89 + * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
  90 + * (chapter 6 section 12 page 31)
  91 + * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
  92 + * (chapter 6 section 17 page 38)
  93 + * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
87 */ 94 */
88 -#define MODBUS_MAX_REGISTERS 125 95 +#define MODBUS_MAX_READ_REGISTERS 125
  96 +#define MODBUS_MAX_WRITE_REGISTERS 123
  97 +#define MODBUS_MAX_RW_WRITE_REGISTERS 121
89 98
90 /* Random number to avoid errno conflicts */ 99 /* Random number to avoid errno conflicts */
91 #define MODBUS_ENOBASE 112345678 100 #define MODBUS_ENOBASE 112345678
tests/bandwidth-client.c
@@ -62,16 +62,16 @@ int main(void) @@ -62,16 +62,16 @@ int main(void)
62 } 62 }
63 63
64 /* Allocate and initialize the memory to store the status */ 64 /* Allocate and initialize the memory to store the status */
65 - tab_bit = (uint8_t *) malloc(MODBUS_MAX_BITS * sizeof(uint8_t));  
66 - memset(tab_bit, 0, MODBUS_MAX_BITS * sizeof(uint8_t)); 65 + tab_bit = (uint8_t *) malloc(MODBUS_MAX_READ_BITS * sizeof(uint8_t));
  66 + memset(tab_bit, 0, MODBUS_MAX_READ_BITS * sizeof(uint8_t));
67 67
68 /* Allocate and initialize the memory to store the registers */ 68 /* Allocate and initialize the memory to store the registers */
69 - tab_reg = (uint16_t *) malloc(MODBUS_MAX_REGISTERS * sizeof(uint16_t));  
70 - memset(tab_reg, 0, MODBUS_MAX_REGISTERS * sizeof(uint16_t)); 69 + tab_reg = (uint16_t *) malloc(MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t));
  70 + memset(tab_reg, 0, MODBUS_MAX_READ_REGISTERS * sizeof(uint16_t));
71 71
72 printf("READ BITS\n\n"); 72 printf("READ BITS\n\n");
73 73
74 - nb_points = MODBUS_MAX_BITS; 74 + nb_points = MODBUS_MAX_READ_BITS;
75 start = gettime_ms(); 75 start = gettime_ms();
76 for (i=0; i<NB_LOOPS; i++) { 76 for (i=0; i<NB_LOOPS; i++) {
77 rc = modbus_read_bits(ctx, 0, nb_points, tab_bit); 77 rc = modbus_read_bits(ctx, 0, nb_points, tab_bit);
@@ -108,7 +108,7 @@ int main(void) @@ -108,7 +108,7 @@ int main(void)
108 108
109 printf("READ REGISTERS\n\n"); 109 printf("READ REGISTERS\n\n");
110 110
111 - nb_points = MODBUS_MAX_REGISTERS; 111 + nb_points = MODBUS_MAX_READ_REGISTERS;
112 start = gettime_ms(); 112 start = gettime_ms();
113 for (i=0; i<NB_LOOPS; i++) { 113 for (i=0; i<NB_LOOPS; i++) {
114 rc = modbus_read_registers(ctx, 0, nb_points, tab_reg); 114 rc = modbus_read_registers(ctx, 0, nb_points, tab_reg);
tests/bandwidth-server-many-up.c
@@ -51,8 +51,8 @@ int main(void) @@ -51,8 +51,8 @@ int main(void)
51 51
52 ctx = modbus_new_tcp("127.0.0.1", 1502); 52 ctx = modbus_new_tcp("127.0.0.1", 1502);
53 53
54 - mb_mapping = modbus_mapping_new(MODBUS_MAX_BITS, 0,  
55 - MODBUS_MAX_REGISTERS, 0); 54 + mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
  55 + MODBUS_MAX_READ_REGISTERS, 0);
56 if (mb_mapping == NULL) { 56 if (mb_mapping == NULL) {
57 fprintf(stderr, "Failed to allocate the mapping: %s\n", 57 fprintf(stderr, "Failed to allocate the mapping: %s\n",
58 modbus_strerror(errno)); 58 modbus_strerror(errno));
tests/bandwidth-server-one.c
@@ -32,8 +32,8 @@ int main(void) @@ -32,8 +32,8 @@ int main(void)
32 32
33 ctx = modbus_new_tcp("127.0.0.1", 1502); 33 ctx = modbus_new_tcp("127.0.0.1", 1502);
34 34
35 - mb_mapping = modbus_mapping_new(MODBUS_MAX_BITS, 0,  
36 - MODBUS_MAX_REGISTERS, 0); 35 + mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
  36 + MODBUS_MAX_READ_REGISTERS, 0);
37 if (mb_mapping == NULL) { 37 if (mb_mapping == NULL) {
38 fprintf(stderr, "Failed to allocate the mapping: %s\n", 38 fprintf(stderr, "Failed to allocate the mapping: %s\n",
39 modbus_strerror(errno)); 39 modbus_strerror(errno));
tests/unit-test-client.c
@@ -372,7 +372,7 @@ int main(void) @@ -372,7 +372,7 @@ int main(void)
372 /** TOO MANY DATA **/ 372 /** TOO MANY DATA **/
373 printf("\nTEST TOO MANY DATA ERROR:\n"); 373 printf("\nTEST TOO MANY DATA ERROR:\n");
374 374
375 - rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_BITS + 1, 375 + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, MODBUS_MAX_READ_BITS + 1,
376 tab_rp_bits); 376 tab_rp_bits);
377 printf("* modbus_read_bits: "); 377 printf("* modbus_read_bits: ");
378 if (rc == -1 && errno == EMBMDATA) { 378 if (rc == -1 && errno == EMBMDATA) {
@@ -383,7 +383,7 @@ int main(void) @@ -383,7 +383,7 @@ int main(void)
383 } 383 }
384 384
385 rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, 385 rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS,
386 - MODBUS_MAX_BITS + 1, 386 + MODBUS_MAX_READ_BITS + 1,
387 tab_rp_bits); 387 tab_rp_bits);
388 printf("* modbus_read_input_bits: "); 388 printf("* modbus_read_input_bits: ");
389 if (rc == -1 && errno == EMBMDATA) { 389 if (rc == -1 && errno == EMBMDATA) {
@@ -394,7 +394,7 @@ int main(void) @@ -394,7 +394,7 @@ int main(void)
394 } 394 }
395 395
396 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, 396 rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS,
397 - MODBUS_MAX_REGISTERS + 1, 397 + MODBUS_MAX_READ_REGISTERS + 1,
398 tab_rp_registers); 398 tab_rp_registers);
399 printf("* modbus_read_registers: "); 399 printf("* modbus_read_registers: ");
400 if (rc == -1 && errno == EMBMDATA) { 400 if (rc == -1 && errno == EMBMDATA) {
@@ -405,7 +405,7 @@ int main(void) @@ -405,7 +405,7 @@ int main(void)
405 } 405 }
406 406
407 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, 407 rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS,
408 - MODBUS_MAX_REGISTERS + 1, 408 + MODBUS_MAX_READ_REGISTERS + 1,
409 tab_rp_registers); 409 tab_rp_registers);
410 printf("* modbus_read_input_registers: "); 410 printf("* modbus_read_input_registers: ");
411 if (rc == -1 && errno == EMBMDATA) { 411 if (rc == -1 && errno == EMBMDATA) {
@@ -416,7 +416,7 @@ int main(void) @@ -416,7 +416,7 @@ int main(void)
416 } 416 }
417 417
418 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, 418 rc = modbus_write_bits(ctx, UT_BITS_ADDRESS,
419 - MODBUS_MAX_BITS + 1, 419 + MODBUS_MAX_WRITE_BITS + 1,
420 tab_rp_bits); 420 tab_rp_bits);
421 printf("* modbus_write_bits: "); 421 printf("* modbus_write_bits: ");
422 if (rc == -1 && errno == EMBMDATA) { 422 if (rc == -1 && errno == EMBMDATA) {
@@ -427,7 +427,7 @@ int main(void) @@ -427,7 +427,7 @@ int main(void)
427 } 427 }
428 428
429 rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS, 429 rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS,
430 - MODBUS_MAX_REGISTERS + 1, 430 + MODBUS_MAX_WRITE_REGISTERS + 1,
431 tab_rp_registers); 431 tab_rp_registers);
432 printf("* modbus_write_registers: "); 432 printf("* modbus_write_registers: ");
433 if (rc == -1 && errno == EMBMDATA) { 433 if (rc == -1 && errno == EMBMDATA) {