使用 I2C windows 物联网和 Arduino 写入数据

Write data using I2C windows iot and Arduino

所以我有一个 Arduino 和 Raspberry pi 3 连接了 windows 物联网,如下图所示: [][1

我想通过 I2C 使用 RPI 作为主设备读取和写入 Arduino 从设备。到目前为止我有以下代码:

C# 大师:

private I2cDevice arduio; // Used to Connect to Arduino
private DispatcherTimer timer = new DispatcherTimer();

public MainPage()
{
    this.InitializeComponent();
    Initialiasecom();
}

public async void Initialiasecom()
{
    var settings = new I2cConnectionSettings(0x40); // Slave Address of Arduino Uno 
    settings.BusSpeed = I2cBusSpeed.FastMode; // this bus has 400Khz speed

    string aqs = I2cDevice.GetDeviceSelector("I2C1"); // This will return Advanced Query String which is used to select i2c device
    var dis = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs);
    arduio = await I2cDevice.FromIdAsync(dis[0].Id, settings);

    timer.Tick += Timer_Tick; // We will create an event handler 
    timer.Interval = new TimeSpan(0, 0, 0, 0, 500); // Timer_Tick is executed every 500 milli second
    timer.Start();
}

private async void Timer_Tick(object sender, object e)
{
    byte[] response = new byte[2];
    byte[] request = new byte[] { 0x40, 0x40 };

    try
    {
        arduio.Read(response); // this funtion will request data from Arduino and read it
        arduio.Write(request); // this function will send data to Arduino
    }
    catch (Exception p)
    {
        //Windows.UI.Popups.MessageDialog msg = new Windows.UI.Popups.MessageDialog(p.Message);

        Debug.WriteLine(p.Message);

        //await msg.ShowAsync(); // this will show error message(if Any)
    }

    text.Text = response[0].ToString();
}

从 Arduino:

include <Wire.h>
#define SLAVE_ADDRESS 0x40

byte response[1]; // this data is sent to PI

void setup() {
    Wire.begin(SLAVE_ADDRESS);                // join i2c bus with address slaveAddress
    Wire.onReceive(I2CReceived);
    Wire.onRequest(I2CRequest);
}

void loop() {
    delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void I2CRequest() {

    response[0] = (byte)17;
    Wire.write(response, 2); // return data to PI
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void I2CReceived(int howMany) {
    Serial.println("test");

    while (1 < Wire.available()) { // loop through all but the last
        char c = Wire.read(); // receive byte as a character
        Serial.print(c);         // print the character
    }
    int x = Wire.read();    // receive byte as an integer
    Serial.println(x);         // print the integer
}

我可以成功地从 Arduino 读取数据并将它们打印到 windows IoT 上的文本块。但我也想给 Arduino 写文本。我尝试了一些但没有用。有人可以解释一下如何写入数据吗?

我真的需要一些帮助而且我不是专业的程序员所以请尽可能简单。如果我当前的代码有问题,请发表评论,以便我可以尝试改进我的代码。

我认为这个问题是由于 Serial.begin 丢失造成的,实际上从 master 发送到 slaver 的数据已经收到,只是没有 printed.Please 在 slaver 上用下面的代码重试。

#include <Wire.h>
#define SLAVE_ADDRESS 0x40

byte response[1]; // this data is sent to PI
static int index = 0;

void setup() {
    Serial.begin(9600); 
    Wire.begin(SLAVE_ADDRESS);                // join i2c bus with address         slaveAddress
    Wire.onReceive(I2CReceived);
    Wire.onRequest(I2CRequest);    
}

void loop() {
    delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void I2CRequest() {
    Serial.println("I2C-Request");
    response[0] = (byte) index ++ ;
    Wire.write(response, 2); // return data to PI
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void I2CReceived(int howMany) {
    Serial.println("I2C-Received");

    while (1 < Wire.available()) { // loop through all but the last
      char c = Wire.read(); // receive byte as a character
      Serial.print(c);         // print the character
    }
    int x = Wire.read();    // receive byte as an integer
    Serial.println(x);         // print the integer
}

我用我的 Arduino UNO 测试过,它 works.The 接收到的数据可以显示在串行监视器中。