我试图设计一个程序在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);
}
}
总是发布你正在使用的代码片段。 http://stackoverflow.com/help/how-to-ask – matt
对不起,代码中的法语 – Mazeo
这对我来说看起来很不错,而且它不像你需要搜索特定的设备名称或任何东西,因为你知道它是在COM4。是的,所以如果它超过千瓦小时就会变红,否则黄色(我的法语不是那么好)。听起来像你有一个非常酷的项目。 – Snoopy