news 2026/9/2 13:18:23

解决Modbus 主站回读数据时,eRcvState 一直保持在 STATE_M_RX_IDLE状态,导致无法正常接收数据的问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
解决Modbus 主站回读数据时,eRcvState 一直保持在 STATE_M_RX_IDLE状态,导致无法正常接收数据的问题

调试报告

问题描述

在 Modbus 主站回读数据时,eRcvState一直保持在STATE_M_RX_IDLE状态,导致无法正常接收数据。经过检查,发现问题出在主循环的轮询调用上。

BOOLxMBMasterRTUReceiveFSM(void){BOOL xTaskNeedSwitch=FALSE;UCHAR ucByte;assert_param((eSndState==STATE_M_TX_IDLE)||(eSndState==STATE_M_TX_XFWR));/* Always read the character. */(void)xMBMasterPortSerialGetByte((CHAR*)&ucByte);switch(eRcvState){/* If we have received a character in the init state we have to * wait until the frame is finished. */caseSTATE_M_RX_INIT:vMBMasterPortTimersT35Enable();break;/* In the error state we wait until all characters in the * damaged frame are transmitted. */caseSTATE_M_RX_ERROR:vMBMasterPortTimersT35Enable();break;/* In the idle state we wait for a new character. If a character * is received the t1.5 and t3.5 timers are started and the * receiver is in the state STATE_RX_RECEIVCE and disable early * the timer of respond timeout . */caseSTATE_M_RX_IDLE:/* In time of respond timeout,the receiver receive a frame. * Disable timer of respond timeout and change the transmiter state to idle. */vMBMasterPortTimersDisable();eSndState=STATE_M_TX_IDLE;usMasterRcvBufferPos=0;ucMasterRTURcvBuf[usMasterRcvBufferPos++]=ucByte;eRcvState=STATE_M_RX_RCV;/* Enable t3.5 timers. */vMBMasterPortTimersT35Enable();break;/* We are currently receiving a frame. Reset the timer after * every character received. If more than the maximum possible * number of bytes in a modbus frame is received the frame is * ignored. */caseSTATE_M_RX_RCV:if(usMasterRcvBufferPos<MB_SER_PDU_SIZE_MAX){ucMasterRTURcvBuf[usMasterRcvBufferPos++]=ucByte;}else{eRcvState=STATE_M_RX_ERROR;}vMBMasterPortTimersT35Enable();break;}returnxTaskNeedSwitch;}

在这个函数中,eRcvState 一直等于0x01 STATE_M_RX_IDLE,一次 case STATE_M_RX_RCV的分支都没执行

原因分析

原始代码中,Modbus 主站的轮询函数 eMBMasterPoll() 在主循环中调用频率过高,导致状态机无法正常切换。具体代码如下:

while(1){// 主机轮询eMBMasterPoll();// 非阻塞方式轮询,避免 HAL_Delay 占用 CPU。if(HAL_GetTick()-lastPollTime>=1000){test(MB_USER_READ_CALLBACK);lastPollTime=HAL_GetTick();// 延时1秒}HAL_Delay(MB_POLL_CYCLE_MS);// 延时1秒}

解决方案

将主循环中的轮询调用调整为合理的频率,确保状态机能够正常切换。调整后的代码如下:

while(1){MX_LWIP_Process();// Modbus 从机轮询eMBPoll();// 主站轮询,调整频率以避免回读问题if(HAL_GetTick()-lastPollTime1>=200){eMBMasterPoll();lastPollTime1=HAL_GetTick();// 延时200ms}// 非阻塞方式轮询,避免 HAL_Delay 占用 CPU。if(HAL_GetTick()-lastPollTime>=1000){test(MB_USER_READ_CALLBACK);lastPollTime=HAL_GetTick();// 延时1秒}}

总结

通过调整主循环中 Modbus 主站轮询的调用频率,解决了eRcvState一直保持在STATE_M_RX_IDLE的问题,确保了主站能够正常接收数据。此调试过程强调了合理设置轮询频率的重要性,避免因调用过于频繁导致状态机无法正常运作。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 17:51:31

29、实现 Linux 与 Windows 互操作及保障 Linux 系统安全

实现 Linux 与 Windows 互操作及保障 Linux 系统安全 在当今复杂的网络环境中,实现不同操作系统之间的互操作以及保障系统安全是至关重要的。本文将介绍 Samba 实现 Linux 与 Windows 互操作性,以及如何保障 Linux 系统的安全。 1. 使用 Samba 实现 Linux 与 Windows 互操作…

作者头像 李华
网站建设 2026/9/2 22:32:39

MoeKoeMusic:5大核心功能带你体验纯净音乐世界 [特殊字符]

MoeKoeMusic&#xff1a;5大核心功能带你体验纯净音乐世界 &#x1f3b5; 【免费下载链接】MoeKoeMusic 一款开源简洁高颜值的酷狗第三方客户端 An open-source, concise, and aesthetically pleasing third-party client for KuGou that supports Windows / macOS / Linux :el…

作者头像 李华
网站建设 2026/9/2 12:51:17

2.7亿参数改写边缘智能:Gemma 3 270M开启AI普惠时代

2.7亿参数改写边缘智能&#xff1a;Gemma 3 270M开启AI普惠时代 【免费下载链接】gemma-3-270m-it-qat-GGUF 项目地址: https://ai.gitcode.com/hf_mirrors/unsloth/gemma-3-270m-it-qat-GGUF 导语 谷歌DeepMind推出的Gemma 3 270M微型大模型&#xff0c;以241MB极致体…

作者头像 李华
网站建设 2026/9/2 15:05:46

Contract-Templates:免费开源合同模板的完整指南

Contract-Templates&#xff1a;免费开源合同模板的完整指南 【免费下载链接】Contract-Templates 项目地址: https://gitcode.com/gh_mirrors/co/Contract-Templates Contract-Templates是一个实用的开源项目&#xff0c;为用户提供专业的合同模板和商业文档解决方案。…

作者头像 李华
网站建设 2026/9/2 22:30:40

D2DX终极指南:暗黑破坏神II现代PC完美运行方案

D2DX终极指南&#xff1a;暗黑破坏神II现代PC完美运行方案 【免费下载链接】d2dx D2DX is a complete solution to make Diablo II run well on modern PCs, with high fps and better resolutions. 项目地址: https://gitcode.com/gh_mirrors/d2/d2dx 还在为经典《暗黑…

作者头像 李华