(coils/discrete) do not shift the readout/write by n%8 for RTU and TCP (#1)

Co-authored-by: Mateusz Grzywacz <mateusz.grzywacz@evalan.com>
This commit is contained in:
Mateusz Grzywacz
2023-08-30 15:18:04 +02:00
committed by aleks
parent aa93eecf57
commit 8dadaac490
2 changed files with 8 additions and 8 deletions

View File

@ -566,7 +566,7 @@ eMBErrorCode eMBRegCoilsCBSerialMaster(UCHAR* pucRegBuffer, USHORT usAddress,
switch (eMode) { switch (eMode) {
case MB_REG_WRITE: case MB_REG_WRITE:
while (usCoils > 0) { while (usCoils > 0) {
UCHAR ucResult = xMBUtilGetBits((UCHAR*)pucRegCoilsBuf, iRegIndex, 1); UCHAR ucResult = xMBUtilGetBits((UCHAR*)pucRegCoilsBuf, iRegIndex - (usAddress % 8), 1);
xMBUtilSetBits(pucRegBuffer, iRegIndex - (usAddress % 8) , 1, ucResult); xMBUtilSetBits(pucRegBuffer, iRegIndex - (usAddress % 8) , 1, ucResult);
iRegIndex++; iRegIndex++;
usCoils--; usCoils--;
@ -575,7 +575,7 @@ eMBErrorCode eMBRegCoilsCBSerialMaster(UCHAR* pucRegBuffer, USHORT usAddress,
case MB_REG_READ: case MB_REG_READ:
while (usCoils > 0) { while (usCoils > 0) {
UCHAR ucResult = xMBUtilGetBits(pucRegBuffer, iRegIndex - (usAddress % 8), 1); UCHAR ucResult = xMBUtilGetBits(pucRegBuffer, iRegIndex - (usAddress % 8), 1);
xMBUtilSetBits((uint8_t*)pucRegCoilsBuf, iRegIndex, 1, ucResult); xMBUtilSetBits((uint8_t*)pucRegCoilsBuf, iRegIndex - (usAddress % 8), 1, ucResult);
iRegIndex++; iRegIndex++;
usCoils--; usCoils--;
} }
@ -621,7 +621,7 @@ eMBErrorCode eMBRegDiscreteCBSerialMaster(UCHAR * pucRegBuffer, USHORT usAddress
iRegBitIndex = (USHORT)(usAddress) % 8; // Get bit index iRegBitIndex = (USHORT)(usAddress) % 8; // Get bit index
while (iNReg > 1) while (iNReg > 1)
{ {
xMBUtilSetBits(pucDiscreteInputBuf++, iRegBitIndex, 8, *pucRegBuffer++); xMBUtilSetBits(pucDiscreteInputBuf++, iRegBitIndex - ((USHORT)(usAddress) % 8), 8, *pucRegBuffer++);
iNReg--; iNReg--;
} }
// last discrete // last discrete
@ -629,7 +629,7 @@ eMBErrorCode eMBRegDiscreteCBSerialMaster(UCHAR * pucRegBuffer, USHORT usAddress
// xMBUtilSetBits has bug when ucNBits is zero // xMBUtilSetBits has bug when ucNBits is zero
if (usNDiscrete != 0) if (usNDiscrete != 0)
{ {
xMBUtilSetBits(pucDiscreteInputBuf, iRegBitIndex, usNDiscrete, *pucRegBuffer++); xMBUtilSetBits(pucDiscreteInputBuf, iRegBitIndex - ((USHORT)(usAddress) % 8), usNDiscrete, *pucRegBuffer++);
} }
} else { } else {
eStatus = MB_ENOREG; eStatus = MB_ENOREG;

View File

@ -685,7 +685,7 @@ eMBErrorCode eMBRegCoilsCBTcpMaster(UCHAR *pucRegBuffer, USHORT usAddress,
switch (eMode) { switch (eMode) {
case MB_REG_WRITE: case MB_REG_WRITE:
while (usCoils > 0) { while (usCoils > 0) {
UCHAR ucResult = xMBUtilGetBits((UCHAR*)pucRegCoilsBuf, iRegIndex, 1); UCHAR ucResult = xMBUtilGetBits((UCHAR*)pucRegCoilsBuf, iRegIndex - (usAddress % 8), 1);
xMBUtilSetBits(pucRegBuffer, iRegIndex - (usAddress % 8) , 1, ucResult); xMBUtilSetBits(pucRegBuffer, iRegIndex - (usAddress % 8) , 1, ucResult);
iRegIndex++; iRegIndex++;
usCoils--; usCoils--;
@ -694,7 +694,7 @@ eMBErrorCode eMBRegCoilsCBTcpMaster(UCHAR *pucRegBuffer, USHORT usAddress,
case MB_REG_READ: case MB_REG_READ:
while (usCoils > 0) { while (usCoils > 0) {
UCHAR ucResult = xMBUtilGetBits(pucRegBuffer, iRegIndex - (usAddress % 8), 1); UCHAR ucResult = xMBUtilGetBits(pucRegBuffer, iRegIndex - (usAddress % 8), 1);
xMBUtilSetBits((uint8_t*)pucRegCoilsBuf, iRegIndex, 1, ucResult); xMBUtilSetBits((uint8_t*)pucRegCoilsBuf, iRegIndex - (usAddress % 8), 1, ucResult);
iRegIndex++; iRegIndex++;
usCoils--; usCoils--;
} }
@ -738,7 +738,7 @@ eMBErrorCode eMBRegDiscreteCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
iRegBitIndex = (USHORT)(usAddress) % 8; // Get bit index iRegBitIndex = (USHORT)(usAddress) % 8; // Get bit index
while (iNReg > 1) while (iNReg > 1)
{ {
xMBUtilSetBits(pucDiscreteInputBuf++, iRegBitIndex, 8, *pucRegBuffer++); xMBUtilSetBits(pucDiscreteInputBuf++, iRegBitIndex - ((USHORT)(usAddress) % 8), 8, *pucRegBuffer++);
iNReg--; iNReg--;
} }
// last discrete // last discrete
@ -746,7 +746,7 @@ eMBErrorCode eMBRegDiscreteCBTcpMaster(UCHAR * pucRegBuffer, USHORT usAddress,
// xMBUtilSetBits has bug when ucNBits is zero // xMBUtilSetBits has bug when ucNBits is zero
if (usNDiscrete != 0) if (usNDiscrete != 0)
{ {
xMBUtilSetBits(pucDiscreteInputBuf, iRegBitIndex, usNDiscrete, *pucRegBuffer++); xMBUtilSetBits(pucDiscreteInputBuf, iRegBitIndex - ((USHORT)(usAddress) % 8), usNDiscrete, *pucRegBuffer++);
} }
} else { } else {
eStatus = MB_ENOREG; eStatus = MB_ENOREG;