-
Notifications
You must be signed in to change notification settings - Fork 34
/
PetitModbus.c
470 lines (393 loc) · 17.3 KB
/
PetitModbus.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "PetitModbus.h"
#include "PetitModbusPort.h"
/*******************************ModBus Functions*******************************/
#define PETITMODBUS_READ_COILS 1
#define PETITMODBUS_READ_DISCRETE_INPUTS 2
#define PETITMODBUS_READ_HOLDING_REGISTERS 3
#define PETITMODBUS_READ_INPUT_REGISTERS 4
#define PETITMODBUS_WRITE_SINGLE_COIL 5
#define PETITMODBUS_WRITE_SINGLE_REGISTER 6
#define PETITMODBUS_WRITE_MULTIPLE_COILS 15
#define PETITMODBUS_WRITE_MULTIPLE_REGISTERS 16
/****************************End of ModBus Functions***************************/
#define PETIT_FALSE_FUNCTION 0
#define PETIT_FALSE_SLAVE_ADDRESS 1
#define PETIT_DATA_NOT_READY 2
#define PETIT_DATA_READY 3
#define PETIT_ERROR_CODE_01 0x01 // Function code is not supported
#define PETIT_ERROR_CODE_02 0x02 // Register address is not allowed or write-protected
unsigned char PETITMODBUS_SLAVE_ADDRESS =1;
typedef enum
{
PETIT_RXTX_IDLE,
PETIT_RXTX_START,
PETIT_RXTX_DATABUF,
PETIT_RXTX_WAIT_ANSWER,
PETIT_RXTX_TIMEOUT
}PETIT_RXTX_STATE;
typedef struct
{
unsigned char Address;
unsigned char Function;
unsigned char DataBuf[PETITMODBUS_RXTX_BUFFER_SIZE];
unsigned short DataLen;
}PETIT_RXTX_DATA;
/**********************Slave Transmit and Receive Variables********************/
PETIT_RXTX_DATA Petit_Tx_Data;
unsigned int Petit_Tx_Current = 0;
unsigned int Petit_Tx_CRC16 = 0xFFFF;
PETIT_RXTX_STATE Petit_Tx_State = PETIT_RXTX_IDLE;
unsigned char Petit_Tx_Buf[PETITMODBUS_TRANSMIT_BUFFER_SIZE];
unsigned int Petit_Tx_Buf_Size = 0;
PETIT_RXTX_DATA Petit_Rx_Data;
unsigned int Petit_Rx_CRC16 = 0xFFFF;
PETIT_RXTX_STATE Petit_Rx_State = PETIT_RXTX_IDLE;
unsigned char Petit_Rx_Data_Available = FALSE;
volatile unsigned short PetitModbusTimerValue = 0;
/****************End of Slave Transmit and Receive Variables*******************/
/*
* Function Name : CRC16
* @param[in] : Data - Data to Calculate CRC
* @param[in/out] : CRC - Anlik CRC degeri
* @How to use : First initial data has to be 0xFFFF.
*/
void Petit_CRC16(const unsigned char Data, unsigned int* CRC)
{
unsigned int i;
*CRC = *CRC ^(unsigned int) Data;
for (i = 8; i > 0; i--)
{
if (*CRC & 0x0001)
*CRC = (*CRC >> 1) ^ 0xA001;
else
*CRC >>= 1;
}
}
/******************************************************************************/
/*
* Function Name : DoTx
* @param[out] : TRUE
* @How to use : It is used for send data package over physical layer
*/
unsigned char Petit_DoSlaveTX(void)
{
PetitModBus_UART_String(Petit_Tx_Buf,Petit_Tx_Buf_Size);
Petit_Tx_Buf_Size = 0;
return TRUE;
}
/******************************************************************************/
/*
* Function Name : SendMessage
* @param[out] : TRUE/FALSE
* @How to use : This function start to sending messages
*/
unsigned char PetitSendMessage(void)
{
if (Petit_Tx_State != PETIT_RXTX_IDLE)
return FALSE;
Petit_Tx_Current =0;
Petit_Tx_State =PETIT_RXTX_START;
return TRUE;
}
/******************************************************************************/
/*
* Function Name : HandleModbusError
* @How to use : This function generated errors to Modbus Master
*/
void HandlePetitModbusError(char ErrorCode)
{
// Initialise the output buffer. The first byte in the buffer says how many registers we have read
Petit_Tx_Data.Function = Petit_Rx_Data.Function | 0x80;
Petit_Tx_Data.Address = PETITMODBUS_SLAVE_ADDRESS;
Petit_Tx_Data.DataLen = 1;
Petit_Tx_Data.DataBuf[0] = ErrorCode;
PetitSendMessage();
}
/******************************************************************************/
/*
* Function Name : HandleModbusReadHoldingRegisters
* @How to use : Modbus function 03 - Read holding registers
*/
#if PETITMODBUS_READ_HOLDING_REGISTERS_ENABLED > 0
void HandlePetitModbusReadHoldingRegisters(void)
{
// Holding registers are effectively numerical outputs that can be written to by the host.
// They can be control registers or analogue outputs.
// We potientially have one - the pwm output value
unsigned int Petit_StartAddress = 0;
unsigned int Petit_NumberOfRegisters = 0;
unsigned int Petit_i = 0;
// The message contains the requested start address and number of registers
Petit_StartAddress = ((unsigned int) (Petit_Rx_Data.DataBuf[0]) << 8) + (unsigned int) (Petit_Rx_Data.DataBuf[1]);
Petit_NumberOfRegisters = ((unsigned int) (Petit_Rx_Data.DataBuf[2]) << 8) + (unsigned int) (Petit_Rx_Data.DataBuf[3]);
// If it is bigger than RegisterNumber return error to Modbus Master
if((Petit_StartAddress+Petit_NumberOfRegisters)>NUMBER_OF_OUTPUT_PETITREGISTERS)
HandlePetitModbusError(PETIT_ERROR_CODE_02);
else
{
// Initialise the output buffer. The first byte in the buffer says how many registers we have read
Petit_Tx_Data.Function = PETITMODBUS_READ_HOLDING_REGISTERS;
Petit_Tx_Data.Address = PETITMODBUS_SLAVE_ADDRESS;
Petit_Tx_Data.DataLen = 1;
Petit_Tx_Data.DataBuf[0] = 0;
for (Petit_i = 0; Petit_i < Petit_NumberOfRegisters; Petit_i++)
{
unsigned short Petit_CurrentData = PetitRegisters[Petit_StartAddress+Petit_i].ActValue;
Petit_Tx_Data.DataBuf[Petit_Tx_Data.DataLen] = (unsigned char) ((Petit_CurrentData & 0xFF00) >> 8);
Petit_Tx_Data.DataBuf[Petit_Tx_Data.DataLen + 1] = (unsigned char) (Petit_CurrentData & 0xFF);
Petit_Tx_Data.DataLen += 2;
Petit_Tx_Data.DataBuf[0] = Petit_Tx_Data.DataLen - 1;
}
PetitSendMessage();
}
}
#endif
/******************************************************************************/
/*
* Function Name : HandleModbusReadInputRegisters
* @How to use : Modbus function 06 - Write single register
*/
#if PETITMODBUSWRITE_SINGLE_REGISTER_ENABLED > 0
void HandlePetitModbusWriteSingleRegister(void)
{
// Write single numerical output
unsigned int Petit_Address = 0;
unsigned int Petit_Value = 0;
unsigned char Petit_i = 0;
// The message contains the requested start address and number of registers
Petit_Address = ((unsigned int) (Petit_Rx_Data.DataBuf[0]) << 8) + (unsigned int) (Petit_Rx_Data.DataBuf[1]);
Petit_Value = ((unsigned int) (Petit_Rx_Data.DataBuf[2]) << 8) + (unsigned int) (Petit_Rx_Data.DataBuf[3]);
// Initialise the output buffer. The first byte in the buffer says how many registers we have read
Petit_Tx_Data.Function = PETITMODBUS_WRITE_SINGLE_REGISTER;
Petit_Tx_Data.Address = PETITMODBUS_SLAVE_ADDRESS;
Petit_Tx_Data.DataLen = 4;
if(Petit_Address>=NUMBER_OF_OUTPUT_PETITREGISTERS)
HandlePetitModbusError(PETIT_ERROR_CODE_02);
else
{
PetitRegisters[Petit_Address].ActValue=Petit_Value;
// Output data buffer is exact copy of input buffer
for (Petit_i = 0; Petit_i < 4; ++Petit_i)
Petit_Tx_Data.DataBuf[Petit_i] = Petit_Rx_Data.DataBuf[Petit_i];
}
PetitSendMessage();
}
#endif
/******************************************************************************/
/*
* Function Name : HandleModbusWriteMultipleRegisters
* @How to use : Modbus function 16 - Write multiple registers
*/
#if PETITMODBUS_WRITE_MULTIPLE_REGISTERS_ENABLED > 0
void HandleMPetitodbusWriteMultipleRegisters(void)
{
// Write single numerical output
unsigned int Petit_StartAddress =0;
unsigned char Petit_ByteCount =0;
unsigned int Petit_NumberOfRegisters =0;
unsigned char Petit_i =0;
unsigned int Petit_Value =0;
// The message contains the requested start address and number of registers
Petit_StartAddress = ((unsigned int) (Petit_Rx_Data.DataBuf[0]) << 8) + (unsigned int) (Petit_Rx_Data.DataBuf[1]);
Petit_NumberOfRegisters = ((unsigned int) (Petit_Rx_Data.DataBuf[2]) << 8) + (unsigned int) (Petit_Rx_Data.DataBuf[3]);
Petit_ByteCount = Petit_Rx_Data.DataBuf[4];
// If it is bigger than RegisterNumber return error to Modbus Master
if((Petit_StartAddress+Petit_NumberOfRegisters)>NUMBER_OF_OUTPUT_PETITREGISTERS)
HandlePetitModbusError(PETIT_ERROR_CODE_02);
else
{
// Initialise the output buffer. The first byte in the buffer says how many outputs we have set
Petit_Tx_Data.Function = PETITMODBUS_WRITE_MULTIPLE_REGISTERS;
Petit_Tx_Data.Address = PETITMODBUS_SLAVE_ADDRESS;
Petit_Tx_Data.DataLen = 4;
Petit_Tx_Data.DataBuf[0] = Petit_Rx_Data.DataBuf[0];
Petit_Tx_Data.DataBuf[1] = Petit_Rx_Data.DataBuf[1];
Petit_Tx_Data.DataBuf[2] = Petit_Rx_Data.DataBuf[2];
Petit_Tx_Data.DataBuf[3] = Petit_Rx_Data.DataBuf[3];
// Output data buffer is exact copy of input buffer
for (Petit_i = 0; Petit_i <Petit_NumberOfRegisters; Petit_i++)
{
Petit_Value=(Petit_Rx_Data.DataBuf[5+2*Petit_i]<<8)+(Petit_Rx_Data.DataBuf[6+2*Petit_i]);
PetitRegisters[Petit_StartAddress+Petit_i].ActValue=Petit_Value;
}
PetitSendMessage();
}
}
#endif
/******************************************************************************/
/*
* Function Name : RxDataAvailable
* @return : If Data is Ready, Return TRUE
* If Data is not Ready, Return FALSE
*/
unsigned char Petit_RxDataAvailable(void)
{
unsigned char Result = Petit_Rx_Data_Available;
Petit_Rx_Data_Available = FALSE;
return Result;
}
/******************************************************************************/
/*
* Function Name : CheckRxTimeout
* @return : If Time is out return TRUE
* If Time is not out return FALSE
*/
unsigned char Petit_CheckRxTimeout(void)
{
// A return value of true indicates there is a timeout
if (PetitModbusTimerValue>= PETITMODBUS_TIMEOUTTIMER)
{
PetitModbusTimerValue =0;
PetitReceiveCounter =0;
return TRUE;
}
return FALSE;
}
/******************************************************************************/
/*
* Function Name : CheckBufferComplete
* @return : If data is ready, return DATA_READY
* If slave address is wrong, return FALSE_SLAVE_ADDRESS
* If data is not ready, return DATA_NOT_READY
* If functions is wrong, return FALSE_FUNCTION
*/
unsigned char CheckPetitModbusBufferComplete(void)
{
int PetitExpectedReceiveCount=0;
if(PetitReceiveCounter>4)
{
if(PetitReceiveBuffer[0]==PETITMODBUS_SLAVE_ADDRESS)
{
if(PetitReceiveBuffer[1]==0x01 || PetitReceiveBuffer[1]==0x02 || PetitReceiveBuffer[1]==0x03 || PetitReceiveBuffer[1]==0x04 || PetitReceiveBuffer[1]==0x05 || PetitReceiveBuffer[1]==0x06) // RHR
{
PetitExpectedReceiveCount =8;
}
else if(PetitReceiveBuffer[1]==0x0F || PetitReceiveBuffer[1]==0x10)
{
PetitExpectedReceiveCount=PetitReceiveBuffer[6]+9;
}
else
{
PetitReceiveCounter=0;
return PETIT_FALSE_FUNCTION;
}
}
else
{
PetitReceiveCounter=0;
return PETIT_FALSE_SLAVE_ADDRESS;
}
}
else
return PETIT_DATA_NOT_READY;
if(PetitReceiveCounter==PetitExpectedReceiveCount)
{
return PETIT_DATA_READY;
}
return PETIT_DATA_NOT_READY;
}
/******************************************************************************/
/*
* Function Name : RxRTU
* @How to use : Check for data ready, if it is good return answer
*/
void Petit_RxRTU(void)
{
unsigned char Petit_i;
unsigned char Petit_ReceiveBufferControl=0;
Petit_ReceiveBufferControl =CheckPetitModbusBufferComplete();
if(Petit_ReceiveBufferControl==PETIT_DATA_READY)
{
Petit_Rx_Data.Address =PetitReceiveBuffer[0];
Petit_Rx_CRC16 = 0xffff;
Petit_CRC16(Petit_Rx_Data.Address, &Petit_Rx_CRC16);
Petit_Rx_Data.Function =PetitReceiveBuffer[1];
Petit_CRC16(Petit_Rx_Data.Function, &Petit_Rx_CRC16);
Petit_Rx_Data.DataLen=0;
for(Petit_i=2;Petit_i<PetitReceiveCounter;Petit_i++)
Petit_Rx_Data.DataBuf[Petit_Rx_Data.DataLen++]=PetitReceiveBuffer[Petit_i];
Petit_Rx_State =PETIT_RXTX_DATABUF;
PetitReceiveCounter=0;
}
Petit_CheckRxTimeout();
if ((Petit_Rx_State == PETIT_RXTX_DATABUF) && (Petit_Rx_Data.DataLen >= 2))
{
// Finish off our CRC check
Petit_Rx_Data.DataLen -= 2;
for (Petit_i = 0; Petit_i < Petit_Rx_Data.DataLen; ++Petit_i)
{
Petit_CRC16(Petit_Rx_Data.DataBuf[Petit_i], &Petit_Rx_CRC16);
}
if (((unsigned int) Petit_Rx_Data.DataBuf[Petit_Rx_Data.DataLen] + ((unsigned int) Petit_Rx_Data.DataBuf[Petit_Rx_Data.DataLen + 1] << 8)) == Petit_Rx_CRC16)
{
// Valid message!
Petit_Rx_Data_Available = TRUE;
}
Petit_Rx_State = PETIT_RXTX_IDLE;
}
}
/******************************************************************************/
/*
* Function Name : TxRTU
* @How to use : If it is ready send answers!
*/
void Petit_TxRTU(void)
{
Petit_Tx_CRC16 =0xFFFF;
Petit_Tx_Buf_Size =0;
Petit_Tx_Buf[Petit_Tx_Buf_Size++] =Petit_Tx_Data.Address;
Petit_CRC16(Petit_Tx_Data.Address, &Petit_Tx_CRC16);
Petit_Tx_Buf[Petit_Tx_Buf_Size++] =Petit_Tx_Data.Function;
Petit_CRC16(Petit_Tx_Data.Function, &Petit_Tx_CRC16);
for(Petit_Tx_Current=0; Petit_Tx_Current < Petit_Tx_Data.DataLen; Petit_Tx_Current++)
{
Petit_Tx_Buf[Petit_Tx_Buf_Size++]=Petit_Tx_Data.DataBuf[Petit_Tx_Current];
Petit_CRC16(Petit_Tx_Data.DataBuf[Petit_Tx_Current], &Petit_Tx_CRC16);
}
Petit_Tx_Buf[Petit_Tx_Buf_Size++] = Petit_Tx_CRC16 & 0x00FF;
Petit_Tx_Buf[Petit_Tx_Buf_Size++] =(Petit_Tx_CRC16 & 0xFF00) >> 8;
Petit_DoSlaveTX();
Petit_Tx_State =PETIT_RXTX_IDLE;
}
/******************************************************************************/
/*
* Function Name : ProcessModbus
* @How to use : ModBus main core! Call this function into main!
*/
void ProcessPetitModbus(void)
{
if (Petit_Tx_State != PETIT_RXTX_IDLE) // If answer is ready, send it!
Petit_TxRTU();
Petit_RxRTU(); // Call this function every cycle
if (Petit_RxDataAvailable()) // If data is ready enter this!
{
if (Petit_Rx_Data.Address == PETITMODBUS_SLAVE_ADDRESS) // Is Data for us?
{
switch (Petit_Rx_Data.Function) // Data is for us but which function?
{
#if PETITMODBUS_READ_HOLDING_REGISTERS_ENABLED > 0
case PETITMODBUS_READ_HOLDING_REGISTERS: { HandlePetitModbusReadHoldingRegisters(); break; }
#endif
#if PETITMODBUSWRITE_SINGLE_REGISTER_ENABLED > 0
case PETITMODBUS_WRITE_SINGLE_REGISTER: { HandlePetitModbusWriteSingleRegister(); break; }
#endif
#if PETITMODBUS_WRITE_MULTIPLE_REGISTERS_ENABLED > 0
case PETITMODBUS_WRITE_MULTIPLE_REGISTERS: { HandleMPetitodbusWriteMultipleRegisters(); break; }
#endif
default: { HandlePetitModbusError(PETIT_ERROR_CODE_01); break; }
}
}
}
}
/******************************************************************************/
/*
* Function Name : InitPetitModbus
* @How to use : Petite ModBus slave initialize
*/
void InitPetitModbus(unsigned char PetitModbusSlaveAddress)
{
PETITMODBUS_SLAVE_ADDRESS =PetitModbusSlaveAddress;
PetitModBus_UART_Initialise();
PetitModBus_TIMER_Initialise();
}
/******************************************************************************/