关于 C++ 的问题
对于你们中的一些人来说,这可能是一个相当简单的问题。
我正在查看以下串行读取函数,但我不太理解 &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(×tamp);
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该片段实现了大小为 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 inprefix[2]
. That's why the address ofprefix[2]
is passed to the functionread()
.The loop is left, when the previous two bytes have been
FA FB
, the current (last received) byte is inprefix[2]
. The function is left before that point, if nothing could be read (the return value ofread
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.调用
read(fd, &prefix[2], 1)
只是意味着“在prefix[2]
中存储单个字节”。一般来说,
&a[n]
与(&a) + n
是相同的地址The call
read(fd, &prefix[2], 1)
just means "store a single byte inprefix[2]
".In general,
&a[n]
is the same address as(&a) + n
它正在读入一个名为 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.
看起来这段代码正在读取串行通信流,等待标有
FA
、FB
的标头开始。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
. Thewhile
loop reads characters singly intoprefix[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 theprefix
array when thewhile
loop quits through thebreak
.