Here are a couple of patches for mb2hal. One fixes the excessive CPU load. On my machine it took CPU load from 60% to 0%. The other adds an option to give pins names rather than numbers.

Les
>From adabc44228869510f69cbfd31d38af2d75dfd730 Mon Sep 17 00:00:00 2001
From: Les <[email protected]>
Date: Sat, 1 Jul 2017 15:00:16 +0100
Subject: [PATCH 2/2] Fixed excessive CPU load in mb2hal

The comminications threads could loop indefinitely without sleeping
while waiting for transactions to complete.
Added 1ms sleep.
---
 src/hal/user_comps/mb2hal/mb2hal.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/hal/user_comps/mb2hal/mb2hal.c 
b/src/hal/user_comps/mb2hal/mb2hal.c
index 4c7efad..709688d 100644
--- a/src/hal/user_comps/mb2hal/mb2hal.c
+++ b/src/hal/user_comps/mb2hal/mb2hal.c
@@ -161,6 +161,7 @@ void *link_loop_and_logic(void *thrd_link_num)
             if (ret_available == 0) {
                 DBG(this_mb_tx->cfg_debug, "mb_tx_num[%d] mb_links[%d] 
thread[%d] fd[%d] NOT available",
                     this_mb_tx_num, this_mb_tx->mb_link_num, this_mb_link_num, 
modbus_get_socket(this_mb_link->modbus));
+                usleep(1000);
                 continue;
             }
 
@@ -176,6 +177,7 @@ void *link_loop_and_logic(void *thrd_link_num)
             if (ret_connected == 0) {
                 DBG(this_mb_tx->cfg_debug, "mb_tx_num[%d] mb_links[%d] 
thread[%d] fd[%d] NOT connected",
                     this_mb_tx_num, this_mb_tx->mb_link_num, this_mb_link_num, 
modbus_get_socket(this_mb_link->modbus));
+                usleep(1000);
                 continue;
             }
 
-- 
2.7.4

>From 9d557589b51ace3d04986ccc0198aad348228bd4 Mon Sep 17 00:00:00 2001
From: Les <[email protected]>
Date: Sat, 1 Jul 2017 14:56:23 +0100
Subject: [PATCH 1/2] Added PIN_NAMES option to mb2hal

The PIN_NAMES option allows you to specify names for each individual
pin, instead of consecutive numbers
---
 src/hal/user_comps/mb2hal/mb2hal.h         |  1 +
 src/hal/user_comps/mb2hal/mb2hal_HOWTO.ini |  9 ++++-
 src/hal/user_comps/mb2hal/mb2hal_hal.c     |  7 +++-
 src/hal/user_comps/mb2hal/mb2hal_init.c    | 64 +++++++++++++++++++++++++++++-
 4 files changed, 76 insertions(+), 5 deletions(-)

diff --git a/src/hal/user_comps/mb2hal/mb2hal.h 
b/src/hal/user_comps/mb2hal/mb2hal.h
index 678e2c2..60e933f 100644
--- a/src/hal/user_comps/mb2hal/mb2hal.h
+++ b/src/hal/user_comps/mb2hal/mb2hal.h
@@ -78,6 +78,7 @@ typedef struct {
     char       mb_tx_fnct_name[64]; //str version of mb_tx_fnct
     int        mb_tx_1st_addr; //MB first register
     int        mb_tx_nelem;    //MB n registers
+    char **    mb_tx_names;    //MB register pin names. NULL if no names are 
specified
     int        mb_response_timeout_ms; //MB response timeout
     int        mb_byte_timeout_ms;     //MB byte timeout
     //cfg_* are others INI config params
diff --git a/src/hal/user_comps/mb2hal/mb2hal_HOWTO.ini 
b/src/hal/user_comps/mb2hal/mb2hal_HOWTO.ini
index 35d2b74..50445e5 100644
--- a/src/hal/user_comps/mb2hal/mb2hal_HOWTO.ini
+++ b/src/hal/user_comps/mb2hal/mb2hal_HOWTO.ini
@@ -84,9 +84,16 @@ MB_SLAVE_ID=1
 #REQUIRED: The first element address.
 FIRST_ELEMENT=0
 
-#REQUIRED: The number of elements.
+#REQUIRED unless PINN_NAMES is specified: The number of elements.
+#It is an error to specify both NELEMENTS and PIN_NAMES
+#The pin names will be sequential numbers e.g mb2hal.plcin.01
 NELEMENTS=16
 
+#REQUIRED unless NELEMENTS is specified: A list of element names.
+#these names will be used for the pin names, e.g mb2hal.plcin.cycle_start
+#NOTE: there must be no white space characters in the list
+PIN_NAMES=cycle_start,stop,feed_hold
+
 #REQUIRED: Modbus transaction function code (see www.modbus.org 
specifications).
 #    fnct_02_read_discrete_inputs     (02 = 0x02)
 #    fnct_03_read_holding_registers   (03 = 0x03)
