簡體   English   中英

使用I2C Windows Iot和Arduino寫入數據

[英]Write data using I2C windows iot and Arduino

因此,我有一個連接Windows IoT的Arduino和Raspberry pi 3,如下圖所示:[ 在此處輸入圖片說明 ] [1

我想使用RPI作為I2C的主設備讀取和寫入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 ,實際上已經收到了從主設備發送到從設備的數據,只是沒有打印出來。請在從設備上使用以下代碼重試。

#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進行了測試,它可以正常工作。接收到的數據可以在串行監視器中顯示。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM