freemodbus eMBRegCoilsCB函数体实例

时间:2016-10-25 11:54:50

标签: c++ c embedded modbus

有人可以解释如何在 xMBUtilGetBits() 中使用 xMBUtilSetBits() eMBRegCoilsCB() 吗?我正在使用freemodbus作为modbus rtu slave驱动程序。

我无法添加我的代码,因为它太大但您可以在演示中看到示例(链接如下)。在所有示例 eMBRegCoilsCB() 未填写。

eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
{
    return MB_ENOREG;
}

eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
    return MB_ENOREG;
}

编辑

如果尝试使用偏移量>写入(0x15)某些位,则代码无效。 0

if ( ( usAddress >= REG_COILS_START )
    && ( usAddress + usNCoils <= REG_COILS_START + REG_COILS_NREGS ) )
{
    iRegIndex = ( int ) ( usAddress - usRegCoilsStart );

    switch ( eMode )
    {
        case MB_REG_READ:
        {
            while ( usNCoils > 0 )
            {
                UCHAR ucResult = xMBUtilGetBits( usRegCoilsBuf, iRegIndex, 1 );

                xMBUtilSetBits( pucRegBuffer, iRegIndex, 1, ucResult );

                iRegIndex++;
                usNCoils--;
            }

            break;
        }

        case MB_REG_WRITE:
        {                
            while ( usNCoils > 0 )
            {
                UCHAR ucResult = xMBUtilGetBits( pucRegBuffer, iRegIndex, 1 );

                xMBUtilSetBits( usRegCoilsBuf, iRegIndex, 1, ucResult );

                iRegIndex++;
                usNCoils--;
            }

            break;
        }
    }
}
else
{
    eStatus = MB_ENOREG;
}

链接

  1. freemodbus

2 个答案:

答案 0 :(得分:1)

尝试这个。我使REG_COILS_START = 0,以简化地址魔术。但即使在阅读和书写时偏移量> 0,它也对我有用。

uint8_t modbus_CS[10];
#define TABLE_CS_SIZE ( sizeof(modbus_CS) * sizeof(modbus_CS[0]) )

eMBErrorCode eMBRegCoilsCB(UCHAR * pucRegBuffer, USHORT usAddress,
    USHORT usNCoils, eMBRegisterMode eMode)
{
    usAddress -= 1; /* to c-style address */

    /* check if we away of table size */
    if (usAddress + usNCoils > TABLE_CS_SIZE) {
        return MB_ENOREG;
    }

    switch (eMode)
    {
        case MB_REG_WRITE:
            for (int i = 0; i < usNCoils; i++) {
                UCHAR wbit = xMBUtilGetBits(pucRegBuffer, i, 1 );
                xMBUtilSetBits( modbus_CS, usAddress+i, 1, wbit );
            }
            break;
        case MB_REG_READ:
            for (int i = 0; i < usNCoils; i++) {
                UCHAR rbit = xMBUtilGetBits( modbus_CS, usAddress+i, 1 );
                xMBUtilSetBits( pucRegBuffer, i, 1, rbit );
            }
            break;
    }

    return MB_ENOERR;
}

答案 1 :(得分:0)

eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
{
    eMBErrorCode eStatus = MB_ENOERR;
    int iRegIndex;

    if ( ( usAddress >= REG_COILS_START )
        && ( usAddress + usNCoils <= REG_COILS_START + REG_COILS_NREGS ) )
    {
        iRegIndex = ( int ) ( usAddress - usRegCoilsStart );

        switch ( eMode )
        {
            case MB_REG_READ:
            {
                while ( usNCoils > 0 )
                {
                    UCHAR ucResult = xMBUtilGetBits( usRegCoilsBuf, iRegIndex, 1 );

                    xMBUtilSetBits( pucRegBuffer, iRegIndex - ( usAddress - usRegCoilsStart ), 1, ucResult );

                    iRegIndex++;
                    usNCoils--;
                }

                break;
            }

            case MB_REG_WRITE:
            {                
                while ( usNCoils > 0 )
                {
                    UCHAR ucResult = xMBUtilGetBits( pucRegBuffer, iRegIndex - ( usAddress - usRegCoilsStart ), 1 );

                    xMBUtilSetBits( usRegCoilsBuf, iRegIndex, 1, ucResult );

                    iRegIndex++;
                    usNCoils--;
                }

                break;
            }
        }
    }
    else
    {
        eStatus = MB_ENOREG;
    }

    return eStatus;
}