2017-02-27 30 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