2016-05-25 14 views
0

これは、I2CとC#/ Windows IoTの両方を使用することにとても慣れています。私はラズベリーパイ3のマスターとArduinoの奴隷を持っています。私は私のPWMデューティサイクルを調整するために使用するArduinoにI2Cを介してUIフォームのスライダから値を送信しようとしています。 Pi、Arduino、またはその両方の場合、私が抱えている問題のいくつかが解決できません。ここでラズベリーPiからArduinoへのバイトをPWM用にI2CとIoTで送信する

は私のArduinoのスレーブコードです:

#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; 
    } 

} 

そして、私のパイマスター:

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であるということですsendposの値がPiにあります(はい、スライダを動かすと変化します)。

私が抱えている2番目の問題は、スライダが動かされた最初のことです。私はArduinoシリアルに出力しますが、その後は何も出力しません。私がArduinoをリセットまたはリロードすると、最初のスライダの出力が再び変化し、その後は何も変化しません。

これがあまりにもあいまいであるか、または説明が不適切であるとお詫び申し上げます。正しい方向への助けや助けがあれば幸いです。

ありがとうございます。

答えて

0

「Wire.onReceive(I2CReceived);」を変更する必要があります。ループには、セットアップ時にarduinoが1つしか出さないので、私の英語は申し訳ありません。

+0

おかげでストリップダウンされます|1βMDPV 'がArduinoの最初の開始時にシリアルに出力され、Arduinoがクラッシュ/フリーズする前の最初のスライダの動きの出力しか得られません。 – Ryland

0

私はNick Gammon Web Siteに基づいてArduino UNOのI2Cスレーブを書いています。 それは働いたが、私は10Kバイト/秒以上を得ることができなかった。あなた自身のコードに欠けている部分があります。ここで

はArduinoのコード

のバージョンI「のVMDPV_1をゲットループにそのメッド、私は移動する場合は、 'Wire.onReceive(I2CReceived)' または 'I2CReceived(パス)' の
#include <Wire.h> 

#define I2C_400K 1 // http://www.gammon.com.au/forum/?id=10896 

bool receiveEventcommandReceived = false; 
bool requestEventCommandReceived = false; 

int _currentRequestInputParameterLen = 0; 

void receiveEvent(int howMany) { 

    receiveEventcommandReceived = true; 
    while (Wire.available() > 0) 
    { 
     _cmd = Wire.read(); 

     if (_cmd == ArduinoCommand_EpromWrite) { 
      // Some code 
     } 
     else if (_cmd == ArduinoCommand_EpromRead) { 

      _addr0 = Wire.read(); 
      _addr1 = Wire.read(); 
      _addr = (_addr0 * 256) + _addr1; 
      _len = Wire.read(); 
      _EEPROMBuffer = NusbioEx.EEPROMRead(_addr, _len); 
      _r  = 128+1; 
     } 
     else { 
      // Some code 
     } 
     _count++; 
    } 
} 

void requestEvent() 
{ 
    requestEventCommandReceived = true; 

    if (_cmd == ArduinoCommand_EpromRead) { 

     Wire.write(_EEPROMBuffer, strlen(_EEPROMBuffer)); 
    } 
    else { // ArduinoCommand_EpromWrite or any other api 

     int v1 = _r >> 8; 
     int v2 = _r & 0xFF; 

     char buffer[2]; 
     buffer[0] = v1; 
     buffer[1] = v2; 
     Wire.write(buffer, 2); // MUST BE SENT IN ONE BUFFER -> TO CREATE ONE I2C TRANSACTION 
    } 
} 

void SetupI2C() { 

    Wire.begin(I2C_SLAVE_ADDR);    // join i2c bus with address #4 

    #if I2C_400K 
     TWBR = 12; // http://www.gammon.com.au/forum/?id=10896 
    #endif 

    Wire.onReceive(receiveEvent); // register event 
    Wire.onRequest(requestEvent); // register event 
} 

void setup() { 
    SetupI2C(); 
} 

void loop() { 

    if (requestEventCommandReceived) 
    { 
     requestEventCommandReceived = false; 
    } 
    #endif 
} 
関連する問題