modbus/mb_runtime.h
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 22 Feb 2021 10:08:36 +0100
branchsvghmi
changeset 3156 76c0c0a524c9
parent 2687 c79c5d49ba34
child 2716 ebb2595504f0
permissions -rw-r--r--
SVGHMI: display progress in IDE console while building
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     1
/* File generated by Beremiz (PlugGenerate_C method of modbus Plugin instance) */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     2
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     3
/*
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     4
 * Copyright (c) 2016 Mario de Sousa (msousa@fe.up.pt)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     5
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     6
 * This file is part of the Modbus library for Beremiz and matiec.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     7
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     8
 * This Modbus library is free software: you can redistribute it and/or modify
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
     9
 * it under the terms of the GNU Lesser General Public License as published by
2019
92f02bb17c7e Make Modbus extension license compatible with the rest of Beremiz IDE python code : GPLv2.
Edouard Tisserant
parents: 1912
diff changeset
    10
 * the Free Software Foundation, either version 2 of the License, or
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    11
 * (at your option) any later version.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    12
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    13
 * This program is distributed in the hope that it will be useful, but
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    16
 * General Public License for more details.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    17
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    18
 * You should have received a copy of the GNU Lesser General Public License
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    19
 * along with this Modbus library.  If not, see <http://www.gnu.org/licenses/>.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    20
 *
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    21
 * This code is made available on the understanding that it will not be
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    23
 */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    24
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    25
#include "mb_addr.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    26
#include "mb_tcp_private.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    27
#include "mb_master_private.h"
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    28
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    29
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    30
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    31
#define DEF_REQ_SEND_RETRIES 0
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    32
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    33
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    34
#define MODBUS_PARAM_STRING_SIZE 64
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    35
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    36
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    37
  // Used by the Modbus server node
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    38
#define MEM_AREA_SIZE 65536
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    39
typedef struct{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    40
	    u16		ro_bits [MEM_AREA_SIZE];
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    41
	    u16		rw_bits [MEM_AREA_SIZE];
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    42
	    u16		ro_words[MEM_AREA_SIZE];
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    43
	    u16		rw_words[MEM_AREA_SIZE];
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    44
	} server_mem_t;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    45
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    46
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    47
/*
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    48
 * Beremiz has a program to run on the PLC (Beremiz_service.py)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    49
 * to handle downloading of compiled programs, start/stop of PLC, etc.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    50
 * (see runtime/PLCObject.py for start/stop, loading, ...)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    51
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    52
 * This service also includes a web server to access PLC state (start/stop)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    53
 * and to change some basic confiuration parameters.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    54
 * (see runtime/NevowServer.py for the web server)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    55
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    56
 * The web server allows for extensions, where additional configuration
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    57
 * parameters may be changed on the running/downloaded PLC.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    58
 * Modbus plugin also comes with an extension to the web server, through
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    59
 * which the basic Modbus plugin configuration parameters may be changed
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    60
 *
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    61
 * This means that most values in the server_node_t and client_node_t
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    62
 * may be changed after the co,piled code (.so file) is loaded into 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    63
 * memory, and before the code starts executing.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    64
 * Since the we will also want to change the host and port (TCP) and the
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    65
 * serial device (RTU) at this time, it is best if we allocate memory for
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    66
 * these strings that may be overwritten by the web server (i.e., do not use
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    67
 * const strings) in the server_node_t and client_node_t structures.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    68
 *
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    69
 * The following structure members
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    70
 *    - node_addr_t.addr.tcp.host
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    71
 *    - node_addr_t.addr.tcp.service  (i.e. the port)
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    72
 *    - node_addr_t.addr.rtu.device
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    73
 * are all char *, and do not allocate memory for the strings.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    74
 * 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    75
 * We therefore include two generic char arrays, str1 and str2,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    76
 * that will store the above strings, and the C code will initiliaze
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    77
 * the node_addre_t.addr string pointers to these strings.
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    78
 * i.e., either addr.rtu.device will point to str1,
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    79
 *          or
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    80
 *        addr.tcp.host and addr.tcp.service 
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    81
 *        will point to str1 and str2 respectively
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    82
 */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    83
