首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在c++中获取libmodbus中modbus_report_slave_id函数的分段错误

在c++中获取libmodbus中modbus_report_slave_id函数的分段错误
EN

Stack Overflow用户
提问于 2019-02-01 12:43:00
回答 1查看 349关注 0票数 0

我正在尝试使用modbus中的report_slave_ID接口获取从机ID信息。我也遇到了同样的分段错误。预期响应为38个字节。

(modbus命令: 28DF)

我可以通过termios使用相同的命令,并获得所需的输出,但使用libmodbus API:modbus_report_slave_id(modbus_t *ctx, uint8_t *dest)获得此命令的分段错误

代码语言:javascript
运行
复制
modbus_t *ctx;
int32_t i32_read_ret = 0;
int32_t i32_Index = 0;
uint8_t gui16_PonselInputRegVal[40]; 

//根据Modbus要求分配奇偶校验

代码语言:javascript
运行
复制
// Creating Modbus rtu 
ctx = modbus_new_rtu("/dev/ttyS1",9600, 0,8,2);

if(ctx == NULL)
{
   // DBG_FPRINTF(stderr,"Unable to create the libmodbus context for RTU: %s\n", modbus_strerror(errno));
    DBG_PRINTF("Unable to create the libmodbus context for RTU\n");
    DBG_PROCEDURE_LOG("Exiting Read_Meter function\n");
    return FAILURE;
}
else
{
    printf("Modbus context for RTU created\n");
}   

// Setting Slave ID 
if(modbus_set_slave(ctx,40) == -1)
{
    //DBG_FPRINTF(stderr, "Unable to set the slave ID in  context for RTU: %s\n", modbus_strerror(errno));
    DBG_PRINTF("Unable to set the slave ID in  context for RTU\n");
    modbus_free(ctx);
    DBG_PROCEDURE_LOG("Exiting Read_Meter function\n");
    return FAILURE;
}
else
{
    printf("Slave ID set successful\n");
}   

//TRUE: Debug message enable, FALSE: Debug message disable
modbus_set_debug(ctx,TRUE);

if(modbus_connect(ctx) == -1) 
{
   // DBG_FPRINTF(stderr, "modbus connection failed: %s\n", modbus_strerror(errno));
    DBG_PRINTF("modbus connection failed\n");
    modbus_free(ctx);
    DBG_PROCEDURE_LOG("\nExiting Read_Meter function\n");
    return FAILURE;
}

//modbus_report_slave_id(modbus_t *ctx, uint8_t *dest)

i32_read_ret=modbus_report_slave_id(ctx,gui16_PonselInputRegVal);
printf("i32_read_ret : %d\n",i32_read_ret);


    //Closing Modbus 
modbus_close(ctx);
modbus_free(ctx);

O/p:

代码语言:javascript
运行
复制
Modbus context for RTU created
Slave ID set successful
Opening /dev/ttyS1 at 9600 bauds (N, 8, 2)
[28][11][DF][BC]
Waiting for a confirmation...
<28><11><21><4E><65><70><68><65><6C><6F><2F><54><55><2F><54><65><6D><70><65><72><61><74><75><72><65><20><50><4F><4E><53><45><4C><20><20><20><FF><17><57>
Segmentation fault
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-04 15:56:08

使用库版本3.1.2后解决的问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54473016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档