2

我正在使用带有官方以太网屏蔽的 Arduino (duemilanove) 将数据发送到控制器以控制 LED 矩阵。我正在尝试通过获取桌面上 32 位值中的 4 个字节并将其作为 4 个连续字节发送到 arduino 来向控制器发送一些原始的 32 位无符号整数值(unix 时间戳)。但是,当字节值大于 127 时,以太网客户端库返回的值是 63。

以下是我在 arduino 方面所做的一个基本示例。为了整洁,有些东西已被删除。

byte buffer[32];
memset(buffer, 0, 32);

int data;
int i=0;

data = client.read();
while(data != -1 && i < 32)
{
  buffer[i++] = (byte)data;
  data = client.read();
}

因此,每当输入字节大于 127 时,变量“data”最终将设置为 63!起初我认为问题出在更远的地方(缓冲区曾经是字符而不是字节)但是当我在读取后立即打印出“数据”时,它仍然是 63。

有什么想法可能导致这种情况吗?我知道 client.read() 应该输出 int 并在内部从套接字读取数据作为 uint8_t 这是一个完整的字节和无符号,所以我应该能够至少去 255 ...

编辑:对,汉斯。没有意识到 Encoding.ASCII.GetBytes 只支持前 7 位而不是全部 8。

4

3 回答 3

4

我更倾向于怀疑传输端。你确定发射端工作正常吗?您是否通过wireshark捕获或类似的方式进行了验证?

于 2011-01-07T05:07:26.520 回答
3

63 is the ASCII code for ?. There's some relevance to the values, ASCII doesn't have character codes for values over 127. An ASCII encoder commonly replaces invalid codes like this with a question mark. Default behavior for the .NET Encoding.ASCII encoder for example.

It isn't exactly clear where that might happen. Definitely not in your snippet. Probably on the other end of the wire. Write bytes, not characters.

于 2011-01-07T05:26:46.963 回答
0

+1 for Hans Passant and Karl Bielefeldt.

Can you just send the data without encoding? How is the data being sent? TCP/UDP/IP/Ethernet definitely support sending binary data without restriction. If this isn't possible, perhaps converting the data to hex will solve the problem. Base64 will also work (better) but is considerably more work. For small amounts of data, hex is probably the easiest and fastest solution.

+1 again to Karl and Ben for mentioning wireshark. Invaluable for debugging network problems like this.

于 2013-11-06T16:01:10.427 回答