c - 需要使用 c 为错误处理提供超时

标签 c

我正在开发一个代码来在两个 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/

相关文章:

c++ - C API 应该包含在可分发库中吗?

c - 调用线程时出错: expected 'void * (*)(void *)' but argument is of type

c - makecontext 函数中的指针说明

c++ - timespec 初始化时非阻塞 TCP 套接字挂起 (C++)

c - 库利 FFT 的实现问题

c - 为什么同样的float值和用atof函数转成float的字符串不相等?

使用 .C() : random value is the same every time 从 R 调用 C 代码

c - 这个双重嵌套循环的时间复杂度是多少?

c - 为什么我在 send() 函数中丢失了这个字节?

C 中的 Chmod 分配错误的权限