diff --git a/src/hal/user_comps/mb2hal/mb2hal_hal.c 
b/src/hal/user_comps/mb2hal/mb2hal_hal.c
index 25614f5..57258a9 100644
--- a/src/hal/user_comps/mb2hal/mb2hal_hal.c
+++ b/src/hal/user_comps/mb2hal/mb2hal_hal.c
@@ -83,8 +83,11 @@ retCode create_each_mb_tx_hal_pins(mb_tx_t *mb_tx)
     }
 
     for (pin_counter = 0; pin_counter < mb_tx->mb_tx_nelem; pin_counter++) {
-
-        snprintf(hal_pin_name, HAL_NAME_LEN, "%s.%s.%02d", gbl.hal_mod_name, 
mb_tx->hal_tx_name, pin_counter);
+        if(mb_tx->mb_tx_names){
+            snprintf(hal_pin_name, HAL_NAME_LEN, "%s.%s.%s", gbl.hal_mod_name, 
mb_tx->hal_tx_name, mb_tx->mb_tx_names[pin_counter]);
+        }else{
+            snprintf(hal_pin_name, HAL_NAME_LEN, "%s.%s.%02d", 
gbl.hal_mod_name, mb_tx->hal_tx_name, pin_counter);
+        }
         DBG(gbl.init_dbg, "mb_tx_num [%d] pin_name [%s]", mb_tx->mb_tx_num, 
hal_pin_name);
 
         switch (mb_tx->mb_tx_fnct) {
diff --git a/src/hal/user_comps/mb2hal/mb2hal_init.c 
b/src/hal/user_comps/mb2hal/mb2hal_init.c
index c1bc0b3..1169c79 100644
--- a/src/hal/user_comps/mb2hal/mb2hal_init.c
+++ b/src/hal/user_comps/mb2hal/mb2hal_init.c
@@ -115,6 +115,50 @@ retCode parse_common_section()
     return retOK;
 }
 
+#define NAME_ALLOC_SIZE 5
+retCode parse_pin_names(const char * names_string, mb_tx_t *this_mb_tx)
+{
+    char *fnct_name = "parse_pin_names";
+    int name_count = 0;
+    int name_buf_size = NAME_ALLOC_SIZE;
+    char **name_ptrs = malloc(sizeof(char *) * name_buf_size);
+    char *names = malloc(strlen(names_string)  + 1);
+    if(name_ptrs == NULL || names == NULL)
+    {
+        ERR(gbl.init_dbg, "Failed allocating memory");
+        return retERR;
+    }
+    strcpy(names, names_string);       // Keep the names in a buffer
+    char * name = strtok(names, ",");
+    while(name)
+    {
+        if(strlen(name) > HAL_NAME_LEN - 15) //this is only a rough estimate
+        {
+            ERR(gbl.init_dbg, "pin name '%s' is too long", name);
+            return retERR;
+        }
+        if(name_count >= name_buf_size)
+        {
+            name_count += NAME_ALLOC_SIZE;
+            name_ptrs = realloc(name_ptrs, sizeof(char *) * name_buf_size);
+            if(name_ptrs == NULL)
+            {
+                ERR(gbl.init_dbg, "Failed allocating memory");
+                return retERR;
+            }
+        }
+        name_ptrs[name_count++]=name;
+        name = strtok(NULL, ",");
+    }
+    if(name_count == 0)
+    {
+        ERR(gbl.init_dbg, "no pin names specified");
+        return retERR;
+    }
+    this_mb_tx->mb_tx_nelem = name_count;
+    this_mb_tx->mb_tx_names = name_ptrs;
+    return retOK;
+}
 
 retCode parse_transaction_section(const int mb_tx_num)
 {
@@ -218,9 +262,25 @@ retCode parse_transaction_section(const int mb_tx_num)
     }
     DBG(gbl.init_dbg, "[%s] [%s] [%d]", section, tag, 
this_mb_tx->mb_tx_1st_addr);
 
+
+    tag = "PIN_NAMES";
+    tmpstr = iniFind(gbl.ini_file_ptr, tag, section);
+    if (tmpstr != NULL) {
+        if(parse_pin_names(tmpstr, this_mb_tx) != retOK)
+        {
+            ERR(gbl.init_dbg, "[%s] [%s] [%s] list format error", section, 
tag, tmpstr);
+            return retERR;
+        }
+        DBG(gbl.init_dbg, "[%s] [%s] [%s]", section, tag, tmpstr);
+    }
+    else {
+        this_mb_tx->mb_tx_names = NULL;
+    }
+
     tag = "NELEMENTS";  //required
-    if (iniFindInt(gbl.ini_file_ptr, tag, section, &this_mb_tx->mb_tx_nelem) 
!= 0) {
-        ERR(gbl.init_dbg, "required [%s] [%s] not found", section, tag);
+    if (iniFindInt(gbl.ini_file_ptr, tag, section, &this_mb_tx->mb_tx_nelem) 
!= 0 &&
+        this_mb_tx->mb_tx_names == NULL) {
+        ERR(gbl.init_dbg, "required [%s] [%s] or [%s] [PIN_NAMES] were not 
found", section, tag, section);
         return retERR;
     }
     if (this_mb_tx->mb_tx_nelem < 1) {
-- 
2.7.4

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Emc-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/emc-developers

Reply via email to