99#include "AvrModbus.h"
1010#include "crc16.h"
1111/*********************************************************************************/
12- #if (AVR_MODBUS_REVISION_DATE != 20200703 )
12+ #if (AVR_MODBUS_REVISION_DATE != 20200804 )
1313#error wrong include file. (AvrModbus.h)
1414#endif
1515/*********************************************************************************/
@@ -88,32 +88,39 @@ static void SlaveReadHolding(tag_AvrModbusSlaveCtrl *Slave)
8888 StartAddr = (tU16 ) (RxQue -> Buf [2 ] << 8 ) + RxQue -> Buf [3 ];
8989 NumberOfPoint = (tU16 ) (RxQue -> Buf [4 ] << 8 ) + RxQue -> Buf [5 ];
9090
91- StartAddr = (StartAddr < 200 ) ? 0 : StartAddr - 200 ;
92- NumberOfPoint *= 2 ;
93- BaseAddr = (tU8 * ) (((tU16 * ) Slave -> BaseAddr ) + StartAddr );
94-
95- AvrUartPutChar (Slave -> Uart , RxQue -> Buf [0 ]); //Slave Address
96- AvrUartPutChar (Slave -> Uart , RxQue -> Buf [1 ]); //Function
97- AvrUartPutChar (Slave -> Uart , NumberOfPoint ); //Byte Count
98-
99- for (i = 0 ; i < NumberOfPoint ; i += 2 )
91+ if (StartAddr < Slave -> MapStartAddr )
10092 {
101- AvrUartPutChar (Slave -> Uart , * (BaseAddr + i + 1 ));
102- AvrUartPutChar (Slave -> Uart , * (BaseAddr + i ));
103- }
104-
105- if (Slave -> Bit .InitCustomFrameCheck )
106- {
107- Crc16 = Slave -> CustomFrameCheck (TxQue , TxQue -> Ctr );
93+ ErrorException (Slave , 2 );
10894 }
10995 else
11096 {
111- Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
97+ StartAddr -= Slave -> MapStartAddr ;
98+ NumberOfPoint *= 2 ;
99+ BaseAddr = (tU8 * ) (((tU16 * ) Slave -> BaseAddr ) + StartAddr );
100+
101+ AvrUartPutChar (Slave -> Uart , RxQue -> Buf [0 ]); //Slave Address
102+ AvrUartPutChar (Slave -> Uart , RxQue -> Buf [1 ]); //Function
103+ AvrUartPutChar (Slave -> Uart , NumberOfPoint ); //Byte Count
104+
105+ for (i = 0 ; i < NumberOfPoint ; i += 2 )
106+ {
107+ AvrUartPutChar (Slave -> Uart , * (BaseAddr + i + 1 ));
108+ AvrUartPutChar (Slave -> Uart , * (BaseAddr + i ));
109+ }
110+
111+ if (Slave -> Bit .InitCustomFrameCheck )
112+ {
113+ Crc16 = Slave -> CustomFrameCheck (TxQue , TxQue -> Ctr );
114+ }
115+ else
116+ {
117+ Crc16 = Crc16Check (TxQue -> OutPtr , TxQue -> Buf , & TxQue -> Buf [TxQue -> Size - 1 ], TxQue -> Ctr );
118+ }
119+
120+ AvrUartPutChar (Slave -> Uart , (Crc16 >> 8 ));
121+ AvrUartPutChar (Slave -> Uart , (Crc16 & 0x00FF ));
122+ AvrUartStartTx (Slave -> Uart );
112123 }
113-
114- AvrUartPutChar (Slave -> Uart , (Crc16 >> 8 ));
115- AvrUartPutChar (Slave -> Uart , (Crc16 & 0x00FF ));
116- AvrUartStartTx (Slave -> Uart );
117124}
118125/*********************************************************************************/
119126static void SlavePresetSingle (tag_AvrModbusSlaveCtrl * Slave )
@@ -137,13 +144,13 @@ static void SlavePresetSingle(tag_AvrModbusSlaveCtrl *Slave)
137144 RegisterAddr = (tU16 ) (RxQue -> Buf [2 ] << 8 ) + RxQue -> Buf [3 ];
138145 PresetData = (tU16 ) (RxQue -> Buf [4 ] << 8 ) + RxQue -> Buf [5 ];
139146
140- if ((Slave -> Bit .InitCheckOutRange == true) && (Slave -> CheckOutRange (RegisterAddr , 1 ) == true))
147+ if ((( Slave -> Bit .InitCheckOutRange == true) && (Slave -> CheckOutRange (RegisterAddr , 1 ) == true)) || ( RegisterAddr < Slave -> MapStartAddr ))
141148 {
142149 ErrorException (Slave , 2 );
143150 }
144151 else
145152 {
146- BaseAddr = ((tU16 * ) Slave -> BaseAddr ) + (( RegisterAddr < Slave -> MapStartAddr ) ? RegisterAddr : RegisterAddr - Slave -> MapStartAddr );
153+ BaseAddr = ((tU16 * ) Slave -> BaseAddr ) + (RegisterAddr - Slave -> MapStartAddr );
147154 * BaseAddr = PresetData ;
148155
149156 if (Slave -> Bit .InitUserException == true)
@@ -197,15 +204,15 @@ static void SlavePresetMultiple(tag_AvrModbusSlaveCtrl *Slave)
197204 StartAddr = (RxQue -> Buf [2 ] << 8 ) + RxQue -> Buf [3 ];
198205 NumberOfRegister = (RxQue -> Buf [4 ] << 8 ) + RxQue -> Buf [5 ];
199206
200- if ((Slave -> Bit .InitCheckOutRange == true) && (Slave -> CheckOutRange (StartAddr , NumberOfRegister ) == true))
207+ if ((( Slave -> Bit .InitCheckOutRange == true) && (Slave -> CheckOutRange (StartAddr , NumberOfRegister ) == true)) || ( StartAddr < Slave -> MapStartAddr ))
201208 {
202209 ErrorException (Slave , 2 );
203210 }
204211 else
205212 {
206213 Length = NumberOfRegister * 2 ;
207214 Length = (Length > (Slave -> Uart -> RxQueue .Size - 9 )) ? (Slave -> Uart -> RxQueue .Size - 9 ) : Length ;
208- BaseAddr = (tU8 * ) (((tU16 * ) Slave -> BaseAddr ) + (( StartAddr < Slave -> MapStartAddr ) ? StartAddr : StartAddr - Slave -> MapStartAddr ));
215+ BaseAddr = (tU8 * ) (((tU16 * ) Slave -> BaseAddr ) + (StartAddr - Slave -> MapStartAddr ));
209216
210217 for (i = 0 ; i < Length ; i += 2 )
211218 {
0 commit comments