我正在开发一个代码来在两个 Controller 板之间进行通信。我正在将 9 字节消息从一个板传递到另一个板。需要在接收方定义错误处理,以便它将等待 9 字节值,直到发生超时。如果达到超时,控制应该从函数的第一行开始。
目前我已经定义了一行,例如
while (/*wait_loop_cnt++<= MAX_WAIT_LOOP &&*/ counter < length);
在我的代码中,但如果没有收到 9 个字节,它将无限地保持在同一个循环中。
请帮忙谢谢
最佳答案
试试这个:
const int length = 9;
int counter = 0;
int wait_loop_cnt = 0;
while (
wait_loop_cnt++ <= MAX_WAIT_LOOP &&
counter < length) /* NO semicolon here! */
{
if (read_byte_successfully(...))
{
++counter;
}
}
if (counter < length)
{
/* Handle case of to few bytes received here. */
}
关于c - 需要使用 c 为错误处理提供超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55339592/