私は Raspberry と Arduino の世界ではかなり新しいものです (特に Windows IoT と一緒に)。私の計画は、Arduino でさまざまなセンサー (温度と圧力) を読み取り、その値を RP2 に送信して、GUI で使用できるようにすることです。
したがって、センサーを読み取ることは問題ではありません。正しい値を受け取ります。その後、I²C バスに送信します (Wire.h ライブラリの要件に基づく)。
私の RP2 では、2 つの同様のプロジェクトが見つかりました。C# のコードはそれらに基づいています。
ここまでは順調ですが、両方のデバイスを起動しても RP にデータがありません。私のRPはArduinoを見つけます。
問題を特定するために、ランダムな値を RP に送信するために Wire.h サンプル スケッチを使用しています。しかし、まだ同じ問題があるので、私の C# コードに問題があると思います。また、RP が値を配列に書き込む場所にブレークポイントを設定します。しかし、入ってくる値がないように見えます。
両方のコードを添付しました。誰かが私を助けてくれることを願っています。私はかなりひどい立ち往生しています...
どうもありがとうございました!ティエモ
Arduino コード:
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
byte val = 0;
byte val_2 = 100;
void setup()
{
Wire.begin(SLAVE_ADDRESS); // join i2c bus
Serial.begin(9600);
}
void loop()
{
Wire.beginTransmission(SLAVE_ADDRESS); // transmit to master device
// device address is specified in datasheet
Wire.write(val); // sends value byte
Wire.write (val_2);
// Serial.write(val);
// Serial.write(val_2);
Wire.endTransmission(); // stop transmitting
val++; // increment value
val_2++;
if(val == 64) // if reached 64th position (max)
{
val = 0; // start over from lowest value
}
if (val_2 == 164)
{
val = 100;
}
delay(500);
}
C# コード:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// i2c Libs
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
using System.Diagnostics;
using System.Threading;
// Die Vorlage "Leere Seite" ist unter http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 dokumentiert.
namespace _007_Test7_I2C
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet werden kann oder auf die innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
private I2cDevice Device;
private Timer periodicTimer;
public MainPage()
{
this.InitializeComponent();
initcomunica();
}
private async void initcomunica()
{
var settings = new I2cConnectionSettings(0x40); // Arduino address
settings.BusSpeed = I2cBusSpeed.StandardMode;
string aqs = I2cDevice.GetDeviceSelector("I2C1");
var dis = await DeviceInformation.FindAllAsync(aqs);
Device = await I2cDevice.FromIdAsync(dis[0].Id, settings);
periodicTimer = new Timer(this.TimerCallback, null, 0, 1000); // Create a timer
}
private void TimerCallback(object state)
{
byte[] RegAddrBuf = new byte[] { 0x40 };
byte[] ReadBuf = new byte[7];
try
{
Device.Read(ReadBuf);
// Debug.WriteLine(ReadBuf);
}
catch (Exception f)
{
Debug.WriteLine(f.Message);
}
char[] cArray = System.Text.Encoding.UTF8.GetString(ReadBuf, 0, 7).ToCharArray(); // Converte Byte to Char
String c = new String(cArray);
Debug.WriteLine(c);
// refresh the screen, note Im using a textbock @ UI
var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{ Temp_2.Text = c; });
}
}
}