Skip to content

Commit bec9074

Browse files
Merge pull request #29 from TimerOverflow/20220209
20220209
2 parents 82a985e + acdbdf8 commit bec9074

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

AvrModbus.c

Lines changed: 37 additions & 11 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 != 20220114)
12+
#if(AVR_MODBUS_REVISION_DATE != 20220209)
1313
#error wrong include file. (AvrModbus.h)
1414
#endif
1515
/*********************************************************************************/
@@ -654,7 +654,8 @@ static void MasterPolling(tag_AvrModbusMasterCtrl *Master)
654654
{
655655
Master->SlavePoll->NoResponseCnt++;
656656
}
657-
657+
658+
Master->IsPollingSend = true;
658659
PollData = &Master->SlavePoll->PollData[Master->SlavePoll->PollDataIndex];
659660

660661
AvrUartPutChar(Master->Uart, Master->SlavePoll->Id);
@@ -692,6 +693,30 @@ static void MasterReceiveStatus(tag_AvrModbusMasterSlavePollData *PollData, tag_
692693
memcpy(PollData->BaseAddr, &RxQue->Buf[3], ByteCount);
693694
}
694695
/*********************************************************************************/
696+
static tU16 GetExpectRxCnt(tag_AvrModbusMasterSlavePollData *PollData)
697+
{
698+
const tU8 HeaderSize = 5; //slave address / function / byte count / crc / crc
699+
tU16 Cnt;
700+
701+
switch(PollData->PollFunction)
702+
{
703+
case AVR_MODBUS_ReadInputStatus :
704+
case AVR_MODBUS_ReadCoilStatus :
705+
Cnt = PollData->NumberOfRegister / 8;
706+
if(PollData->NumberOfRegister % 8) Cnt++;
707+
Cnt += HeaderSize;
708+
break;
709+
710+
default :
711+
case AVR_MODBUS_ReadInputRegister :
712+
case AVR_MODBUS_ReadHoldingRegister :
713+
Cnt = (PollData->NumberOfRegister * 2) + HeaderSize;
714+
break;
715+
}
716+
717+
return Cnt;
718+
}
719+
/*********************************************************************************/
695720
static void MasterReceive(tag_AvrModbusMasterCtrl *Master)
696721
{
697722
tU16 Crc16;
@@ -710,10 +735,10 @@ static void MasterReceive(tag_AvrModbusMasterCtrl *Master)
710735
- Slave의 응답을 처리함.
711736
*/
712737

713-
Slave = AvrModbusMasterFindSlaveById(Master, RxQue->Buf[0]);
738+
Slave = Master->SlavePoll;
714739
PollData = &Slave->PollData[Slave->PollDataIndex];
715740

716-
if((Slave != null) && (RxQue->Buf[1] == PollData->PollFunction))
741+
if((Slave != null) && (Master->IsPollingSend == true) && (RxQue->Buf[1] == PollData->PollFunction) && (GetExpectRxCnt(PollData) == RxQue->Ctr))
717742
{
718743
Crc16 = Crc16Check(RxQue->OutPtr, RxQue->Buf, &RxQue->Buf[RxQue->Size - 1], RxQue->Ctr - 2);
719744

@@ -730,6 +755,7 @@ static void MasterReceive(tag_AvrModbusMasterCtrl *Master)
730755
}
731756

732757
Slave->NoResponseCnt = 0;
758+
Master->IsPollingSend = false;
733759
if(Master->Bit.InitRxUserException == true)
734760
{
735761
Master->UserException(Slave->Id);
@@ -780,7 +806,7 @@ tU8 AvrModbusMasterGeneralInit(tag_AvrModbusMasterCtrl *Master, tag_AvrUartCtrl
780806
}
781807
//tag_AvrModbusMasterSlavePollData 동적 할당
782808

783-
Master->WriteCmdPending = false;
809+
Master->IsWriteCmdPending = false;
784810
Master->Uart = Uart;
785811
Master->MaxSlave = MaxSlave;
786812
Master->Tick_us = MasterProcTick_us;
@@ -1065,9 +1091,9 @@ void AvrModbusMasterProc(tag_AvrModbusMasterCtrl *Master)
10651091
else
10661092
{
10671093
Master->PollCnt = Master->PollDelay;
1068-
if(Master->WriteCmdPending)
1094+
if(Master->IsWriteCmdPending)
10691095
{
1070-
Master->WriteCmdPending = false;
1096+
Master->IsWriteCmdPending = false;
10711097
AvrUartStartTx(Master->Uart);
10721098
}
10731099
else
@@ -1100,12 +1126,12 @@ tU8 AvrModbusMasterWriteSingle(tag_AvrModbusMasterCtrl *Master, tU8 SlaveId, enu
11001126
- 현재 이 함수는 AVR_MODBUS_ForceSingleCoil, AVR_MODBUS_PresetSingleRegister 펑션만 지원한다.
11011127
*/
11021128

1103-
if((Master->Bit.InitComplete == false) || (Master->AddedSlave == 0) || (Master->WriteCmdPending == true) || ((Func != AVR_MODBUS_ForceSingleCoil) && (Func != AVR_MODBUS_PresetSingleRegister)))
1129+
if((Master->Bit.InitComplete == false) || (Master->AddedSlave == 0) || (Master->IsWriteCmdPending == true) || ((Func != AVR_MODBUS_ForceSingleCoil) && (Func != AVR_MODBUS_PresetSingleRegister)))
11041130
{
11051131
return false;
11061132
}
11071133

1108-
Master->WriteCmdPending = true;
1134+
Master->IsWriteCmdPending = true;
11091135
if(Func == AVR_MODBUS_ForceSingleCoil){ PresetData = PresetData ? 0xFF00 : 0x0000; }
11101136

11111137
AvrUartPutChar(Master->Uart, SlaveId);
@@ -1164,12 +1190,12 @@ tU8 AvrModbusMasterPresetMultiple(tag_AvrModbusMasterCtrl *Master, tU8 SlaveId,
11641190
- 인수로 받은 SlaveId에 PresetMultiple 명령을 보냄.
11651191
*/
11661192

1167-
if((Master->Bit.InitComplete == false) || (Master->AddedSlave == 0) || (Master->WriteCmdPending == true))
1193+
if((Master->Bit.InitComplete == false) || (Master->AddedSlave == 0) || (Master->IsWriteCmdPending == true))
11681194
{
11691195
return false;
11701196
}
11711197

1172-
Master->WriteCmdPending = true;
1198+
Master->IsWriteCmdPending = true;
11731199

11741200
AvrUartPutChar(Master->Uart, SlaveId);
11751201
AvrUartPutChar(Master->Uart, AVR_MODBUS_PresetMultipleRegister);

AvrModbus.h

Lines changed: 23 additions & 13 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 20220114
12+
#define AVR_MODBUS_REVISION_DATE 20220209
1313
/*********************************************************************************/
1414
/** REVISION HISTORY **/
1515
/*
16+
2022. 02. 09. - Master 수신로직 개선. 수신 데이터 수 비교 추가 및 1회 폴링에 1회 수신처리하도록 변경.
17+
Jeong Hyun Gu - support legacy define (enum_AvrModbusFunction)
18+
1619
2022. 01. 14. - AvrModbusSlaveProc() error respone 0x03 삭제.
1720
Jeong Hyun Gu
1821
@@ -165,6 +168,12 @@ typedef enum
165168
AVR_MODBUS_PresetMultipleRegister = 0x10,
166169
}enum_AvrModbusFunction;
167170

171+
//support legacy define.
172+
#define AVR_MODBUS_ReadHolding AVR_MODBUS_ReadHoldingRegister
173+
#define AVR_MODBUS_ReadInput AVR_MODBUS_ReadInputRegister
174+
#define AVR_MODBUS_PresetSingle AVR_MODBUS_PresetSingleRegister
175+
#define AVR_MODBUS_PresetMultipleRegister AVR_MODBUS_PresetMultipleRegister
176+
168177
/*********************************************************************************/
169178
/**Struct**/
170179

@@ -174,13 +183,13 @@ typedef struct tag_AvrModbusSlaveCtrl
174183
{
175184
struct
176185
{
177-
tU8 InitGeneral : 1;
178-
tU8 InitCheckOutRange : 1;
179-
tU8 InitUserException : 1;
180-
tU8 InitPreUserException : 1;
181-
tU8 InitCustomFrameCheck : 1;
182-
tU8 InitSerialNumber : 1;
183-
tU8 InitComplete : 1;
186+
tU8 InitGeneral : 1;
187+
tU8 InitCheckOutRange : 1;
188+
tU8 InitUserException : 1;
189+
tU8 InitPreUserException : 1;
190+
tU8 InitCustomFrameCheck : 1;
191+
tU8 InitSerialNumber : 1;
192+
tU8 InitComplete : 1;
184193
}Bit;
185194

186195
tag_AvrUartCtrl *Uart;
@@ -220,13 +229,14 @@ typedef struct
220229
{
221230
struct
222231
{
223-
tU8 InitGeneral : 1;
224-
tU8 InitRxUserException : 1;
225-
tU8 InitComplete : 1;
226-
tU8 PollDataAllocFail : 1;
232+
tU8 InitGeneral : 1;
233+
tU8 InitRxUserException : 1;
234+
tU8 InitComplete : 1;
235+
tU8 PollDataAllocFail : 1;
227236
}Bit;
228237

229-
tU8 WriteCmdPending : 1;
238+
tU8 IsWriteCmdPending : 1;
239+
tU8 IsPollingSend : 1;
230240

231241
tag_AvrUartCtrl *Uart;
232242

0 commit comments

Comments
 (0)