99#include "AvrModbus.h"
1010#include "crc16.h"
1111/*********************************************************************************/
12- #if (AVR_MODBUS_REVISION_DATE != 20190507 )
12+ #if (AVR_MODBUS_REVISION_DATE != 20190828 )
1313#error wrong include file. (AvrModbus.h)
1414#endif
1515/*********************************************************************************/
1616/** Global variable **/
1717
18-
1918/*********************************************************************************/
2019#if (AVR_MODBUS_SLAVE == true )
2120
@@ -92,7 +91,7 @@ static void SlaveReadHolding(tag_AvrModbusSlaveCtrl *Slave)
9291 StartAddr = (StartAddr < 200 ) ? 0 : StartAddr - 200 ;
9392 NumberOfPoint *= 2 ;
9493 BaseAddr = (char * ) (((int * ) Slave -> BaseAddr ) + StartAddr );
95-
94+
9695 AvrUartPutChar (Slave -> Uart , RxQue -> Buf [0 ]); //Slave Address
9796 AvrUartPutChar (Slave -> Uart , RxQue -> Buf [1 ]); //Function
9897 AvrUartPutChar (Slave -> Uart , NumberOfPoint ); //Byte Count
@@ -103,7 +102,14 @@ static void SlaveReadHolding(tag_AvrModbusSlaveCtrl *Slave)
103102 AvrUartPutChar (Slave -> Uart , * (BaseAddr + i ));
104103 }
105104
106- Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
105+ if (Slave -> Bit .InitCustomFrameCheck )
106+ {
107+ Crc16 = Slave -> CustomFrameCheck (TxQue , TxQue -> Ctr );
108+ }
109+ else
110+ {
111+ Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
112+ }
107113
108114 AvrUartPutChar (Slave -> Uart , (Crc16 >> 8 ));
109115 AvrUartPutChar (Slave -> Uart , (Crc16 & 0x00FF ));
@@ -137,7 +143,7 @@ static void SlavePresetSingle(tag_AvrModbusSlaveCtrl *Slave)
137143 }
138144 else
139145 {
140- BaseAddr = ((int * ) Slave -> BaseAddr ) + ((RegisterAddr < 200 ) ? 0 : RegisterAddr - 200 );
146+ BaseAddr = ((int * ) Slave -> BaseAddr ) + ((RegisterAddr < Slave -> MapStartAddr ) ? RegisterAddr : RegisterAddr - Slave -> MapStartAddr );
141147 * BaseAddr = PresetData ;
142148
143149 if (Slave -> Bit .InitUserException == true)
@@ -154,7 +160,14 @@ static void SlavePresetSingle(tag_AvrModbusSlaveCtrl *Slave)
154160 AvrUartPutChar (Slave -> Uart , RxQue -> Buf [4 ]); //Preset Data Hi
155161 AvrUartPutChar (Slave -> Uart , RxQue -> Buf [5 ]); //Preset Data Lo
156162
157- Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
163+ if (Slave -> Bit .InitCustomFrameCheck )
164+ {
165+ Crc16 = Slave -> CustomFrameCheck (TxQue , TxQue -> Ctr );
166+ }
167+ else
168+ {
169+ Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
170+ }
158171
159172 AvrUartPutChar (Slave -> Uart , (Crc16 >> 8 ));
160173 AvrUartPutChar (Slave -> Uart , (Crc16 & 0x00FF ));
@@ -192,7 +205,7 @@ static void SlavePresetMultiple(tag_AvrModbusSlaveCtrl *Slave)
192205 {
193206 Length = NumberOfRegister * 2 ;
194207 Length = (Length > (Slave -> Uart -> RxQueue .Size - 9 )) ? (Slave -> Uart -> RxQueue .Size - 9 ) : Length ;
195- BaseAddr = (char * ) (((int * ) Slave -> BaseAddr ) + ((StartAddr < 200 ) ? 0 : StartAddr - 200 ));
208+ BaseAddr = (char * ) (((int * ) Slave -> BaseAddr ) + ((StartAddr < Slave -> MapStartAddr ) ? StartAddr : StartAddr - Slave -> MapStartAddr ));
196209
197210 for (i = 0 ; i < Length ; i += 2 )
198211 {
@@ -214,7 +227,14 @@ static void SlavePresetMultiple(tag_AvrModbusSlaveCtrl *Slave)
214227 AvrUartPutChar (Slave -> Uart , RxQue -> Buf [4 ]);
215228 AvrUartPutChar (Slave -> Uart , RxQue -> Buf [5 ]);
216229
217- Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
230+ if (Slave -> Bit .InitCustomFrameCheck )
231+ {
232+ Crc16 = Slave -> CustomFrameCheck (TxQue , TxQue -> Ctr );
233+ }
234+ else
235+ {
236+ Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
237+ }
218238
219239 AvrUartPutChar (Slave -> Uart , (Crc16 >> 8 ));
220240 AvrUartPutChar (Slave -> Uart , (Crc16 & 0x00FF ));
@@ -245,6 +265,7 @@ char AvrModbusSlaveGeneralInit(tag_AvrModbusSlaveCtrl *Slave, tag_AvrUartCtrl *U
245265 {
246266 Slave -> Uart = Uart ;
247267 Slave -> BaseAddr = BaseAddr ;
268+ Slave -> MapStartAddr = 200 ;
248269 Slave -> Uart -> ReceivingDelay = AVR_MODBUS_RECEIVING_DELAY_US / SlaveProcTick_us ;
249270 if (Slave -> Uart -> ReceivingDelay < 2 ) Slave -> Uart -> ReceivingDelay = 2 ;
250271
@@ -343,6 +364,57 @@ char AvrModbusSlaveLinkPreUserExceptionFunc(tag_AvrModbusSlaveCtrl *Slave, char
343364 return Slave -> Bit .InitPreUserException ;
344365}
345366/*********************************************************************************/
367+ char AvrModbusSlaveSetMapStartAddr (tag_AvrModbusSlaveCtrl * Slave , unsigned int MapStartAddr )
368+ {
369+ /*
370+ 1) 인수
371+ - Slave : tag_AvrModbusSlaveCtrl 인스턴스의 주소.
372+ - MapStartAddr : 모드버스맵상의 시작 주소.
373+
374+ 2) 반환
375+ - 0 : 초기화 실패
376+ - 1 : 초기화 성공
377+
378+ 3) 설명
379+ - 슬레이브의 모드버스맵 주소상 시작 주소 설정.
380+ */
381+
382+ if (Slave -> Bit .InitComplete == false)
383+ {
384+ return false;
385+ }
386+
387+ Slave -> MapStartAddr = MapStartAddr ;
388+
389+ return true;
390+ }
391+ /*********************************************************************************/
392+ char AvrModbusSlaveLinkCustomFrameCheck (tag_AvrModbusSlaveCtrl * Slave , int (* CustomFrameCheck )(tag_AvrUartRingBuf * RxQue , int Ctr ))
393+ {
394+ /*
395+ 1) 인수
396+ - Slave : tag_AvrModbusSlaveCtrl 인스턴스의 주소.
397+ - CustomFrameCheck : 사영자 정의 프레임 에러 검출 함수 주소.
398+
399+ 2) 반환
400+ - 0 : 초기화 실패
401+ - 1 : 초기화 성공
402+
403+ 3) 설명
404+ - 사용자 정의 프레임 에러 검출 함수 연결.
405+ */
406+
407+ if (Slave -> Bit .InitComplete == false)
408+ {
409+ return false;
410+ }
411+
412+ Slave -> CustomFrameCheck = CustomFrameCheck ;
413+ Slave -> Bit .InitCustomFrameCheck = true;
414+
415+ return Slave -> Bit .InitCustomFrameCheck ;
416+ }
417+ /*********************************************************************************/
346418void AvrModbusSlaveProc (tag_AvrModbusSlaveCtrl * Slave , unsigned char SlaveId )
347419{
348420 tag_AvrUartRingBuf * RxQue = & Slave -> Uart -> RxQueue ;
@@ -377,7 +449,14 @@ void AvrModbusSlaveProc(tag_AvrModbusSlaveCtrl *Slave, unsigned char SlaveId)
377449 {
378450 if ((RxQue -> Buf [0 ] == SlaveId ) || (RxQue -> Buf [0 ] == 0 ) || (RxQue -> Buf [0 ] == 255 ))
379451 {
380- Crc16 = Crc16Check (RxQue -> OutPtr , RxQue -> Buf , & RxQue -> Buf [RxQue -> Size - 1 ], RxQue -> Ctr - 2 );
452+ if (Slave -> Bit .InitCustomFrameCheck )
453+ {
454+ Crc16 = Slave -> CustomFrameCheck (RxQue , RxQue -> Ctr - 2 );
455+ }
456+ else
457+ {
458+ Crc16 = Crc16Check (RxQue -> OutPtr , RxQue -> Buf , & RxQue -> Buf [RxQue -> Size - 1 ], RxQue -> Ctr - 2 );
459+ }
381460
382461 if ((RxQue -> Buf [RxQue -> Ctr - 2 ] == (Crc16 >> 8 )) && (RxQue -> Buf [RxQue -> Ctr - 1 ] == (Crc16 & 0x00FF )))
383462 {
@@ -409,7 +488,6 @@ void AvrModbusSlaveProc(tag_AvrModbusSlaveCtrl *Slave, unsigned char SlaveId)
409488 }
410489 }
411490 }
412-
413491 AvrUartClearQueueBuf (RxQue );
414492 }
415493}
0 commit comments