freeemodbus eMBRegCoilsCB函数体示例
freemodbus eMBRegCoilsCB function body example
有人能解释一下如何在 eMBRegCoilsCB()
中使用 xMBUtilGetBits()
和 xMBUtilSetBits()
吗?我使用freemodbus作为modbus rtu从驱动程序。
我不能添加我的代码,因为它太大了,但你可以在演示中看到示例(下面的链接)。在所有示例中 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;
}
与
- freemodbus
试试这个。我让REG_COILS_START = 0来简化地址魔术。但是对于我来说,即使在读取和写入时offset>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;
}
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;
}
相关文章:
- 用C++在单独的头文件中完成函数体
- 我可以在 C++ 中的函数体之外进行操作吗?
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- binary_search() 函数在我的函数体中不起作用
- 如何显示函数开始、结束行和函数体?
- 主函数体未检测到对重载可变变量模板化函数C++的调用
- 错误:函数声明符之后的预期函数体
- C++编译器如何检测非常量函数体
- 使用委托调用构造函数和在另一个构造函数体中调用的区别
- Flex和Bison忽略了函数体
- 我看到将我的类成员函数指定为内联实际上会增加执行时间,即使函数体非常小
- 为什么将函数体放置在单独的源代码文件而不是标题中的C/C 中
- 类(模板)成员函数体内部的ADL查找
- 如果未设置编译符号,如何排除方法?如何传递可变数量的参数并在函数体中格式化它们
- constexpr函数体c++14中可以有非文字类型的定义变量吗
- 内联失败:可以在链接时覆盖函数体
- C++函数体中的Const变量
- 在类定义之外的模板类成员函数体中,何时需要模板参数
- c++--直接--访问非成员函数体中的类成员
- 是否可以在初始化列表以外的构造函数体中进行一些计算