【发布时间】:2016-05-25 09:33:01
【问题描述】:
我对同时使用 I2C 和 C#/Windows IoT 非常陌生,所以如果这是一个愚蠢的问题,请提前道歉。我有一个 Raspberry Pi 3 主机和 Arduino 从机。我正在尝试通过 I2C 从我的 UI 表单上的滑块向 Arduino 发送一个值,我将使用它来调整我的 PWM 占空比。我遇到了几个问题,如果是 Pi、Arduino 或两者兼而有之,我无法解决。
这是我的 Arduino Slave 代码:
#include <Wire.h>
#define MyAddress 0x03
byte ReceivedData;
int pass;
void setup() {
Wire.begin(MyAddress);
Wire.onReceive(I2CReceived);
Serial.begin(9600);
//Wire.onRequest(I2CRequest);
}
void loop() {
delay(100);
}
void I2CReceived(int NumberOfBytes)
{
/* WinIoT have sent data byte; read it */
byte ReceivedData = Wire.read();
Serial.println(ReceivedData);
if (ReceivedData <= 127){
Serial.println("Equal or under");
return;
}else{
Serial.println("over");
return;
}
}
还有我的 Pi 大师:
using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Core;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
using System.Diagnostics;
using System.Threading;
namespace I2COutput
{
public sealed partial class MainPage : Page
{
private I2cDevice TransPump;
private Timer periodicTimer;
private const byte pump = 0x03;
double pos;
public MainPage()
{
InitializeComponent();
initcomunica();
}
private async void initcomunica()
{
var pumpset = new I2cConnectionSettings(pump);
pumpset.BusSpeed = I2cBusSpeed.StandardMode;
string aqs = I2cDevice.GetDeviceSelector("I2C1");
var dis = await DeviceInformation.FindAllAsync(aqs);
TransPump = await I2cDevice.FromIdAsync(dis[0].Id, pumpset);
}
private async void SendChange()
{
byte[] sendpos;
try
{
sendpos = BitConverter.GetBytes(pos);
TransPump.Write(sendpos);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
private void tempLbl_SelectionChanged(object sender, RoutedEventArgs e)
{
}
private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
pos = slider.Value;
temp2Lbl.Text = pos.ToString();
Convert.ToInt16(pos);
SendChange();
return;
}
}
}
我遇到的第一个问题是我在 Arduino 上的 ReceivedData 始终为 0,不管 Pi 上的 sendpos 的值是多少(是的,当我移动滑块时它确实会改变)。
我遇到的第二个问题是第一次移动滑块时,我在 Arduino 串行上得到了输出,但之后就没有了。如果我重置或重新加载 Arduino,我会再次获得初始滑块更改的输出,之后什么都没有。
如果其中任何内容过于含糊或解释不佳,我们深表歉意,我们将非常感谢您在正确方向上的任何帮助或推动。
提前致谢。
【问题讨论】:
标签: c# arduino raspberry-pi iot i2c