Hallo Leute, ich brauche deine Hilfe bei meinem Projekt. Ich habe eine 2 Textfelder (Typ String) in einem C# -Programm und ich muss diese Zahlen an Arduino mit einem Serial.Port senden. Bis jetzt habe ich einen der Werte zu arduino geschickt, aber es funktioniert nicht sehr gut, wenn ich "1200" arduino Lese und zeige: 1,2,0,0 ich brauche "1200". Wie sende ich 2 Werte von C# zu Arduino? Wie das Arduino diese Werte (x und y) lesen wird?Von C# zu Arduino mit Serial.Port
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports; // necessário para ter acesso as portas
namespace interfaceArduinoVS2013
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
timerCOM.Enabled = true;
}
private void atualizaListaCOMs()
{
int i;
bool quantDiferente; //If there are more ports
i = 0;
quantDiferente = false;
//if there are new ports
if (comboBox1.Items.Count == SerialPort.GetPortNames().Length)
{
foreach (string s in SerialPort.GetPortNames())
{
if (comboBox1.Items[i++].Equals(s) == false)
{
quantDiferente = true;
}
}
}
else
{
quantDiferente = true;
}
//it was't detected difference
if (quantDiferente == false)
{
return;
}
//clean comboBox
comboBox1.Items.Clear();
//add all the COMs in the list
foreach (string s in SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
//select the first position
comboBox1.SelectedIndex = 0;
}
private void timerCOM_Tick(object sender, EventArgs e)
{
atualizaListaCOMs();
}
private void btConectar_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
{
try
{
serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
serialPort1.Open();
}
catch
{
return;
}
if (serialPort1.IsOpen)
{
btConectar.Text = "Desconectar";
comboBox1.Enabled = false;
}
}
else
{
try
{
serialPort1.Close();
comboBox1.Enabled = true;
btConectar.Text = "Conectar";
}
catch
{
return;
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if(serialPort1.IsOpen == true) // if the port is open
serialPort1.Close(); //close
}
private void btEnviar_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen == true) //porta está aberta
serialPort1.Write(textBoxX.Text); //send the text from textboxX
serialPort1.Write(textBoxY.Text);
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting(); //read data from serial
this.Invoke(new EventHandler(trataDadoRecebido));
}
private void trataDadoRecebido(object sender, EventArgs e)
{
textBoxReceber.AppendText(RxString);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Arduino Script
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
char c = Serial.read();
Serial.println(c);
}
}