使用I2C windows iot和Arduino写入数据

时间:2018-03-13 17:51:00

标签: c# arduino raspberry-pi i2c windowsiot

所以我有一个连接了Windows IoT的Arduino和Raspberry pi 3,如下图所示: [enter image description here] [1

我希望使用RPI作为主设备来读取和写入Arduino从设备。到目前为止,我有以下代码:

C#Master:

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();
}

Slave 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。我尝试了一些东西,但它不起作用。有人可以解释如何写数据。

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

1 个答案:

答案 0 :(得分:2)

我认为问题是由于Serial.begin缺失,实际上已经收到了从主服务器发送到服务器的数据,只是没有打印。请在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进行测试,它确实有效。收到的数据可以在串口监视器中显示。

enter image description here