2017-02-27 10 views
1

Arduinoボードと通信するC#でプログラムを設計しようとしています。このプログラムはArduinoから整数データを受け取り、その値に関連するものを表示する必要があります。arduinoのintをPCのc#に送信する方法

私が必要とするのは、pc(ラップトップ内蔵Bluetooth)のarduinoからc#に値(int)を送るために、C#とArduino Unoのコードだけです。

私のプログラムのコードが必要な場合は私に尋ねてください。

私はC#でプログラムを実行しましたが、それが正しいかどうか教えてください。

using System; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO.Ports; 

public class travauxEncadre 
{ 
static public void Main() 
{ 

    string data = "0"; 
    int Consommer = int.Parse(data); 

    //Début Prise des valeurs manuelle 

    Console.Clear(); 


    //Définition du seuil d'avertissement 

    Console.WriteLine("Seuil d'avertissement"); 
    string SeuilAvertissement = Console.ReadLine(); 
    Console.Clear(); 


    // Définition du seuil d'exces 

    Console.WriteLine("Seuil d'exces"); 
    string SeuilExces = Console.ReadLine(); 
    Console.Clear(); 


    //Défintion de la conso actuelle (a enlever) 

    // Console.WriteLine("Consommation"); 
    // string Conso = Console.ReadLine(); 
    // Console.Clear(); 



    int Avertissement = int.Parse(SeuilAvertissement); 
    int Exces = int.Parse(SeuilExces); 
    // int Consommer = int.Parse(Conso); 

    //Fin Prise des valeurs manuelle 



    //Début Bluetooth 

    SerialPort port; 

    port = new SerialPort(); 

    port.BaudRate = 9600; 
    port.DataBits = 8; 
    port.StopBits = StopBits.One; 
    port.Parity = Parity.None; 

    port.PortName = "COM4"; 

    port.DataReceived += Port_DataReceived; 

    port.Open(); 


    //Fin Bluetooth 



    //Début Vérification 

    if (Avertissement >= Exces) 
    { 
     Console.WriteLine("Impossible"); 
     System.Threading.Thread.Sleep(1000); 

    } 

    else 
    { 

     if (Consommer < Avertissement) 
     { 
      Console.WriteLine("Vert"); 
      Console.WriteLine(data + " Kw/H"); 
      System.Threading.Thread.Sleep(1000); 

     } 
     else 
     { 
      if (Consommer >= Exces) 
      { 
       Console.WriteLine("Rouge"); 
       Console.WriteLine(data + "Kw/H"); 
       System.Threading.Thread.Sleep(1000); 

      } 
      else 
      { 
       Console.WriteLine("Jaune"); 
       Console.WriteLine(data + "Kw/H"); 
       System.Threading.Thread.Sleep(1000); 

      } 

      // Fin Vérification 


     } 
    } 



} 
private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 


    SerialPort port; 
    string data = string.Empty; 

    port = (SerialPort)sender; 

    data = port.ReadExisting(); 

    int Consommer = int.Parse(data); 

} 
} 
+0

常に作業しているコードスニペットを投稿してください。 http://stackoverflow.com/help/how-to-ask – matt

+0

フランス語で申し訳ありません – Mazeo

+0

私にはすばらしく、特定のデバイス名などを検索する必要はありませんCOM4。ええ、kW時間を超えると赤色になり、そうでなければ黄色になります(私のフランス語はそれほどうれしくありません)。あなたは本当にクールなプロジェクトを持っているように思えます。 – Snoopy

答えて

1

データを送信するために多くの方法があるので、私はあなたにもArduinoの側で行うとしたいのか全くわからないので、私はあなたにC#のシリアルポート側を説明します。 Arduinoと通信する最も簡単な方法は、RS-232ポート(おそらくシリアルポートのBluetoothアダプタドングル)を使用することです。

まず、シリアルポートを開いて、Arduinoボードが組み込むべきRS-232経由のArduinoと通信したいと思います。私は先に進んで、

/// <summary> 
/// Read data from Arduino until user presses key. 
/// </summary> 
/// <param name="args">Arguments to the program (we do not take any).</param> 
static void Main(string[] args) 
{ 
    SerialPort port; 

    // first, create a new serial-port 
    port = new SerialPort(); 

    // configure the settings to match the Arduino board 
    // below i've just used some of the most common settings 
    // to get the point across 
    port.BaudRate = 9600; 
    port.DataBits = 8; 
    port.StopBits = StopBits.One; 
    port.Parity = Parity.None; 

    // you'll have to figure out what your actual COM name is 
    // for this example I'll just use COM 11 
    port.PortName = "COM11"; 

    // subscribe to when the data is coming from the port 
    port.DataReceived += Port_DataReceived; 

    // open up communications with the port 
    port.Open(); 

    // continue to receive data until user presses key 
    Console.ReadKey(); 

    // close access to the port when finished 
    port.Close(); 
} 

...以下のシリアルポートは、あなたがする必要があります他の事は、加入者(実際にデータを印刷する方法)を作成することです。私はあなたのためにそれを下にしました...

/// <summary> 
/// Methods for handling the incoming data from Arduino. 
/// </summary> 
/// <param name="sender">The port that's getting data from Arduino.</param> 
/// <param name="e">When the new data comes in.</param> 
private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    SerialPort port; 
    string data = string.Empty; 

    // get a reference to the port that's sending the data 
    port = (SerialPort)sender; 

    // read the data from the port 
    data = port.ReadExisting(); 

    // print Arduino data to the screen 
    Console.WriteLine(data); 
} 
+0

スヌーピーありがとう!私はよく分かっていれば、私は自分のプログラムにそのコードを入れるべきですが、私はbluetoothモジュール(名前を覚えていない)とラップトップを内蔵したブルートゥース(hp elitebook laptop)でarduino unoを使います。だから私はあなたが与えたコードで変更する必要がありますか? – Mazeo

+0

@Mazeoシリアルポートを使用するルートに行く場合は、それぞれを照会する必要があります。だから、各シリアルポートデバイスから文字列を受け取る 'for'ループを書く必要があります。デバイスが「私はArduinoです」と応答すると、正しいCOMポートにいることがわかります。 – Snoopy

+0

@Mazeo Arduinoにある種のコマンド/レスポンスメカニズムがあることを確認してください...あなたがArduinoに送信するとき、「こんにちは、あなたは誰ですか? ...それは... "こんにちは、私はアルドゥイノです!"と応答します... ...そうすれば、どのポートに接続されているのか知ることができます。 – Snoopy

関連する問題