如何在PC上将一个int从arduino发送到c#

How to send an int from arduino to c# on pc

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

我唯一需要的是 C# 和 Arduino Uno 中的代码,以便将值 (int) 从 arduino 发送到 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);

}
}

由于我不完全确定你想在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);
}