summaryrefslogtreecommitdiff
path: root/src/hal/classicladder/protocol_modbus_slave.c
blob: 37455fd4a5d20eba5551ac23686fbd89a44171b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* Classic Ladder Project */
/* Copyright (C) 2001-2004 Marc Le Douarain */
/* http://www.multimania.com/mavati/classicladder */
/* http://www.sourceforge.net/projects/classicladder */
/* December 2004 */
/* --------------------- */
/* Modbus slave protocol */
/* --------------------- */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */

/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
/* Lesser General Public License for more details. */

/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "classicladder.h"
#include "global.h"
#include "vars_access.h"
#include "protocol_modbus_slave.h"
#include "protocol_modbus_master.h" // some Modbus defines shared



/* TEMP!!! put this variable in global config instead ? */
int OffsetForVars = 1;


/* Question here start directly with function code 
  (no IP header or Slave number) */
int ModbusRequestToRespond( unsigned char * Question, int LgtQuestion, unsigned char * Response )
{
	int LgtResponse = 0;
	int ErrorCode = 0;
	int ScanEle;
printf("FUNCTION CODE=%d\n", Question[ 0 ] );
	switch( Question[ 0 ] )
	{
		// Read n bits (read or write bits)
		case 1:
		case 2:
			if ( LgtQuestion>=5 )
			{
				int FirstBit = (Question[1]<<8) | Question[2];
				int NbrBits = (Question[3]<<8) | Question[4];
				int NbrRealBytes = (NbrBits+7)/8;
				int ScanByte, ScanBit;
				// validity request verify
				if ( FirstBit+1+NbrRealBytes*8>InfosGene->GeneralParams.SizesInfos.nbr_bits )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
				if ( NbrBits>2000 )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
					
				if ( ErrorCode==0 )
				{ 
					// Code Function for response
					Response[ LgtResponse++ ] = Question[ 0 ];
					// Length in bytes
					Response[ LgtResponse++ ] = NbrRealBytes;
					// Bits values
					ScanEle = 0;
					for( ScanByte=0; ScanByte<NbrRealBytes; ScanByte++ )
					{
						unsigned char BitsValues = 0;
						unsigned char Mask = 0x01;
						for( ScanBit=0; ScanBit<8; ScanBit++ )
						{
							if( ReadVar( VAR_MEM_BIT, FirstBit+OffsetForVars+ScanEle++ ) )
								BitsValues = BitsValues|Mask;
							Mask = Mask<<1;
						}
						Response[ LgtResponse++ ] = BitsValues;
					}
				}
			}
			else
			{
				ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
			}
			break;
		// Read n words (read or write words)
		case 3:
		case 4:
			if ( LgtQuestion>=5 )
			{
				int FirstWord = (Question[1]<<8) | Question[2];
				int NbrWords = (Question[3]<<8) | Question[4];
				// validity request verify
				if ( FirstWord+1+NbrWords>InfosGene->GeneralParams.SizesInfos.nbr_words )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
				if ( NbrWords>200 )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
					
				if ( ErrorCode==0 )
				{ 
					// Code Function for response
					Response[ LgtResponse++ ] = Question[ 0 ];
					// Length in bytes
					Response[ LgtResponse++ ] = NbrWords*2;
					// Words values
					for( ScanEle=0; ScanEle<NbrWords; ScanEle++ )
					{
						int ValueWord = ReadVar( VAR_MEM_WORD, FirstWord+OffsetForVars+ScanEle );
						Response[ LgtResponse++ ] = (unsigned char)(ValueWord>>8);
						Response[ LgtResponse++ ] = (unsigned char)ValueWord;
					}
				}
			}
			else
			{
				ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
			}
			break;
		// Write one bit (write bit)
		case 5:
			if ( LgtQuestion>=5 )
			{
				int FirstBit = (Question[1]<<8) | Question[2];
				int OffsetQuest = 3;
				int ValueBit;
				ValueBit = (Question[ OffsetQuest++ ]<<8 );
				ValueBit = ValueBit | Question[ OffsetQuest++ ];
			
				// validity request verify
				if ( FirstBit+1>InfosGene->GeneralParams.SizesInfos.nbr_bits )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
				if ( ValueBit!=MODBUS_BIT_ON && ValueBit!=MODBUS_BIT_OFF )
					ErrorCode = MODBUS_ILLEGAL_DATA_VALUE;
					
				if ( ErrorCode==0 )
				{
					WriteVar( VAR_MEM_BIT, FirstBit+OffsetForVars, ValueBit?1:0 );
					// Code Function for response
					Response[ LgtResponse++ ] = Question[ 0 ];
					// First Bit
					Response[ LgtResponse++ ] = (unsigned char)(FirstBit>>8);
					Response[ LgtResponse++ ] = (unsigned char)(FirstBit);
					// Bit value
					Response[ LgtResponse++ ] = (unsigned char)(ValueBit>>8);
					Response[ LgtResponse++ ] = (unsigned char)(ValueBit);
				}
			}
			break;
		// Write n bits (write bits)
		case 15:
			if ( LgtQuestion>=7 )
			{
				int FirstBit = (Question[1]<<8) | Question[2];
				int NbrBits = (Question[3]<<8) | Question[4];
				// validity request verify
				if ( FirstBit+1+NbrBits>InfosGene->GeneralParams.SizesInfos.nbr_bits )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
				if ( NbrBits>2000 )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
				if ( (NbrBits+7)/8>Question[5] )
					ErrorCode = MODBUS_ILLEGAL_DATA_VALUE;
					
				if ( ErrorCode==0 )
				{ 
					int ScanByte = 6;
					int ScanBit = 0;
					int CurrentBit = FirstBit;
					unsigned char Mask = 0x01;
					do
					{
						int Value = 0;
						if ( Question[ ScanByte ]&Mask )
							Value = 1;
						WriteVar( VAR_MEM_BIT, CurrentBit+OffsetForVars, Value );
						ScanBit++;
						Mask = Mask<<1; 
						if ( ScanBit>=8 )
						{
							ScanBit = 0;
							ScanByte++;
							Mask = 0x01;
						}
						CurrentBit++;
					}
					while( CurrentBit<FirstBit+NbrBits );
					
					// Code Function for response
					Response[ LgtResponse++ ] = Question[ 0 ];
					// First bit
					Response[ LgtResponse++ ] = (unsigned char)(FirstBit>>8);
					Response[ LgtResponse++ ] = (unsigned char)(FirstBit);
					// Nbr bits
					Response[ LgtResponse++ ] = (unsigned char)(NbrBits>>8);
					Response[ LgtResponse++ ] = (unsigned char)(NbrBits);
				}
			}
			break;
		// Write 1 or n words (write words)
		case 6:
		case 16:
			if ( ( LgtQuestion>=5 && Question[0]==6 ) || ( LgtQuestion>=8 && Question[0]==16 ) )
			{
				int OffsetQuest = 3;
				int FirstWord = (Question[1]<<8) | Question[2];
				int NbrWords = 1;
				int ValueWord = 0;
				// is type n words ?
				if ( Question[0]==16 )
				{
					NbrWords = (Question[3]<<8) | Question[4];
					OffsetQuest = 6;
					if ( NbrWords*2>Question[5] )
						ErrorCode = MODBUS_ILLEGAL_DATA_VALUE;
				}
				// request verify
				if ( FirstWord+1+NbrWords>InfosGene->GeneralParams.SizesInfos.nbr_words )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
				if ( NbrWords>200 )
					ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
					
				if ( ErrorCode==0 )
				{ 
					// Words values
					for( ScanEle=0; ScanEle<NbrWords; ScanEle++ )
					{
						ValueWord = (Question[ OffsetQuest++ ]<<8 );
						ValueWord = ValueWord | Question[ OffsetQuest++ ];
						WriteVar( VAR_MEM_WORD, FirstWord+OffsetForVars+ScanEle, ValueWord );
					}
					// Code Function for response
					Response[ LgtResponse++ ] = Question[ 0 ];
					// First word
					Response[ LgtResponse++ ] = (unsigned char)(FirstWord>>8);
					Response[ LgtResponse++ ] = (unsigned char)(FirstWord);
					// is type n words ?
					if ( Question[0]==16 )
					{
						// Nbr Words
						Response[ LgtResponse++ ] = (unsigned char)(NbrWords>>8);
						Response[ LgtResponse++ ] = (unsigned char)(NbrWords);
					}
					else
					{
						// Word value
						Response[ LgtResponse++ ] = (unsigned char)(ValueWord>>8);
						Response[ LgtResponse++ ] = (unsigned char)(ValueWord);
					}
				}
			}
			else
			{
				ErrorCode = MODBUS_ILLEGAL_DATA_ADDRESS;
			}
			break;
		default:
			Response[ LgtResponse++ ] = 0x80 | Question[ 0 ];
			Response[ LgtResponse++ ] = MODBUS_ILLEGAL_FUNCTION;
			break;
	}
	if ( ErrorCode>0 )
	{
		LgtResponse = 0;
		Response[ LgtResponse++ ] = 0x80 | Question[ 0 ];
		Response[ LgtResponse++ ] = ErrorCode;
	}
	return LgtResponse;
}