如何将arduino中的int发送到pc上的c#

时间:2017-02-27 15:19:43

标签: c# bluetooth int arduino-uno

我正在尝试用C#设计一个与Arduino板通信的程序。该程序应该从Arduino接收整数数据并显示与该值相关的内容。

我唯一需要的是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);

}
}

1 个答案:

答案 0 :(得分:1)

由于我不完全确定你想在Arduino方面做什么,也因为有很多方法可以发送数据,我将向你解释C#串口端。与Arduino通信的最简单方法可能是通过RS-232端口(或者在你的情况下可能是串口蓝牙适配器加密狗)。

首先,您需要打开一个串口,通过RS-232与您的Arduino进行通信,这是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);
}