cwalter-at / freemodbus

BSD licensed MODBUS RTU/ASCII and TCP slave
720 stars 379 forks source link

When Write MULTIPLE_COILS #2

Closed ixuhan closed 5 years ago

ixuhan commented 5 years ago

if i send : 01 0F 00 00 00 03 01 0f CF 53 i means wirte three coils to bit 1,but it returns four bits to bit 1, if we should just get three bits in 0x0f ?

JoelStienlet commented 5 years ago

Hello, Do you use xMBUtilSetBits() of mbutils.c to set the bits when in the eMBRegCoilsCB() callback?

ixuhan commented 5 years ago

of course. i think eMBRegCoilsCB(): case MB_REG_WRITE: while( iNCoils > 0 ) { UCHAR ucCoilsNums = iNCoils > 8 ? 8 : iNCoils; UCHAR ucByte = *pucRegBuffer++ & ( UCHAR )( ( 1 << ( UCHAR ) ucCoilsNums ) - 1 ); xMBUtilSetBits( pHTD_HoldRegSelfModelValue, usBitOffset, ( unsigned char )( iNCoils > 8 ? 8 : iNCoils ), ucByte ); iNCoils -= 8; usBitOffset += 8; }

JoelStienlet commented 5 years ago

I think this problem could be corrected by inserting: usValue &= ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 ); after usValue <<= usNPreBits; in xMBUtilSetBits().

ixuhan commented 5 years ago

ok,it works well,thks!