Skip to content

Commit 88e0271

Browse files
Merge pull request #22 from TimerOverflow/20200804
20200804
2 parents 16990b3 + dea00e1 commit 88e0271

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

AvrModbus.c

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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
/*********************************************************************************/
119126
static 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
{

AvrModbus.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
/*********************************************************************************/
1010
#include "AvrUart.h"
1111
/*********************************************************************************/
12-
#define AVR_MODBUS_REVISION_DATE 20200703
12+
#define AVR_MODBUS_REVISION_DATE 20200804
1313
/*********************************************************************************/
1414
/** REVISION HISTORY **/
1515
/*
16+
2020. 08. 04. - Slave파트 시작주소 범위를 벗어나 호출하거나 쓰기 명령이 있을 경우 에러 리턴하도록 수정.
17+
Jeong Hyun Gu - Slave파트 시작번지가 200 미만일 경우 정상 응답하지 않는 부분 수정.
18+
1619
2020. 07. 03. - Slave파트 AVR_MODBUS_ReadSerialNumber (0x73)펑션 마스터가 요청하는 길이 부분은 무시하고,
1720
Jeong Hyun Gu 연결 되어 있는 프로그램명 문자열 길이로 응답.
1821
- Master파트 AvrModbusMasterSetSlavePollFunction() 삭제. 앞으로 PollFunction은 AvrModbusMasterAddSlave(),
@@ -116,7 +119,7 @@
116119
#define false 0
117120
#define null 0
118121

119-
#define AVR_MODBUS_MASTER true
122+
#define AVR_MODBUS_MASTER false
120123
#define AVR_MODBUS_SLAVE true
121124

122125
#define AVR_MODBUS_RECEIVING_DELAY_US 20000

0 commit comments

Comments
 (0)