modbus/mb_runtime.h
changeset 2647 990004083eb8
parent 2019 92f02bb17c7e
child 2654 7575050a80c5
equal deleted inserted replaced
2640:1b4b335e19ea 2647:990004083eb8
    52 
    52 
    53   // Used by the Modbus client node
    53   // Used by the Modbus client node
    54 typedef struct{
    54 typedef struct{
    55 	    const char *location;
    55 	    const char *location;
    56 	    node_addr_t	node_address;
    56 	    node_addr_t	node_address;
    57 	    int		mb_nd;
    57 	    int		mb_nd;      // modbus library node used for this client
    58 	    int		init_state; // store how far along the client's initialization has progressed
    58 	    int		init_state; // store how far along the client's initialization has progressed
    59 	    u64		comm_period;
    59 	    u64		comm_period;// period to use when periodically sending requests to remote server
    60 	    int		prev_error; // error code of the last printed error message (0 when no error) 
    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
    61 	    pthread_t   thread_id;  // thread handling all communication for this client node
       
    62 	    timer_t      timer_id;  // timer used to periodically activate this client node's thread
       
    63 	    pthread_mutex_t mutex;  // mutex to be used with the following condition variable
       
    64         pthread_cond_t  condv;  // used to signal the client thread when to start new modbus transactions
       
    65         int       execute_req;  /* used, in association with condition variable,  
       
    66                                  *   to signal when to send the modbus request to the server
       
    67                                  * Note that we cannot simply rely on the condition variable to signal
       
    68                                  *   when to activate the client thread, as the call to 
       
    69                                  *   pthread_cond_wait() may return without having been signaled!
       
    70                                  *   From the manual:
       
    71                                  *      Spurious  wakeups  from  the
       
    72                                  *      pthread_cond_timedwait() or pthread_cond_wait()  functions  may  occur.
       
    73                                  *      Since  the  return from pthread_cond_timedwait() or pthread_cond_wait()
       
    74                                  *      does not imply anything about the value of this predicate,  the  predi-
       
    75                                  *      cate should be re-evaluated upon such return.
       
    76                                  */
       
    77         int      periodic_act;  /* (boolen) flag will be set when the client node's thread was activated 
       
    78                                  * (by signaling the above condition variable) by the periodic timer.
       
    79                                  * Note that this same thread may also be activated (condition variable is signaled)
       
    80                                  * by other sources, such as when the user program requests that a specific 
       
    81                                  * client MB transation be executed (flag_exec_req in client_request_t)
       
    82                                  */
    62 	} client_node_t;
    83 	} client_node_t;
    63 
    84 
    64 
    85 
    65   // Used by the Modbus client plugin
    86   // Used by the Modbus client plugin
    66 typedef enum {
    87 typedef enum {
    80 	    u16		count;
   101 	    u16		count;
    81 	    int		retries;
   102 	    int		retries;
    82 	    u8		error_code; // modbus error code (if any) of current request
   103 	    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) 
   104 	    int		prev_error; // error code of the last printed error message (0 when no error) 
    84 	    struct timespec resp_timeout;
   105 	    struct timespec resp_timeout;
       
   106 	    u8		write_on_change; // boolean flag. If true => execute MB request when data to send changes
    85 	      // buffer used to store located PLC variables
   107 	      // buffer used to store located PLC variables
    86 	    u16		plcv_buffer[REQ_BUF_SIZE];
   108 	    u16		plcv_buffer[REQ_BUF_SIZE];
    87 	      // buffer used to store data coming from / going to server
   109 	      // buffer used to store data coming from / going to server
    88 	    u16		coms_buffer[REQ_BUF_SIZE]; 
   110 	    u16		coms_buffer[REQ_BUF_SIZE]; 
    89 	    pthread_mutex_t coms_buf_mutex; // mutex to access coms_buffer[]
   111 	    pthread_mutex_t coms_buf_mutex; // mutex to access coms_buffer[]
       
   112           /* boolean flag that will be mapped onto a (BOOL) located variable 
       
   113            * (u16 because IEC 61131-3 BOOL are mapped onto u16 in C code! )
       
   114            *    -> allow PLC program to request when to start the MB transaction
       
   115            *    -> will be reset once the MB transaction has completed
       
   116            */
       
   117         u16     flag_exec_req;  
       
   118           /* flag that works in conjunction with flag_exec_req
       
   119            * (does not really need to be u16 as it is not mapped onto a located variable. )
       
   120            *    -> used by internal logic to indicate that the client thread 
       
   121            *       that will be executing the MB transaction
       
   122            *       requested by flag exec_req has already been activated.
       
   123            *    -> will be reset once the MB transaction has completed
       
   124            */
       
   125         u16     flag_exec_started;  
       
   126           /* flag that will be mapped onto a (WORD) located variable 
       
   127            * (u16 because the flag is a word! )
       
   128            *    -> MSByte will store the result of the last executed MB transaction
       
   129            *         1 -> error accessing IP network, or serial interface
       
   130            *         2 -> reply received from server was an invalid frame
       
   131            *         3 -> server did not reply before timeout expired
       
   132            *         4 -> server returned a valid error frame
       
   133            *    -> if the MSByte is 4, the LSByte will store the MB error code returned by the server
       
   134            *    -> will be reset (set to 0) once this MB transaction has completed sucesfully
       
   135            */
       
   136         u16     flag_exec_status;  
    90 	} client_request_t;
   137 	} client_request_t;
    91 
   138 
    92 
   139 
    93 /* The total number of nodes, needed to support _all_ instances of the modbus plugin */
   140 /* The total number of nodes, needed to support _all_ instances of the modbus plugin */
    94 #define TOTAL_TCPNODE_COUNT       %(total_tcpnode_count)s
   141 #define TOTAL_TCPNODE_COUNT       %(total_tcpnode_count)s