关于 C++ 的问题

发布于 2024-11-05 04:26:52 字数 583 浏览 3 评论 0原文

对于你们中的一些人来说,这可能是一个相当简单的问题。

我正在查看以下串行读取函数,但我不太理解 &prefix[2] 在这里做什么。这是否意味着只能填充两个字节还是其他什么?

我还应该提到这是玩家/舞台平台的一部分。

while (1)
{
  cnt = 0;
  while (cnt != 1)
  {
    if ((cnt += read(fd, &prefix[2], 1)) < 0)
    {
      perror("Error reading packet header from robot connection: P2OSPacket():Receive():read():");
      return (1);
    }
  }

  if (prefix[0] == 0xFA && prefix[1] == 0xFB)
  {
    break;
  }

  GlobalTime->GetTimeDouble(&timestamp);

  prefix[0] = prefix[1];
  prefix[1] = prefix[2];

}

This is perhaps quite a simple one for some of you.

I was looking at the following serial read function and I can't quite comprehend what &prefix[2] does here. Does it mean only two bytes can be filled or something else ?

I should also mention this is part of the player/stage platform.

while (1)
{
  cnt = 0;
  while (cnt != 1)
  {
    if ((cnt += read(fd, &prefix[2], 1)) < 0)
    {
      perror("Error reading packet header from robot connection: P2OSPacket():Receive():read():");
      return (1);
    }
  }

  if (prefix[0] == 0xFA && prefix[1] == 0xFB)
  {
    break;
  }

  GlobalTime->GetTimeDouble(×tamp);

  prefix[0] = prefix[1];
  prefix[1] = prefix[2];

}

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

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

发布评论

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

评论(4

瀟灑尐姊 2024-11-12 04:26:52

该片段实现了大小为 3 的移位寄存器。

最旧的值位于 prefix[0] 中,最新的值位于 prefix[2] 中。这就是为什么 prefix[2] 的地址被传递给函数 read() 的原因。

循环离开,当前两个字节已被 FA FB 时,当前(最后接收到的)字节位于 prefix[2] 中。如果无法读取任何内容(read 的返回值不同于 1),则该函数将保留在该点之前。

当您无法预测同步字符FA FB前面是否接收到其他字节时,使用此移位寄存器。每次读取操作读取三个字节将不允许同步数据流中的某个位置。

This fragment implements a shift register of the size 3.

The oldest value is in prefix[0] and the latest in prefix[2]. That's why the address of prefix[2] is passed to the function read().

The loop is left, when the previous two bytes have been FA FB, the current (last received) byte is in prefix[2]. The function is left before that point, if nothing could be read (the return value of read differs from 1).

This shift register is used when you can't predict if other bytes are received in front of the sync characters FA FB. Reading three bytes with each read operation would not allow to synchronize somewhere in a data stream.

浮世清欢 2024-11-12 04:26:52

调用 read(fd, &prefix[2], 1) 只是意味着“在 prefix[2] 中存储单个字节”。

一般来说,&a[n](&a) + n 是相同的地址

The call read(fd, &prefix[2], 1) just means "store a single byte in prefix[2]".

In general, &a[n] is the same address as (&a) + n

蝶…霜飞 2024-11-12 04:26:52

它正在读入一个名为 prefix 的数组,从距数组开头两个位置的偏移量开始。在这种情况下,仅读取一个字符,但可以读取更多字符。

It is reading into an array called prefix, starting at an offset two places from the start of the array. In this case, only a single character is being read, but one could read more.

倒带 2024-11-12 04:26:52

看起来这段代码正在读取串行通信流,等待标有 FAFB 的标头开始。 while 循环将单个字符读取到 prefix[2] 中,并在数组中向后打乱它们。

我认为使用 &prefix[2] 是一个技巧,当 while< 时,它可以使标头中的下一个字符出现在 prefix 数组中/code> 循环通过 break 退出。

It looks like this piece of code is reading a serial communications stream waiting for the start of a header which is marked with FA, FB. The while loop reads characters singly into prefix[2] and shuffles them backwards through the array.

I think that the use of &prefix[2] is a trick which enables the next character in the header to appear in the prefix array when the while loop quits through the break.

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