用于接收器将无线介质访问到有限状态机的伪代码

发布于 2024-12-21 05:11:13 字数 507 浏览 1 评论 0原文

此伪代码适用于尝试访问无线介质以从传感器发送和接收数据的接收器。

  1. set pc = 0.01
  2. 发送轮询数据包
  3. 如果没有传感器响应轮询数据包,则设置 pc = min (pc + 0.01, 1.0)
  4. 如果成功接收到来自传感器的数据包传感器,将 pc 保持在当前值
  5. 如果两个或多个传感器之间存在冲突(如数据包损坏所示),则设置 pc = pc / 2
  6. 重复步骤 2

我已读过如何阅读 FSM 图 的链接,它确实对我有帮助传感器部分。但我仍然对尝试将上述伪代码转换为 FSM 感到困惑。

任何人都可以推荐一个链接或电子书,对将伪代码转换为 FSM 进行清晰的解释吗?

This pseudocode is for a sink that tries to access the wireless medium in send and receive data from sensors.

  1. set pc = 0.01
  2. send a polling packet
  3. If no sensor responds to polling packet, set pc = min (pc + 0.01, 1.0)
  4. If a data packet is successfully received from one of the sensors, keep pc at current value
  5. If there is a collision between two or more sensors as indicated by a corrupted data packet, set pc = pc / 2
  6. Repeat step 2

I have read the link by How to read a FSM diagram and it really helped me for the sensor part. But I am still confused about trying to convert the above pseudocode into an FSM.

Can anyone suggest a link or ebook that gives a clear explanation about converting the pseudocode into a FSM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-12-28 05:11:13

我不确定你真正在这里寻找什么;简单地编码会非常简单,而且这个问题看起来并不值得全面解决对我来说,FSM 表驱动方法

这是一些类似 C 的伪代码:

double pc = 0.01;
int sensorsfd;

void loop(void) {
    for (;;) {
        fd_set readfds, writefds, exceptfds;
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_ZERO(&exceptfds);
        FD_SET(sensorsfd, &readfds);

        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 1; /* 0.001 seconds */

        int r;

        send_polling_packet();

        r = select(sensorsfd+1, &readfds, &writefds, &exceptfds, &tv);

        if (r == -1 && errno == EINTR) {
            continue;
        } else if (r == -1) {
            perror("select() failed");
            exit(1);
        } else if (r == 0) {
        /* timeout expired */
            pc = min (pc + 0.01, 1.0);
        } else if (r == 1) {
        /* sensorsfd won't block when reading it */
            packet_t p = read_packet(sensorsfd);
        /* should also handle _no packet_ if the sensors
           socket returns EOF */
            if (packet_corrupted(p)) {
                pc /= 2;
            } else {
                handle_packet(p);
            }
        } else {
            /* error in program logic */
        }
    }
}

伪代码是指我刚刚编写的代码,并且没有测试机制
它。如果你的程序变得比这复杂得多,你可能会
想要将所有 select(2) 设置封装到它自己的函数中,并且
可能是处理来自传感器套接字的数据包的所有细节。

I'm not sure what you're really looking for here; coding this simply would be pretty straight forward, and this problem doesn't look like it deserves the full-blown FSM table-driven approach to me.

Here's some C-like pseudo-code:

double pc = 0.01;
int sensorsfd;

void loop(void) {
    for (;;) {
        fd_set readfds, writefds, exceptfds;
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_ZERO(&exceptfds);
        FD_SET(sensorsfd, &readfds);

        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 1; /* 0.001 seconds */

        int r;

        send_polling_packet();

        r = select(sensorsfd+1, &readfds, &writefds, &exceptfds, &tv);

        if (r == -1 && errno == EINTR) {
            continue;
        } else if (r == -1) {
            perror("select() failed");
            exit(1);
        } else if (r == 0) {
        /* timeout expired */
            pc = min (pc + 0.01, 1.0);
        } else if (r == 1) {
        /* sensorsfd won't block when reading it */
            packet_t p = read_packet(sensorsfd);
        /* should also handle _no packet_ if the sensors
           socket returns EOF */
            if (packet_corrupted(p)) {
                pc /= 2;
            } else {
                handle_packet(p);
            }
        } else {
            /* error in program logic */
        }
    }
}

Pseudo-code in the sense that I just wrote this and have no mechanism to test
it. If your program gets much more complicated than this, you would probably
want to encapsulate all the select(2) setup into a function of its own, and
possibly all the details of handling the packet from the sensor's socket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文