typedef struct{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    84
	    const char *location;
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
    85
        const char *config_name;
2655
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    86
              char  str1[MODBUS_PARAM_STRING_SIZE];
d2b2ee04bfa1 Add web extension: configure Modbus Server plugin parameters
Mario de Sousa <msousa@fe.up.pt>
parents: 2654
diff changeset
    87
              char  str2[MODBUS_PARAM_STRING_SIZE]; 
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    88
	    u8		slave_id;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    89
	    node_addr_t	node_address;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    90
	    int		mb_nd;      // modbus library node used for this server 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    91
	    int		init_state; // store how far along the server's initialization has progressed
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    92
	    pthread_t	thread_id;  // thread handling this server
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    93
	    server_mem_t	mem_area;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    94
	} server_node_t;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    95
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    96
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    97
  // Used by the Modbus client node
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    98
typedef struct{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
    99
	    const char *location;
2654
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   100
        const char *config_name;
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   101
              char  str1[MODBUS_PARAM_STRING_SIZE];
7575050a80c5 Add web extension: configure Modbus plugin parameters (currently only supports Modbus clients)
Mario de Sousa <msousa@fe.up.pt>
parents: 2647
diff changeset
   102
              char  str2[MODBUS_PARAM_STRING_SIZE]; 
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   103
	    node_addr_t	node_address;
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   104
	    int		mb_nd;      // modbus library node used for this client
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   105
	    int		init_state; // store how far along the client's initialization has progressed
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   106
	    u64		comm_period;// period to use when periodically sending requests to remote server
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   107
	    int		prev_error; // error code of the last printed error message (0 when no error) 
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   108
	    pthread_t   thread_id;  // thread handling all communication for this client node
2683
5d8a4deacfe1 Modbus add a timer thread, and switch timer to SIGEV_THREAD_ID to follow xenomai posix skin restrictions.
Edouard Tisserant
parents: 2655
diff changeset
   109
	    pthread_t	timer_thread_id;  // thread handling periodical timer for this client node
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   110
	    pthread_mutex_t mutex;  // mutex to be used with the following condition variable
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   111
        pthread_cond_t  condv;  // used to signal the client thread when to start new modbus transactions
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   112
        int       execute_req;  /* used, in association with condition variable,  
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   113
                                 *   to signal when to send the modbus request to the server
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   114
                                 * Note that we cannot simply rely on the condition variable to signal
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   115
                                 *   when to activate the client thread, as the call to 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   116
                                 *   pthread_cond_wait() may return without having been signaled!
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   117
                                 *   From the manual:
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   118
                                 *      Spurious  wakeups  from  the
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   119
                                 *      pthread_cond_timedwait() or pthread_cond_wait()  functions  may  occur.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   120
                                 *      Since  the  return from pthread_cond_timedwait() or pthread_cond_wait()
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   121
                                 *      does not imply anything about the value of this predicate,  the  predi-
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   122
                                 *      cate should be re-evaluated upon such return.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   123
                                 */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   124
        int      periodic_act;  /* (boolen) flag will be set when the client node's thread was activated 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   125
                                 * (by signaling the above condition variable) by the periodic timer.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   126
                                 * Note that this same thread may also be activated (condition variable is signaled)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   127
                                 * by other sources, such as when the user program requests that a specific 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   128
                                 * client MB transation be executed (flag_exec_req in client_request_t)
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   129
                                 */
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   130
	} client_node_t;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   131
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   132
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   133
  // Used by the Modbus client plugin
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   134
typedef enum {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   135
	    req_input,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   136
	    req_output,
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   137
	    no_request		/* just for tests to quickly disable a request */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   138
	} iotype_t;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   139
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   140
#define REQ_BUF_SIZE 2000
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   141
typedef struct{
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   142
	    const char *location;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   143
	    int		client_node_id;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   144
	    u8		slave_id;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   145
	    iotype_t	req_type;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   146
	    u8		mb_function;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   147
	    u16		address;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   148
	    u16		count;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   149
	    int		retries;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   150
	    u8		error_code; // modbus error code (if any) of current request
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   151
	    int		prev_error; // error code of the last printed error message (0 when no error) 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   152
	    struct timespec resp_timeout;
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   153
	    u8		write_on_change; // boolean flag. If true => execute MB request when data to send changes
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   154
	      // buffer used to store located PLC variables
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   155
	    u16		plcv_buffer[REQ_BUF_SIZE];
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   156
	      // buffer used to store data coming from / going to server
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   157
	    u16		coms_buffer[REQ_BUF_SIZE]; 
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   158
	    pthread_mutex_t coms_buf_mutex; // mutex to access coms_buffer[]
2647
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   159
          /* boolean flag that will be mapped onto a (BOOL) located variable 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   160
           * (u16 because IEC 61131-3 BOOL are mapped onto u16 in C code! )
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   161
           *    -> allow PLC program to request when to start the MB transaction
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   162
           *    -> will be reset once the MB transaction has completed
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   163
           */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   164
        u16     flag_exec_req;  
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   165
          /* flag that works in conjunction with flag_exec_req
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   166
           * (does not really need to be u16 as it is not mapped onto a located variable. )
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   167
           *    -> used by internal logic to indicate that the client thread 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   168
           *       that will be executing the MB transaction
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   169
           *       requested by flag exec_req has already been activated.
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   170
           *    -> will be reset once the MB transaction has completed
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   171
           */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   172
        u16     flag_exec_started;  
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   173
          /* flag that will be mapped onto a (WORD) located variable 
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   174
           * (u16 because the flag is a word! )
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   175
           *    -> MSByte will store the result of the last executed MB transaction
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   176
           *         1 -> error accessing IP network, or serial interface
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   177
           *         2 -> reply received from server was an invalid frame
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   178
           *         3 -> server did not reply before timeout expired
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   179
           *         4 -> server returned a valid error frame
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   180
           *    -> if the MSByte is 4, the LSByte will store the MB error code returned by the server
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   181
           *    -> will be reset (set to 0) once this MB transaction has completed sucesfully
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   182
           */
990004083eb8 Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents: 2019
diff changeset
   183
        u16     flag_exec_status;  
1912
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   184
	} client_request_t;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   185
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   186
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   187
/* The total number of nodes, needed to support _all_ instances of the modbus plugin */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   188
#define TOTAL_TCPNODE_COUNT       %(total_tcpnode_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   189
#define TOTAL_RTUNODE_COUNT       %(total_rtunode_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   190
#define TOTAL_ASCNODE_COUNT       %(total_ascnode_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   191
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   192
/* Values for instance %(locstr)s of the modbus plugin */
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   193
#define MAX_NUMBER_OF_TCPCLIENTS  %(max_remote_tcpclient)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   194
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   195
#define NUMBER_OF_TCPSERVER_NODES %(tcpserver_node_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   196
#define NUMBER_OF_TCPCLIENT_NODES %(tcpclient_node_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   197
#define NUMBER_OF_TCPCLIENT_REQTS %(tcpclient_reqs_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   198
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   199
#define NUMBER_OF_RTUSERVER_NODES %(rtuserver_node_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   200
#define NUMBER_OF_RTUCLIENT_NODES %(rtuclient_node_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   201
#define NUMBER_OF_RTUCLIENT_REQTS %(rtuclient_reqs_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   202
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   203
#define NUMBER_OF_ASCIISERVER_NODES %(ascserver_node_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   204
#define NUMBER_OF_ASCIICLIENT_NODES %(ascclient_node_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   205
#define NUMBER_OF_ASCIICLIENT_REQTS %(ascclient_reqs_count)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   206
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   207
#define NUMBER_OF_SERVER_NODES (NUMBER_OF_TCPSERVER_NODES + \
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   208
                                NUMBER_OF_RTUSERVER_NODES + \
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   209
                                NUMBER_OF_ASCIISERVER_NODES)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   210
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   211
#define NUMBER_OF_CLIENT_NODES (NUMBER_OF_TCPCLIENT_NODES + \
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   212
                                NUMBER_OF_RTUCLIENT_NODES + \
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   213
                                NUMBER_OF_ASCIICLIENT_NODES)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   214
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   215
#define NUMBER_OF_CLIENT_REQTS (NUMBER_OF_TCPCLIENT_REQTS + \
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   216
                                NUMBER_OF_RTUCLIENT_REQTS + \
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   217
                                NUMBER_OF_ASCIICLIENT_REQTS)
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   218
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   219
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   220
/*initialization following all parameters given by user in application*/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   221
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   222
static client_node_t		client_nodes[NUMBER_OF_CLIENT_NODES] = {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   223
%(client_nodes_params)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   224
};
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   225
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   226
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   227
static client_request_t	client_requests[NUMBER_OF_CLIENT_REQTS] = {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   228
%(client_req_params)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   229
};
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   230
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   231
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   232
static server_node_t		server_nodes[NUMBER_OF_SERVER_NODES] = {
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   233
%(server_nodes_params)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   234
}
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   235
;
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   236
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   237
/*******************/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   238
/*located variables*/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   239
/*******************/
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   240
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   241
%(loc_vars)s
8391c11477f4 Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents: 1909
diff changeset
   242