author | Mario de Sousa <msousa@fe.up.pt> |
Thu, 28 May 2020 11:15:22 +0100 | |
changeset 2649 | db68cb0e6bdc |
parent 2647 | 990004083eb8 |
child 2654 | 7575050a80c5 |
permissions | -rw-r--r-- |
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) */ |
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:
2011
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 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
26 |
#include <stdio.h> |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
27 |
#include <string.h> /* required for memcpy() */ |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
28 |
#include <time.h> |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
29 |
#include <signal.h> |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
30 |
#include "mb_slave_and_master.h" |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
31 |
#include "MB_%(locstr)s.h" |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
32 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
33 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
34 |
#define MAX_MODBUS_ERROR_CODE 11 |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
35 |
static const char *modbus_error_messages[MAX_MODBUS_ERROR_CODE+1] = { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
36 |
/* 0 */ "", /* un-used -> no error! */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
37 |
/* 1 */ "illegal/unsuported function", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
38 |
/* 2 */ "illegal data address", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
39 |
/* 3 */ "illegal data value", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
40 |
/* 4 */ "slave device failure", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
41 |
/* 5 */ "acknowledge -> slave intends to reply later", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
42 |
/* 6 */ "slave device busy", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
43 |
/* 7 */ "negative acknowledge", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
44 |
/* 8 */ "memory parity error", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
45 |
/* 9 */ "", /* undefined by Modbus */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
46 |
/* 10*/ "gateway path unavalilable", |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
47 |
/* 11*/ "gateway target device failed to respond" |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
48 |
}; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
49 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
50 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
51 |
/* Execute a modbus client transaction/request */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
52 |
static int __execute_mb_request(int request_id){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
53 |
switch (client_requests[request_id].mb_function){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
54 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
55 |
case 1: /* read coils */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
56 |
return read_output_bits(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
57 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
58 |
client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
59 |
client_requests[request_id].coms_buffer, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
60 |
(int) client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
61 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
62 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
63 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
64 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
65 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
66 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
67 |
case 2: /* read discrete inputs */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
68 |
return read_input_bits( client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
69 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
70 |
client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
71 |
client_requests[request_id].coms_buffer, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
72 |
(int) client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
73 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
74 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
75 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
76 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
77 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
78 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
79 |
case 3: /* read holding registers */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
80 |
return read_output_words(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
81 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
82 |
client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
83 |
client_requests[request_id].coms_buffer, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
84 |
(int) client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
85 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
86 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
87 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
88 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
89 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
90 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
91 |
case 4: /* read input registers */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
92 |
return read_input_words(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
93 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
94 |
client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
95 |
client_requests[request_id].coms_buffer, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
96 |
(int) client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
97 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
98 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
99 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
100 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
101 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
102 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
103 |
case 5: /* write single coil */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
104 |
return write_output_bit(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
105 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
106 |
client_requests[request_id].coms_buffer[0], |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
107 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
108 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
109 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
110 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
111 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
112 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
113 |
case 6: /* write single register */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
114 |
return write_output_word(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
115 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
116 |
client_requests[request_id].coms_buffer[0], |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
117 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
118 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
119 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
120 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
121 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
122 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
123 |
case 7: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
124 |
case 8: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
125 |
case 9: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
126 |
case 10: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
127 |
case 11: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
128 |
case 12: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
129 |
case 13: break; /* function not yet supported */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
130 |
case 14: break; /* function not yet supported */ |
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 |
case 15: /* write multiple coils */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
133 |
return write_output_bits(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
134 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
135 |
client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
136 |
client_requests[request_id].coms_buffer, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
137 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
138 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
139 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
140 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
141 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
142 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
143 |
case 16: /* write multiple registers */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
144 |
return write_output_words(client_requests[request_id].slave_id, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
145 |
client_requests[request_id].address, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
146 |
client_requests[request_id].count, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
147 |
client_requests[request_id].coms_buffer, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
148 |
client_nodes[client_requests[request_id].client_node_id].mb_nd, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
149 |
client_requests[request_id].retries, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
150 |
&(client_requests[request_id].error_code), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
151 |
&(client_requests[request_id].resp_timeout), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
152 |
&(client_requests[request_id].coms_buf_mutex)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
153 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
154 |
default: break; /* should never occur, if file generation is correct */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
155 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
156 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
157 |
fprintf(stderr, "Modbus plugin: Modbus function %%d not supported\n", request_id); /* should never occur, if file generation is correct */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
158 |
return -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
159 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
160 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
161 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
162 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
163 |
/* pack bits from unpacked_data to packed_data */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
164 |
static inline int __pack_bits(u16 *unpacked_data, u16 start_addr, u16 bit_count, u8 *packed_data) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
165 |
u8 bit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
166 |
u16 byte, coils_processed; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
167 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
168 |
if ((0 == bit_count) || (65535-start_addr < bit_count-1)) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
169 |
return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
170 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
171 |
for( byte = 0, coils_processed = 0; coils_processed < bit_count; byte++) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
172 |
packed_data[byte] = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
173 |
for( bit = 0x01; (bit & 0xFF) && (coils_processed < bit_count); bit <<= 1, coils_processed++ ) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
174 |
if(unpacked_data[start_addr + coils_processed]) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
175 |
packed_data[byte] |= bit; /* set bit */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
176 |
else packed_data[byte] &= ~bit; /* reset bit */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
177 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
178 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
179 |
return 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
180 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
181 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
182 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
183 |
/* unpack bits from packed_data to unpacked_data */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
184 |
static inline int __unpack_bits(u16 *unpacked_data, u16 start_addr, u16 bit_count, u8 *packed_data) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
185 |
u8 temp, bit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
186 |
u16 byte, coils_processed; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
187 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
188 |
if ((0 == bit_count) || (65535-start_addr < bit_count-1)) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
189 |
return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
190 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
191 |
for(byte = 0, coils_processed = 0; coils_processed < bit_count; byte++) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
192 |
temp = packed_data[byte] ; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
193 |
for(bit = 0x01; (bit & 0xff) && (coils_processed < bit_count); bit <<= 1, coils_processed++) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
194 |
unpacked_data[start_addr + coils_processed] = (temp & bit)?1:0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
195 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
196 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
197 |
return 0; |
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 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
200 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
201 |
static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
202 |
{return __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
203 |
static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
204 |
{return __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
205 |
static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
206 |
{return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); } |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
207 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
208 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
209 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
210 |
static int __read_inwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
1913
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
211 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
212 |
if ((start_addr + word_count) > MEM_AREA_SIZE) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
213 |
return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
1913
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
214 |
|
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
215 |
/* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
216 |
memcpy(/* dest */ (void *)data_words, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
217 |
/* src */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
218 |
/* size */ word_count * 2); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
219 |
return 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
220 |
} |
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 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
223 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
224 |
static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
1913
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
225 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
226 |
if ((start_addr + word_count) > MEM_AREA_SIZE) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
227 |
return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
1913
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
228 |
|
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
229 |
/* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
230 |
memcpy(/* dest */ (void *)data_words, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
231 |
/* src */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
232 |
/* size */ word_count * 2); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
233 |
return 0; |
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 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
239 |
static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
1913
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
240 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
241 |
if ((start_addr + word_count) > MEM_AREA_SIZE) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
242 |
return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
243 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
244 |
/* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
245 |
* It is not therefore safe to cast it to an u16 data type. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
246 |
* The following code cannot be used. memcpy() is used instead. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
247 |
*/ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
248 |
/* |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
249 |
for (count = 0; count < word_count ; count++) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
250 |
((server_mem_t *)mem_map)->rw_words[count + start_addr] = data_words[count]; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
251 |
*/ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
252 |
memcpy(/* dest */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
253 |
/* src */ (void *)data_words, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
254 |
/* size */ word_count * 2); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
255 |
return 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
256 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
257 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
258 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
259 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
260 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
261 |
#include <pthread.h> |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
262 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
263 |
static void *__mb_server_thread(void *_server_node) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
264 |
server_node_t *server_node = _server_node; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
265 |
mb_slave_callback_t callbacks = { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
266 |
&__read_inbits, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
267 |
&__read_outbits, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
268 |
&__write_outbits, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
269 |
&__read_inwords, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
270 |
&__read_outwords, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
271 |
&__write_outwords, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
272 |
(void *)&(server_node->mem_area) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
273 |
}; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
274 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
275 |
// Enable thread cancelation. Enabled is default, but set it anyway to be safe. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
276 |
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
277 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
278 |
// mb_slave_run() should never return! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
279 |
mb_slave_run(server_node->mb_nd /* nd */, callbacks, server_node->slave_id); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
280 |
fprintf(stderr, "Modbus plugin: Modbus server for node %%s died unexpectedly!\n", server_node->location); /* should never occur */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
281 |
return NULL; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
282 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
283 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
284 |
|
2480
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
285 |
#define timespec_add(ts, sec, nsec) { \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
286 |
ts.tv_sec += sec; \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
287 |
ts.tv_nsec += nsec; \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
288 |
if (ts.tv_nsec >= 1000000000) { \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
289 |
ts.tv_sec ++; \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
290 |
ts.tv_nsec -= 1000000000; \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
291 |
} \ |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
292 |
} |
8efa26af791d
Merged Mario's modbus fix : Modbus plugin: reset periodic activation timer when overrun occurs (delays due to communication errors, ...)
Edouard Tisserant
diff
changeset
|
293 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
294 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
295 |
static void *__mb_client_thread(void *_index) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
296 |
int client_node_id = (char *)_index - (char *)NULL; // Use pointer arithmetic (more portable than cast) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
297 |
struct timespec next_cycle; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
298 |
int period_sec = client_nodes[client_node_id].comm_period / 1000; /* comm_period is in ms */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
299 |
int period_nsec = (client_nodes[client_node_id].comm_period %%1000)*1000000; /* comm_period is in ms */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
300 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
301 |
// Enable thread cancelation. Enabled is default, but set it anyway to be safe. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
302 |
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
303 |
|
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
304 |
// configure the timer for periodic activation |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
305 |
{ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
306 |
struct itimerspec timerspec; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
307 |
timerspec.it_interval.tv_sec = period_sec; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
308 |
timerspec.it_interval.tv_nsec = period_nsec; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
309 |
timerspec.it_value = timerspec.it_interval; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
310 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
311 |
if (timer_settime(client_nodes[client_node_id].timer_id, 0 /* flags */, &timerspec, NULL) < 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
312 |
fprintf(stderr, "Modbus plugin: Error configuring periodic activation timer for Modbus client %%s.\n", client_nodes[client_node_id].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
313 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
314 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
315 |
/* loop the communication with the client |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
316 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
317 |
* When the client thread has difficulty communicating with remote client and/or server (network issues, for example), |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
318 |
* then the communications get delayed and we will fall behind in the period. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
319 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
320 |
* This is OK. Note that if the condition variable were to be signaled multiple times while the client thread is inside the same |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
321 |
* Modbus transaction, then all those signals would be ignored. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
322 |
* However, and since we keep the mutex locked during the communication cycle, it is not possible to signal the condition variable |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
323 |
* during that time (it is only possible while the thread is blocked during the call to pthread_cond_wait(). |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
324 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
325 |
* This means that when network issues eventually get resolved, we will NOT have a bunch of delayed activations to handle |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
326 |
* in quick succession (which would goble up CPU time). |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
327 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
328 |
* Notice that the above property is valid whether the communication cycle is run with the mutex locked, or unlocked. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
329 |
* Since it makes it easier to implement the correct semantics for the other activation methods if the communication cycle |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
330 |
* is run with the mutex locked, then that is what we do. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
331 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
332 |
* Note that during all the communication cycle we will keep locked the mutex |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
333 |
* (i.e. the mutex used together with the condition variable that will activate a new communication cycle) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
334 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
335 |
* Note that we never get to explicitly unlock this mutex. It will only be unlocked by the pthread_cond_wait() |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
336 |
* call at the end of the cycle. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
337 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
338 |
pthread_mutex_lock(&(client_nodes[client_node_id].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
339 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
340 |
while (1) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
341 |
/* |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
342 |
struct timespec cur_time; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
343 |
clock_gettime(CLOCK_MONOTONIC, &cur_time); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
344 |
fprintf(stderr, "Modbus client thread - new cycle (%%ld:%%ld)!\n", cur_time.tv_sec, cur_time.tv_nsec); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
345 |
*/ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
346 |
int req; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
347 |
for (req=0; req < NUMBER_OF_CLIENT_REQTS; req ++){ |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
348 |
/* just do the requests belonging to the client */ |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
349 |
if (client_requests[req].client_node_id != client_node_id) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
350 |
continue; |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
351 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
352 |
/* only do the request if: |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
353 |
* - this request was explictly asked to be executed by the client program |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
354 |
* OR |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
355 |
* - the client thread was activated periodically |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
356 |
* (in which case we execute all the requests belonging to the client node) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
357 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
358 |
if ((client_requests[req].flag_exec_req == 0) && (client_nodes[client_requests[req].client_node_id].periodic_act == 0)) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
359 |
continue; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
360 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
361 |
//fprintf(stderr, "Modbus plugin: RUNNING<###> of Modbus request %%d (periodic = %%d flag_exec_req = %%d)\n", |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
362 |
// req, client_nodes[client_requests[req].client_node_id].periodic_act, client_requests[req].flag_exec_req ); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
363 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
364 |
int res_tmp = __execute_mb_request(req); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
365 |
switch (res_tmp) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
366 |
case PORT_FAILURE: { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
367 |
if (res_tmp != client_nodes[client_node_id].prev_error) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
368 |
fprintf(stderr, "Modbus plugin: Error connecting Modbus client %%s to remote server.\n", client_nodes[client_node_id].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
369 |
client_nodes[client_node_id].prev_error = res_tmp; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
370 |
break; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
371 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
372 |
case INVALID_FRAME: { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
373 |
if ((res_tmp != client_requests[req].prev_error) && (0 == client_nodes[client_node_id].prev_error)) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
374 |
fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s was unsuccesful. Server/slave returned an invalid/corrupted frame.\n", client_requests[req].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
375 |
client_requests[req].prev_error = res_tmp; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
376 |
break; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
377 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
378 |
case TIMEOUT: { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
379 |
if ((res_tmp != client_requests[req].prev_error) && (0 == client_nodes[client_node_id].prev_error)) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
380 |
fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s timed out waiting for reply from server.\n", client_requests[req].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
381 |
client_requests[req].prev_error = res_tmp; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
382 |
break; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
383 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
384 |
case MODBUS_ERROR: { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
385 |
if (client_requests[req].prev_error != client_requests[req].error_code) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
386 |
fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s was unsuccesful. Server/slave returned error code 0x%%2x", client_requests[req].location, client_requests[req].error_code); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
387 |
if (client_requests[req].error_code <= MAX_MODBUS_ERROR_CODE ) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
388 |
fprintf(stderr, "(%%s)", modbus_error_messages[client_requests[req].error_code]); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
389 |
fprintf(stderr, ".\n"); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
390 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
391 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
392 |
client_requests[req].prev_error = client_requests[req].error_code; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
393 |
break; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
394 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
395 |
default: { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
396 |
if ((res_tmp >= 0) && (client_nodes[client_node_id].prev_error != 0)) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
397 |
fprintf(stderr, "Modbus plugin: Modbus client %%s has reconnected to server/slave.\n", client_nodes[client_node_id].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
398 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
399 |
if ((res_tmp >= 0) && (client_requests[req] .prev_error != 0)) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
400 |
fprintf(stderr, "Modbus plugin: Modbus client request configured at location %%s has succesfully resumed comunication.\n", client_requests[req].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
401 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
402 |
client_nodes[client_node_id].prev_error = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
403 |
client_requests[req] .prev_error = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
404 |
break; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
405 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
406 |
} |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
407 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
408 |
/* We have just finished excuting a client transcation request. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
409 |
* If the current cycle was activated by user request we reset the flag used to ask to run it |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
410 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
411 |
if (0 != client_requests[req].flag_exec_req) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
412 |
client_requests[req].flag_exec_req = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
413 |
client_requests[req].flag_exec_started = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
414 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
415 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
416 |
//fprintf(stderr, "Modbus plugin: RUNNING<---> of Modbus request %%d (periodic = %%d flag_exec_req = %%d)\n", |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
417 |
// req, client_nodes[client_requests[req].client_node_id].periodic_act, client_requests[req].flag_exec_req ); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
418 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
419 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
420 |
// Wait for signal (from timer or explicit request from user program) before starting the next cycle |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
421 |
{ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
422 |
// No need to lock the mutex. Is is already locked just before the while(1) loop. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
423 |
// Read the comment there to understand why. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
424 |
// pthread_mutex_lock(&(client_nodes[client_node_id].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
425 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
426 |
/* the client thread has just finished a cycle, so all the flags used to signal an activation |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
427 |
* and specify the activation source (periodic, user request, ...) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
428 |
* get reset here, before waiting for a new activation. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
429 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
430 |
client_nodes[client_node_id].periodic_act = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
431 |
client_nodes[client_node_id].execute_req = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
432 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
433 |
while (client_nodes[client_node_id].execute_req == 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
434 |
pthread_cond_wait(&(client_nodes[client_node_id].condv), |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
435 |
&(client_nodes[client_node_id].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
436 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
437 |
// We run the communication cycle with the mutex locked. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
438 |
// Read the comment just above the while(1) to understand why. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
439 |
// pthread_mutex_unlock(&(client_nodes[client_node_id].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
440 |
} |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
441 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
442 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
443 |
// humour the compiler. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
444 |
return NULL; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
445 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
446 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
447 |
|
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
448 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
449 |
/* Function to activate a client node's thread */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
450 |
/* returns -1 if it could not send the signal */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
451 |
static int __signal_client_thread(int client_node_id) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
452 |
/* We TRY to signal the client thread. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
453 |
* We do this because this function can be called at the end of the PLC scan cycle |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
454 |
* and we don't want it to block at that time. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
455 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
456 |
if (pthread_mutex_trylock(&(client_nodes[client_node_id].mutex)) != 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
457 |
return -1; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
458 |
client_nodes[client_node_id].execute_req = 1; // tell the thread to execute |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
459 |
pthread_cond_signal (&(client_nodes[client_node_id].condv)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
460 |
pthread_mutex_unlock(&(client_nodes[client_node_id].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
461 |
return 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
462 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
463 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
464 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
465 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
466 |
/* Function that will be called whenever a client node's periodic timer expires. */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
467 |
/* The client node's thread will be waiting on a condition variable, so this function simply signals that |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
468 |
* condition variable. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
469 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
470 |
* The same callback function is called by the timers of all client nodes. The id of the client node |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
471 |
* in question will be passed as a parameter to the call back function. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
472 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
473 |
void __client_node_timer_callback_function(union sigval sigev_value) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
474 |
/* signal the client node's condition variable on which the client node's thread should be waiting... */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
475 |
/* Since the communication cycle is run with the mutex locked, we use trylock() instead of lock() */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
476 |
//pthread_mutex_lock (&(client_nodes[sigev_value.sival_int].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
477 |
if (pthread_mutex_trylock (&(client_nodes[sigev_value.sival_int].mutex)) != 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
478 |
/* we never get to signal the thread for activation. But that is OK. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
479 |
* If it still in the communication cycle (during which the mutex is kept locked) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
480 |
* then that means that the communication cycle is falling behing in the periodic |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
481 |
* communication cycle, and we therefore need to skip a period. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
482 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
483 |
return; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
484 |
client_nodes[sigev_value.sival_int].execute_req = 1; // tell the thread to execute |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
485 |
client_nodes[sigev_value.sival_int].periodic_act = 1; // tell the thread the activation was done by periodic timer |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
486 |
pthread_cond_signal (&(client_nodes[sigev_value.sival_int].condv)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
487 |
pthread_mutex_unlock(&(client_nodes[sigev_value.sival_int].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
488 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
489 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
490 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
491 |
|
1913
338e2f51b685
modbus extension code cleanup, from smarteh's patch
Edouard Tisserant
parents:
1912
diff
changeset
|
492 |
int __cleanup_%(locstr)s (); |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
493 |
int __init_%(locstr)s (int argc, char **argv){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
494 |
int index; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
495 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
496 |
for (index=0; index < NUMBER_OF_CLIENT_NODES;index++) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
497 |
client_nodes[index].mb_nd = -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
498 |
for (index=0; index < NUMBER_OF_SERVER_NODES;index++) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
499 |
// mb_nd with negative numbers indicate how far it has been initialised (or not) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
500 |
// -2 --> no modbus node created; no thread created |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
501 |
// -1 --> modbus node created!; no thread created |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
502 |
// >=0 --> modbus node created!; thread created! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
503 |
server_nodes[index].mb_nd = -2; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
504 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
505 |
/* modbus library init */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
506 |
/* Note that TOTAL_xxxNODE_COUNT are the nodes required by _ALL_ the instances of the modbus |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
507 |
* extension currently in the user's project. This file (MB_xx.c) is handling only one instance, |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
508 |
* but must initialize the library for all instances. Only the first call to mb_slave_and_master_init() |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
509 |
* will result in memory being allocated. All subsequent calls (by other MB_xx,c files) will be ignored |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
510 |
* by the mb_slave_and_master_init() funtion, as long as they are called with the same arguments. |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
511 |
*/ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
512 |
if (mb_slave_and_master_init(TOTAL_TCPNODE_COUNT, TOTAL_RTUNODE_COUNT, TOTAL_ASCNODE_COUNT) <0) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
513 |
fprintf(stderr, "Modbus plugin: Error starting modbus library\n"); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
514 |
// return imediately. Do NOT goto error_exit, as we did not get to |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
515 |
// start the modbus library! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
516 |
return -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
517 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
518 |
|
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
519 |
/* init each client request */ |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
520 |
/* Must be done _before_ launching the client threads!! */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
521 |
for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){ |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
522 |
/* make sure flags connected to user program MB transaction start request are all reset */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
523 |
client_requests[index].flag_exec_req = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
524 |
client_requests[index].flag_exec_started = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
525 |
/* init the mutex for each client request */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
526 |
/* Must be done _before_ launching the client threads!! */ |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
527 |
if (pthread_mutex_init(&(client_requests[index].coms_buf_mutex), NULL)) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
528 |
fprintf(stderr, "Modbus plugin: Error initializing request for modbus client node %%s\n", client_nodes[client_requests[index].client_node_id].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
529 |
goto error_exit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
530 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
531 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
532 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
533 |
/* init each client connection to remote modbus server, and launch thread */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
534 |
/* NOTE: All client_nodes[].init_state are initialised to 0 in the code |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
535 |
* generated by the modbus plugin |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
536 |
*/ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
537 |
for (index=0; index < NUMBER_OF_CLIENT_NODES;index++){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
538 |
/* establish client connection */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
539 |
client_nodes[index].mb_nd = mb_master_connect (client_nodes[index].node_address); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
540 |
if (client_nodes[index].mb_nd < 0){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
541 |
fprintf(stderr, "Modbus plugin: Error creating modbus client node %%s\n", client_nodes[index].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
542 |
goto error_exit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
543 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
544 |
client_nodes[index].init_state = 1; // we have created the node |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
545 |
|
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
546 |
/* initialize the mutex variable that will be used by the thread handling the client node */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
547 |
if (pthread_mutex_init(&(client_nodes[index].mutex), NULL) < 0) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
548 |
fprintf(stderr, "Modbus plugin: Error creating mutex for modbus client node %%s\n", client_nodes[index].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
549 |
goto error_exit; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
550 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
551 |
client_nodes[index].init_state = 2; // we have created the mutex |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
552 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
553 |
/* initialize the condition variable that will be used by the thread handling the client node */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
554 |
if (pthread_cond_init(&(client_nodes[index].condv), NULL) < 0) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
555 |
fprintf(stderr, "Modbus plugin: Error creating condition variable for modbus client node %%s\n", client_nodes[index].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
556 |
goto error_exit; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
557 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
558 |
client_nodes[index].execute_req = 0; //variable associated with condition variable |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
559 |
client_nodes[index].init_state = 3; // we have created the condition variable |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
560 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
561 |
/* initialize the timer that will be used to periodically activate the client node */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
562 |
{ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
563 |
// start off by reseting the flag that will be set whenever the timer expires |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
564 |
client_nodes[index].periodic_act = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
565 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
566 |
struct sigevent evp; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
567 |
evp.sigev_notify = SIGEV_THREAD; /* Notification method - call a function in a new thread context */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
568 |
evp.sigev_value.sival_int = index; /* Data passed to function upon notification - used to indentify which client node to activate */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
569 |
evp.sigev_notify_function = __client_node_timer_callback_function; /* function to call upon timer expiration */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
570 |
evp.sigev_notify_attributes = NULL; /* attributes for new thread in which sigev_notify_function will be called/executed */ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
571 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
572 |
if (timer_create(CLOCK_MONOTONIC, &evp, &(client_nodes[index].timer_id)) < 0) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
573 |
fprintf(stderr, "Modbus plugin: Error creating timer for modbus client node %%s\n", client_nodes[index].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
574 |
goto error_exit; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
575 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
576 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
577 |
client_nodes[index].init_state = 4; // we have created the timer |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
578 |
|
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
579 |
/* launch a thread to handle this client node */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
580 |
{ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
581 |
int res = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
582 |
pthread_attr_t attr; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
583 |
res |= pthread_attr_init(&attr); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
584 |
res |= pthread_create(&(client_nodes[index].thread_id), &attr, &__mb_client_thread, (void *)((char *)NULL + index)); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
585 |
if (res != 0) { |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
586 |
fprintf(stderr, "Modbus plugin: Error starting thread for modbus client node %%s\n", client_nodes[index].location); |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
587 |
goto error_exit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
588 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
589 |
} |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
590 |
client_nodes[index].init_state = 5; // we have created the thread |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
591 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
592 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
593 |
/* init each local server */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
594 |
/* NOTE: All server_nodes[].init_state are initialised to 0 in the code |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
595 |
* generated by the modbus plugin |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
596 |
*/ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
597 |
for (index=0; index < NUMBER_OF_SERVER_NODES;index++){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
598 |
/* create the modbus server */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
599 |
server_nodes[index].mb_nd = mb_slave_new (server_nodes[index].node_address); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
600 |
if (server_nodes[index].mb_nd < 0){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
601 |
fprintf(stderr, "Modbus plugin: Error creating modbus server node %%s\n", server_nodes[index].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
602 |
goto error_exit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
603 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
604 |
server_nodes[index].init_state = 1; // we have created the node |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
605 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
606 |
/* launch a thread to handle this server node */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
607 |
{ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
608 |
int res = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
609 |
pthread_attr_t attr; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
610 |
res |= pthread_attr_init(&attr); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
611 |
res |= pthread_create(&(server_nodes[index].thread_id), &attr, &__mb_server_thread, (void *)&(server_nodes[index])); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
612 |
if (res != 0) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
613 |
fprintf(stderr, "Modbus plugin: Error starting modbus server thread for node %%s\n", server_nodes[index].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
614 |
goto error_exit; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
615 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
616 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
617 |
server_nodes[index].init_state = 2; // we have created the node and thread |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
618 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
619 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
620 |
return 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
621 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
622 |
error_exit: |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
623 |
__cleanup_%(locstr)s (); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
624 |
return -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
625 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
626 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
627 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
628 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
629 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
630 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
631 |
void __publish_%(locstr)s (){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
632 |
int index; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
633 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
634 |
for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){ |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
635 |
/* synchronize the PLC and MB buffers only for the output requests */ |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
636 |
if (client_requests[index].req_type == req_output){ |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
637 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
638 |
// lock the mutex brefore copying the data |
2011
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
639 |
if(pthread_mutex_trylock(&(client_requests[index].coms_buf_mutex)) == 0){ |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
640 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
641 |
// Check if user configured this MB request to be activated whenever the data to be written changes |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
642 |
if (client_requests[index].write_on_change) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
643 |
// Let's check if the data did change... |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
644 |
// compare the data in plcv_buffer to coms_buffer |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
645 |
int res; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
646 |
res = memcmp((void *)client_requests[index].coms_buffer /* buf 1 */, |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
647 |
(void *)client_requests[index].plcv_buffer /* buf 2*/, |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
648 |
REQ_BUF_SIZE * sizeof(u16) /* size in bytes */); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
649 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
650 |
// if data changed, activate execution request |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
651 |
if (0 != res) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
652 |
client_requests[index].flag_exec_req = 1; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
653 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
654 |
|
2011
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
655 |
// copy from plcv_buffer to coms_buffer |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
656 |
memcpy((void *)client_requests[index].coms_buffer /* destination */, |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
657 |
(void *)client_requests[index].plcv_buffer /* source */, |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
658 |
REQ_BUF_SIZE * sizeof(u16) /* size in bytes */); |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
659 |
pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex)); |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
660 |
} |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
661 |
} |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
662 |
/* if the user program set the execution request flag, then activate the thread |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
663 |
* that handles this Modbus client transaction so it gets a chance to be executed |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
664 |
* (but don't activate the thread if it has already been activated!) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
665 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
666 |
* NOTE that we do this, for both the IN and OUT mapped location, under this |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
667 |
* __publish_() function. The scan cycle of the PLC works as follows: |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
668 |
* - call __retrieve_() |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
669 |
* - execute user programs |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
670 |
* - call __publish_() |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
671 |
* - insert <delay> until time to start next periodic/cyclic scan cycle |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
672 |
* |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
673 |
* In an attempt to be able to run the MB transactions during the <delay> |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
674 |
* interval in which not much is going on, we handle the user program |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
675 |
* requests to execute a specific MB transaction in this __publish_() |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
676 |
* function. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
677 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
678 |
if ((client_requests[index].flag_exec_req != 0) && (0 == client_requests[index].flag_exec_started)) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
679 |
int client_node_id = client_requests[index].client_node_id; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
680 |
if (__signal_client_thread(client_node_id) >= 0) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
681 |
/* - upon success, set flag_exec_started |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
682 |
* - both flags (flag_exec_req and flag_exec_started) will be reset |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
683 |
* once the transaction has completed. |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
684 |
*/ |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
685 |
client_requests[index].flag_exec_started = 1; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
686 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
687 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
688 |
} |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
689 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
690 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
691 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
692 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
693 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
694 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
695 |
void __retrieve_%(locstr)s (){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
696 |
int index; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
697 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
698 |
for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
699 |
/*just do the input requests */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
700 |
if (client_requests[index].req_type == req_input){ |
2011
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
701 |
if(pthread_mutex_trylock(&(client_requests[index].coms_buf_mutex)) == 0){ |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
702 |
// copy from coms_buffer to plcv_buffer |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
703 |
memcpy((void *)client_requests[index].plcv_buffer /* destination */, |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
704 |
(void *)client_requests[index].coms_buffer /* source */, |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
705 |
REQ_BUF_SIZE * sizeof(u16) /* size in bytes */); |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
706 |
pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex)); |
64268e87613e
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
Edouard Tisserant
parents:
1913
diff
changeset
|
707 |
} |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
708 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
709 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
710 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
711 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
712 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
713 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
714 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
715 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
716 |
int __cleanup_%(locstr)s (){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
717 |
int index, close; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
718 |
int res = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
719 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
720 |
/* kill thread and close connections of each modbus client node */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
721 |
for (index=0; index < NUMBER_OF_CLIENT_NODES; index++) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
722 |
close = 0; |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
723 |
if (client_nodes[index].init_state >= 5) { |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
724 |
// thread was launched, so we try to cancel it! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
725 |
close = pthread_cancel(client_nodes[index].thread_id); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
726 |
close |= pthread_join (client_nodes[index].thread_id, NULL); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
727 |
if (close < 0) |
2647
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
728 |
fprintf(stderr, "Modbus plugin: Error closing thread for modbus client node %%s\n", client_nodes[index].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
729 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
730 |
res |= close; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
731 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
732 |
close = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
733 |
if (client_nodes[index].init_state >= 4) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
734 |
// timer was created, so we try to destroy it! |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
735 |
close = timer_delete(client_nodes[index].timer_id); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
736 |
if (close < 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
737 |
fprintf(stderr, "Modbus plugin: Error destroying timer for modbus client node %%s\n", client_nodes[index].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
738 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
739 |
res |= close; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
740 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
741 |
close = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
742 |
if (client_nodes[index].init_state >= 3) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
743 |
// condition variable was created, so we try to destroy it! |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
744 |
close = pthread_cond_destroy(&(client_nodes[index].condv)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
745 |
if (close < 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
746 |
fprintf(stderr, "Modbus plugin: Error destroying condition variable for modbus client node %%s\n", client_nodes[index].location); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
747 |
} |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
748 |
res |= close; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
749 |
|
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
750 |
close = 0; |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
751 |
if (client_nodes[index].init_state >= 2) { |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
752 |
// mutex was created, so we try to destroy it! |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
753 |
close = pthread_mutex_destroy(&(client_nodes[index].mutex)); |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
754 |
if (close < 0) |
990004083eb8
Modbus plugin: Add "exec. req. flag" and "write on change" features
Mario de Sousa <msousa@fe.up.pt>
parents:
2521
diff
changeset
|
755 |
fprintf(stderr, "Modbus plugin: Error destroying mutex for modbus client node %%s\n", client_nodes[index].location); |
1912
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
756 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
757 |
res |= close; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
758 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
759 |
close = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
760 |
if (client_nodes[index].init_state >= 1) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
761 |
// modbus client node was created, so we try to close it! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
762 |
close = mb_master_close (client_nodes[index].mb_nd); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
763 |
if (close < 0){ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
764 |
fprintf(stderr, "Modbus plugin: Error closing modbus client node %%s\n", client_nodes[index].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
765 |
// We try to shut down as much as possible, so we do not return noW! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
766 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
767 |
client_nodes[index].mb_nd = -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
768 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
769 |
res |= close; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
770 |
client_nodes[index].init_state = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
771 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
772 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
773 |
/* kill thread and close connections of each modbus server node */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
774 |
for (index=0; index < NUMBER_OF_SERVER_NODES; index++) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
775 |
close = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
776 |
if (server_nodes[index].init_state >= 2) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
777 |
// thread was launched, so we try to cancel it! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
778 |
close = pthread_cancel(server_nodes[index].thread_id); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
779 |
close |= pthread_join (server_nodes[index].thread_id, NULL); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
780 |
if (close < 0) |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
781 |
fprintf(stderr, "Modbus plugin: Error closing thread for modbus server %%s\n", server_nodes[index].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
782 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
783 |
res |= close; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
784 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
785 |
close = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
786 |
if (server_nodes[index].init_state >= 1) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
787 |
// modbus server node was created, so we try to close it! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
788 |
close = mb_slave_close (server_nodes[index].mb_nd); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
789 |
if (close < 0) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
790 |
fprintf(stderr, "Modbus plugin: Error closing node for modbus server %%s (%%d)\n", server_nodes[index].location, server_nodes[index].mb_nd); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
791 |
// We try to shut down as much as possible, so we do not return noW! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
792 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
793 |
server_nodes[index].mb_nd = -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
794 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
795 |
res |= close; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
796 |
server_nodes[index].init_state = 0; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
797 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
798 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
799 |
/* destroy the mutex of each client request */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
800 |
for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
801 |
if (pthread_mutex_destroy(&(client_requests[index].coms_buf_mutex))) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
802 |
fprintf(stderr, "Modbus plugin: Error destroying request for modbus client node %%s\n", client_nodes[client_requests[index].client_node_id].location); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
803 |
// We try to shut down as much as possible, so we do not return noW! |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
804 |
res |= -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
805 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
806 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
807 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
808 |
/* modbus library close */ |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
809 |
//fprintf(stderr, "Shutting down modbus library...\n"); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
810 |
if (mb_slave_and_master_done()<0) { |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
811 |
fprintf(stderr, "Modbus plugin: Error shutting down modbus library\n"); |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
812 |
res |= -1; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
813 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
814 |
|
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
815 |
return res; |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
816 |
} |
8391c11477f4
Merged Mario's modbus branch. Fixed line endings.
Edouard Tisserant
parents:
1909
diff
changeset
|
817 |