如何向I2C主设备发送多个32字节字符串?

时间:2017-02-25 15:59:39

标签: arduino i2c

我有多个可变长度字符串,每个字符串不超过32个字节,需要通过I2C在2个Arduino Nanos之间发送。我使用#来终止每个字符串。我可以成功发送和接收第一个字符串,但对于我的生活,我无法弄清楚如何发送后续字符串。我正在测试的样本如下所示:

    String answer = "12345,123.4,50.6,12345#";
    String answerV = "4.10,4.15,4.13,4.05,4.18,0#";

主码

    #include <Wire.h> 
    int esc = 0;
    int throttle = 80;    

    void setup() {
    Wire.begin();
    Serial.begin(115200);
    Serial.println("I2C Master ready!");
    esc = throttle * 100 / 180;
    }

    void loop() {
      delay(500);
      Serial.println("Receive data");
      Wire.requestFrom(9,32);
      String response = "";
      Serial.println(Wire.available());  
      while (Wire.available()) 
      {
        char b = Wire.read();
        response += b;
      }
      String dataString = response;
      int hashIndex = dataString.indexOf('#');
      String SportData = dataString.substring(0, hashIndex);
      Serial.println(SportData);

      String SDdata = String (esc) + "," + String(SportData);
      Serial.println(SDdata);
    }

奴隶代码

    #include <Wire.h>

    byte ANSWERSIZE= 22;

    String answer = "12345,123.4,50.6,12345#";
    String answerV = "4.10,4.15,4.13,4.05,4.18,0#";

    void setup() {
      Wire.begin(9);
      Wire.onRequest(requestEvent); // data request from Master
      Serial.begin(115200);
      Serial.println("I2C Slave ready!");
      ANSWERSIZE = answer.length();
    }

    void requestEvent() {
      byte response[ANSWERSIZE];
      for (byte i=0;i<ANSWERSIZE;i++) 
      {
        response[i] = (byte)answer.charAt(i);
      }
      Wire.write(response,sizeof(response));
    }

    void loop() {
      delay(50);
    }

有人可以告诉我这是怎么做到的吗?

1 个答案:

答案 0 :(得分:1)

一个简单的想法是跟踪调用requestEvent()的次数,并使用它来决定将哪些内容发送回

这是代码(n.b。我冒昧地优化了一下)

#include <Wire.h> 

/**
 * globals
 */

int esc = 0;
int throttle = 80;    
char buffer[33];

/**
 * setup & loop
 */

void setup()
{
    Serial.begin(115200);
    Wire.begin();

    esc = throttle * 100 / 180;
    buffer[32] = '\0';

    Serial.println("I2C Master ready!");
}

void loop()
{
    delay(500);

    Wire.requestFrom(9, 32);

    for (byte i = 0; i < 32 && Wire.available(); ++i)
    {
        buffer[i] = Wire.read();

        if (buffer[i] == '#') {
            buffer[i] = '\0';
            break;
        }
    }

    Serial.print(esc);
    Serial.print(',');
    Serial.println(buffer);
}

<强>从站:

#include <Wire.h>

/**
 * globals
 */

const byte DATA_SIZE = 2;
String data[DATA_SIZE] = {
    "12345,123.4,50.6,12345#",
    "4.10,4.15,4.13,4.05,4.18,0#"
};

/**
 * setup & loop
 */

void setup()
{
    Serial.begin(115200);
    Wire.begin(9);
    Wire.onRequest(requestEvent); // data request from Master

    Serial.println("I2C Slave ready!");
}

void loop()
{
    delay(50);
}

/**
 * help functions
 */

void requestEvent()
{
    static byte req_number = 0;

    Wire.write(reinterpret_cast<const unsigned char*>(data[req_number].c_str()),
               data[req_number].length());

    req_number = (req_number + 1) % DATA_SIZE;
}

注意: 我没有两台Arduino设备,因此我无法测试此代码。如果您发现了一些错误,请回报并重新修复。