Modbus

b9f302d97b18
Parents 89333b3746d9
Children 330ac0b37846
use writev() in RTU layer -> extra bytes no longer needed
--- a/mb_ascii.c Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_ascii.c Sun Oct 16 21:23:11 2022 +0100
@@ -1610,8 +1610,7 @@
/******************************/
int modbus_ascii_init(int nd_count,
- optimization_t opt,
- int *extra_bytes)
+ optimization_t opt)
{
#ifdef DEBUG
printf("modbus_asc_init(): called...\n");
@@ -1623,22 +1622,12 @@
#endif
/* check input parameters...*/
- if (0 == nd_count) {
- if (extra_bytes != NULL)
- // Not the corect value for this layer.
- // What we set it to in case this layer is not used!
- *extra_bytes = 0;
- return 0;
- }
- if (nd_count <= 0)
- goto error_exit_0;
-
- if (extra_bytes == NULL)
- goto error_exit_0;
+ if (0 == nd_count) return 0;
+ if (nd_count <= 0) return -1;
/* initialise nd table... */
if (nd_table_init(&nd_table_, nd_count) < 0)
- goto error_exit_0;
+ return -1;
/* remember the optimization choice for later reference... */
optimization_ = opt;
@@ -1647,11 +1636,6 @@
printf("modbus_asc_init(): returning succesfuly...\n");
#endif
return 0;
-
-error_exit_0:
- if (extra_bytes != NULL)
- *extra_bytes = 0; // The value we set this to in case of error.
- return -1;
}
--- a/mb_layer1_prototypes.h Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_layer1_prototypes.h Sun Oct 16 21:23:11 2022 +0100
@@ -103,8 +103,7 @@
/* init the library */
int modbus_init(int nd_count, /* maximum number of nodes... */
- optimization_t opt,
- int *extra_bytes);
+ optimization_t opt);
/* shutdown the library...*/
int modbus_done(void);
--- a/mb_master.c Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_master.c Sun Oct 16 21:23:11 2022 +0100
@@ -65,20 +65,13 @@
/** **/
/******************************************/
/******************************************/
- /* The layer 1 (RTU, ASCII, TCP) implementation will be adding some headers and CRC (at the end)
- * of the packet we build here (actually currently it is only at the end). Since we want to
- * re-use the same buffer so as not to continuosly copy the same info from buffer to buffer,
- * we need tp allocate more bytes than the ones we need for this layer. Therefore, the
- * extra_bytes parameter.
- *
- * Note that we add one more extra byte. This is because some packets will not be
+ /* Note that we add one more extra byte. This is because some packets will not be
* starting off at byte 0, but rather at byte 1 of the buffer. This is in order to guarantee
* that the data that is sent on the buffer is aligned on even bytes (the 16 bit words!).
* This will allow us to reference this memory as an u16 *, without producing 'bus error'
* messages in some embedded devices that do not allow acessing u16 on odd numbered addresses.
*/
-static int buff_extra_bytes_;
-#define QUERY_BUFFER_SIZE (MAX_L2_FRAME_LENGTH + buff_extra_bytes_ + 1)
+#define QUERY_BUFFER_SIZE (MAX_L2_FRAME_LENGTH + 1)
/******************************************/
@@ -1156,11 +1149,10 @@
/* Initialise the Modbus Master Layer */
-int mb_master_init__(int extra_bytes) {
+int mb_master_init__() {
#ifdef DEBUG
- fprintf(stderr, "mb_master_init__(extra_bytes=%d), QUERY_BUFFER_SIZE=%d\n", extra_bytes, QUERY_BUFFER_SIZE);
+ fprintf(stderr, "mb_master_init__(), QUERY_BUFFER_SIZE=%d\n", QUERY_BUFFER_SIZE);
#endif
- buff_extra_bytes_ = extra_bytes;
return 0;
}
--- a/mb_master_private.h Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_master_private.h Sun Oct 16 21:23:11 2022 +0100
@@ -38,7 +38,7 @@
#define DEF_OPTIMIZATION optimize_speed
-int mb_master_init__(int extra_bytes);
+int mb_master_init__(void);
int mb_master_done__(void);
--- a/mb_rtu.c Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_rtu.c Sun Oct 16 21:23:11 2022 +0100
@@ -1957,8 +1957,7 @@
/******************************/
int modbus_rtu_init(int nd_count,
- optimization_t opt,
- int *extra_bytes)
+ optimization_t opt)
{
#ifdef DEBUG
fprintf(stderr, "modbus_rtu_init(): called...\n");
@@ -1970,38 +1969,19 @@
#endif
/* check input parameters...*/
- if (0 == nd_count) {
- if (extra_bytes != NULL)
- // Not the corect value for this layer.
- // What we set it to in case this layer is not used!
- *extra_bytes = 0;
- return 0;
- }
- if (nd_count <= 0)
- goto error_exit_0;
-
- if (extra_bytes == NULL)
- goto error_exit_0;
+ if (0 == nd_count) return 0;
+ if (nd_count <= 0) return -1;
if (crc_init(opt) < 0) {
#ifdef ERRMSG
fprintf(stderr, ERRMSG_HEAD "Out of memory: error initializing crc buffers\n");
#endif
- goto error_exit_0;
+ return -1;
}
- /* set the extra_bytes value... */
- /* Please see note before the modbus_rtu_write() function for a
- * better understanding of this extremely ugly hack...
- *
- * The number of extra bytes that must be allocated to the data buffer
- * before calling modbus_rtu_write()
- */
- *extra_bytes = 0;
-
/* initialise nd table... */
if (nd_table_init(&nd_table_, nd_count) < 0)
- goto error_exit_0;
+ return -1;
/* remember the optimization choice for later reference... */
optimization_ = opt;
@@ -2010,13 +1990,6 @@
fprintf(stderr, "modbus_rtu_init(): returning succesfuly...\n");
#endif
return 0;
-
-error_exit_0:
- if (extra_bytes != NULL)
- // Not the corect value for this layer.
- // What we set it to in case of error!
- *extra_bytes = 0;
- return -1;
}
--- a/mb_slave.c Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_slave.c Sun Oct 16 21:23:11 2022 +0100
@@ -65,14 +65,7 @@
/** **/
/******************************************/
/******************************************/
-/* The layer 1 (RTU, ASCII, TCP) implementations will be adding some
- * header and tail bytes (e.g. CRC) to the packet we build here. Since
- * layer1 will re-use the same buffer allocated in this slave layer
- * (so as not to continuosly copy the same info from buffer to buffer),
- * we need to allocate more bytes than those strictly required for this
- * slave layer. Therefore, the extra_bytes parameter.
- *
- * Note that we add one more extra byte to the response buffer.
+/* Note that we add one more extra byte to the response buffer.
* This is because some response packets will not be starting off
* at byte 0, but rather at byte 1 of the buffer. This is in order
* to guarantee that the data that is sent on the buffer is aligned
@@ -82,8 +75,7 @@
* 'bus error' messages in some embedded devices that do not allow
* acessing u16 on odd numbered addresses.
*/
-static int buff_extra_bytes_;
-#define RESP_BUFFER_SIZE (MAX_L2_FRAME_LENGTH + buff_extra_bytes_ + 1)
+#define RESP_BUFFER_SIZE (MAX_L2_FRAME_LENGTH + 1)
/******************************************/
/******************************************/
@@ -647,14 +639,8 @@
/***********************************************/
/***********************************************/
-int mb_slave_init__(int extra_bytes) {
- buff_extra_bytes_ = extra_bytes;
- return 0;
-}
-
-
-int mb_slave_done__(void)
- {return 0;}
+int mb_slave_init__(void) {return 0;}
+int mb_slave_done__(void) {return 0;}
#if 0
--- a/mb_slave_and_master.c Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_slave_and_master.c Sun Oct 16 21:23:11 2022 +0100
@@ -106,7 +106,6 @@
#define max(a,b) (((a)>(b))?(a):(b))
int mb_slave_and_master_init(int nd_count_tcp, int nd_count_rtu, int nd_count_ascii) {
- int extra_bytes, extra_bytes_tcp, extra_bytes_rtu, extra_bytes_ascii;
#ifdef DEBUG
fprintf( stderr, "mb_slave_and_master_init()\n");
@@ -114,20 +113,13 @@
#endif
/* initialise layer 1 library */
- if (modbus_tcp_init (nd_count_tcp, DEF_OPTIMIZATION, &extra_bytes_tcp ) < 0)
- goto error_exit_0;
- if (modbus_rtu_init (nd_count_rtu, DEF_OPTIMIZATION, &extra_bytes_rtu ) < 0)
- goto error_exit_1;
- if (modbus_ascii_init(nd_count_ascii, DEF_OPTIMIZATION, &extra_bytes_ascii) < 0)
- goto error_exit_2;
- extra_bytes= max(extra_bytes_tcp, extra_bytes_rtu);
- extra_bytes= max(extra_bytes , extra_bytes_ascii);
+ if (modbus_tcp_init (nd_count_tcp, DEF_OPTIMIZATION) < 0) goto error_exit_0;
+ if (modbus_rtu_init (nd_count_rtu, DEF_OPTIMIZATION) < 0) goto error_exit_1;
+ if (modbus_ascii_init(nd_count_ascii, DEF_OPTIMIZATION) < 0) goto error_exit_2;
/* initialise master and slave libraries... */
- if (mb_slave_init__(extra_bytes) < 0)
- goto error_exit_3;
- if (mb_master_init__(extra_bytes) < 0)
- goto error_exit_4;
+ if (mb_slave_init__ () < 0) goto error_exit_3;
+ if (mb_master_init__() < 0) goto error_exit_4;
return 0;
/*
--- a/mb_slave_private.h Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_slave_private.h Sun Oct 16 21:23:11 2022 +0100
@@ -38,7 +38,7 @@
-int mb_slave_init__(int extra_bytes);
+int mb_slave_init__(void);
int mb_slave_done__(void);
--- a/mb_tcp.c Sun Oct 16 19:40:53 2022 +0100
+++ b/mb_tcp.c Sun Oct 16 21:23:11 2022 +0100
@@ -1453,8 +1453,8 @@
* returns -1 on error.
*/
int modbus_tcp_init(int nd_count,
- optimization_t opt /* ignored... */,
- int *extra_bytes) {
+ optimization_t opt /* ignored... */
+ ) {
#ifdef DEBUG
printf("[%lu] modbus_tcp_init(): called...\n", pthread_self());
printf("[%lu] creating %d nodes:\n", pthread_self(), nd_count);
@@ -1462,27 +1462,12 @@
modbus_tcp_init_counter++;
- /* set the extra_bytes value... */
- /* Please see note before the modbus_rtu_write() function for a
- * better understanding of this extremely ugly hack... This will be
- * in the mb_rtu.c file!!
- *
- * The number of extra bytes that must be allocated to the data buffer
- * before calling modbus_tcp_write()
- */
- if (extra_bytes != NULL)
- *extra_bytes = 0;
-
- if (0 == nd_count)
- /* no need to initialise this layer! */
- return 0;
- if (nd_count <= 0)
- /* invalid node count... */
- goto error_exit_1;
+ if (0 == nd_count) return 0; /* no need to initialise this layer! */
+ if (nd_count <= 0) return -1; /* invalid node count... */
/* initialise the node table... */
if (nd_table_init(&nd_table_, nd_count) < 0)
- goto error_exit_1;
+ return -1;
#ifdef DEBUG
printf("[%lu] modbus_tcp_init(): %d node(s) opened succesfully\n", pthread_self(), nd_count);
@@ -1490,13 +1475,10 @@
return nd_count; /* number of succesfully created nodes! */
/*
-error_exit_2:
+error_exit_1:
nd_table_done(&nd_table_);
+ return -1;
*/
-error_exit_1:
- if (extra_bytes != NULL)
- *extra_bytes = 0;
- return -1;
}