2017-02-27 32 views
1

我試圖設計一個程序在C#中,與Arduino板進行通信。這個程序應該接收來自Arduino的整數數據並顯示與該值相關的內容。如何發送int從arduino到c#在PC上

我唯一需要的是C#和Arduino Uno中的代碼,以便從arduino發送一個值(int)到pc(筆記本電腦集成藍牙)上的c#。

問我是否需要我的程序代碼。

我已經完成了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。是的,所以如果它超過千瓦小時就會變紅,否則黃色(我的法語不是那麼好)。聽起來像你有一個非常酷的項目。 – Snoopy

回答

1

因爲我不完全相信你想要的東西對Arduino的側做,也是因爲有許多方法來發送數據,我將解釋C#串口端給你。與Arduino進行通信的最簡單方法可能是通過RS-232端口(或者在您的情況下串行端口藍牙適配器加密狗)。

首先,你要打開一個串口與你的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

謝謝史努比!如果我理解的很好,我應該將該代碼放入我的程序中,但是,我使用藍牙模塊(不記得名稱)和筆記本電腦集成藍牙(hp elitebook筆記本電腦)的arduino uno。所以我應該改變你給的代碼嗎? – Mazeo

+0

@Mazeo如果你使用串行端口的路線,你必須查詢每一個。所以你需要編寫一個'for'循環來從每個串口設備獲取一個字符串。當設備響應「我是Arduino」時,您會知道您處於正確的COM端口。 – Snoopy

+0

@Mazeo確保你的Arduino具有某種指令/響應機制......當你傳送給Arduino時:「你好,你是誰?」 ......它會迴應......「你好,我是Arduino!」......這樣你就會知道它連接的是哪個端口。 – Snoopy