简体   繁体   English

使用I2C发送0x00

[英]Sending 0x00 using I2C

I am using the Wire class to have two Arduino Unos communicate using I2C. 我正在使用Wire类让两个Arduino Unos使用I2C进行通信。 It seems that the Wire class ends transmission on a value of 0. So if I send bytes 0x01, 0x02, 0x00, and 0x04, the master receives: 0x01, 0x02, 0xFF, 0xFF. 似乎Wire类在值为0时结束了传输。因此,如果我发送字节0x01、0x02、0x00和0x04,则主机将接收:0x01、0x02、0xFF,0xFF。

It seems odd that a communication network designed for processor communication cannot send a 0x00. 设计用于处理器通信的通信网络无法发送0x00似乎很奇怪。 All the examples for I2C that I have seen use ASCII only. 我看到的所有I2C示例仅使用ASCII。 Inter-processor communication restricted to ASCII does not seem to make any sense. 仅限于ASCII的处理器间通信似乎没有任何意义。 Am I missing something with I2C or is this just an implementation limitation of the Arduino Wire class? 我是否缺少I2C的东西,或者这仅仅是Arduino Wire类的实现限制?

Master

void loop() {
  Wire.requestFrom(2, 4); 

  while(Wire.available()) { 
    byte b = (byte) Wire.read(); 
    Serial.println(b); 
  }

  delay(1000);
}

Slave 奴隶

void requestEvent() {
  char bytes[4] = {0x01,0x02,0x00,0x04};
  Wire.write(bytes);
}

The results are: 1 2 255 255. 结果是:1 2 255 255。

When that 0x00 was 0x03 I got the expected: 1 2 3 4. 当0x00为0x03时,我得到了预期的结果:1 2 3 4。

You are passing an array of char s to Wire.write() ... This makes Wire treat the argument as a null-terminated string, and therefore the NULL byte terminates the transmission. 您正在将一个char数组传递给Wire.write() ...这使Wire将参数视为以空字符结尾的字符串,因此NULL字节终止了传输。 If you want to send byte s, call the write() function once for each byte, passing it byte types. 如果要发送byte s,请为每个字节调用一次write()函数,并将byte类型传递给它。

Alternatively, you can call the write(const uint8_t *, size_t); 另外,您可以调用write(const uint8_t *, size_t); version: you pass it a pointer to / array of bytes, but you also have to give it a size of your data array. 版本:向它传递一个指向/字节数组的指针,但还必须给它一个数据数组的大小。

在调用Wire.write()之前,是否发生了Wire.begin()和Wire.beginTransmission()(具有正确的地址)的发生?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM