modbus/mb_runtime.h
changeset 1909 bb883e063175
child 1912 8391c11477f4
equal deleted inserted replaced
1475:de4ee16f7c6c 1909:bb883e063175
       
     1 /* File generated by Beremiz (PlugGenerate_C method of modbus Plugin instance) */
       
     2 
       
     3 /*
       
     4  * Copyright (c) 2016 Mario de Sousa (msousa@fe.up.pt)
       
     5  *
       
     6  * This file is part of the Modbus library for Beremiz and matiec.
       
     7  *
       
     8  * This Modbus library is free software: you can redistribute it and/or modify
       
     9  * it under the terms of the GNU Lesser General Public License as published by
       
    10  * the Free Software Foundation, either version 3 of the License, or
       
    11  * (at your option) any later version.
       
    12  *
       
    13  * This program is distributed in the hope that it will be useful, but
       
    14  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
       
    16  * General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU Lesser General Public License
       
    19  * along with this Modbus library.  If not, see <http://www.gnu.org/licenses/>.
       
    20  *
       
    21  * This code is made available on the understanding that it will not be
       
    22  * used in safety-critical situations without a full and competent review.
       
    23  */
       
    24 
       
    25 #include "mb_addr.h"
       
    26 #include "mb_tcp_private.h"
       
    27 #include "mb_master_private.h"
       
    28 
       
    29 
       
    30 
       
    31 #define DEF_REQ_SEND_RETRIES 0
       
    32 
       
    33   // Used by the Modbus server node
       
    34 #define MEM_AREA_SIZE 65536
       
    35 typedef struct{
       
    36 	    u16		ro_bits [MEM_AREA_SIZE];
       
    37 	    u16		rw_bits [MEM_AREA_SIZE];
       
    38 	    u16		ro_words[MEM_AREA_SIZE];
       
    39 	    u16		rw_words[MEM_AREA_SIZE];
       
    40 	} server_mem_t;
       
    41 
       
    42 typedef struct{
       
    43 	    const char *location;
       
    44 	    u8		slave_id;
       
    45 	    node_addr_t	node_address;
       
    46 	    int		mb_nd;      // modbus library node used for this server 
       
    47 	    int		init_state; // store how far along the server's initialization has progressed
       
    48 	    pthread_t	thread_id;  // thread handling this server
       
    49 	    server_mem_t	mem_area;
       
    50 	} server_node_t;
       
    51 
       
    52 
       
    53   // Used by the Modbus client node
       
    54 typedef struct{
       
    55 	    const char *location;
       
    56 	    node_addr_t	node_address;
       
    57 	    int		mb_nd;
       
    58 	    int		init_state; // store how far along the client's initialization has progressed
       
    59 	    u64		comm_period;
       
    60 	    int		prev_error; // error code of the last printed error message (0 when no error) 
       
    61 	    pthread_t	thread_id;  // thread handling all communication with this client
       
    62 	} client_node_t;
       
    63 
       
    64 
       
    65   // Used by the Modbus client plugin
       
    66 typedef enum {
       
    67 	    req_input,
       
    68 	    req_output,
       
    69 	    no_request		/* just for tests to quickly disable a request */
       
    70 	} iotype_t;
       
    71 
       
    72 #define REQ_BUF_SIZE 2000
       
    73 typedef struct{
       
    74 	    const char *location;
       
    75 	    int		client_node_id;
       
    76 	    u8		slave_id;
       
    77 	    iotype_t	req_type;
       
    78 	    u8		mb_function;
       
    79 	    u16		address;
       
    80 	    u16		count;
       
    81 	    int		retries;
       
    82 	    u8		error_code; // modbus error code (if any) of current request
       
    83 	    int		prev_error; // error code of the last printed error message (0 when no error) 
       
    84 	    struct timespec resp_timeout;
       
    85 	      // buffer used to store located PLC variables
       
    86 	    u16		plcv_buffer[REQ_BUF_SIZE];
       
    87 	      // buffer used to store data coming from / going to server
       
    88 	    u16		coms_buffer[REQ_BUF_SIZE]; 
       
    89 	    pthread_mutex_t coms_buf_mutex; // mutex to access coms_buffer[]
       
    90 	} client_request_t;
       
    91 
       
    92 
       
    93 /* The total number of nodes, needed to support _all_ instances of the modbus plugin */
       
    94 #define TOTAL_TCPNODE_COUNT       %(total_tcpnode_count)s
       
    95 #define TOTAL_RTUNODE_COUNT       %(total_rtunode_count)s
       
    96 #define TOTAL_ASCNODE_COUNT       %(total_ascnode_count)s
       
    97 
       
    98 /* Values for instance %(locstr)s of the modbus plugin */
       
    99 #define MAX_NUMBER_OF_TCPCLIENTS  %(max_remote_tcpclient)s
       
   100 
       
   101 #define NUMBER_OF_TCPSERVER_NODES %(tcpserver_node_count)s
       
   102 #define NUMBER_OF_TCPCLIENT_NODES %(tcpclient_node_count)s
       
   103 #define NUMBER_OF_TCPCLIENT_REQTS %(tcpclient_reqs_count)s
       
   104 
       
   105 #define NUMBER_OF_RTUSERVER_NODES %(rtuserver_node_count)s
       
   106 #define NUMBER_OF_RTUCLIENT_NODES %(rtuclient_node_count)s
       
   107 #define NUMBER_OF_RTUCLIENT_REQTS %(rtuclient_reqs_count)s
       
   108 
       
   109 #define NUMBER_OF_ASCIISERVER_NODES %(ascserver_node_count)s
       
   110 #define NUMBER_OF_ASCIICLIENT_NODES %(ascclient_node_count)s
       
   111 #define NUMBER_OF_ASCIICLIENT_REQTS %(ascclient_reqs_count)s
       
   112 
       
   113 #define NUMBER_OF_SERVER_NODES (NUMBER_OF_TCPSERVER_NODES + \
       
   114                                 NUMBER_OF_RTUSERVER_NODES + \
       
   115                                 NUMBER_OF_ASCIISERVER_NODES)
       
   116 
       
   117 #define NUMBER_OF_CLIENT_NODES (NUMBER_OF_TCPCLIENT_NODES + \
       
   118                                 NUMBER_OF_RTUCLIENT_NODES + \
       
   119                                 NUMBER_OF_ASCIICLIENT_NODES)
       
   120 
       
   121 #define NUMBER_OF_CLIENT_REQTS (NUMBER_OF_TCPCLIENT_REQTS + \
       
   122                                 NUMBER_OF_RTUCLIENT_REQTS + \
       
   123                                 NUMBER_OF_ASCIICLIENT_REQTS)
       
   124 
       
   125 
       
   126 /*initialization following all parameters given by user in application*/
       
   127 
       
   128 static client_node_t		client_nodes[NUMBER_OF_CLIENT_NODES] = {
       
   129 %(client_nodes_params)s
       
   130 };
       
   131 
       
   132 
       
   133 static client_request_t	client_requests[NUMBER_OF_CLIENT_REQTS] = {
       
   134 %(client_req_params)s
       
   135 };
       
   136 
       
   137 
       
   138 static server_node_t		server_nodes[NUMBER_OF_SERVER_NODES] = {
       
   139 %(server_nodes_params)s
       
   140 }
       
   141 ;
       
   142 
       
   143 /*******************/
       
   144 /*located variables*/
       
   145 /*******************/
       
   146 
       
   147 %(loc_vars)s
       
   148