Serial Read Arduino Leonardo from C#

userHead anonymous 2022-06-10 06:06:57 735 Views2 Replies
Hi everyone,

For a project, I'm trying to "echo" data from the COM port of the arduino Leonardo.
Unfortunately I can't read the data I send. The problem comes from the C# program.


Here is my C# code :

Code: Select all

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; namespace CP_Send_and_Arduino_send_back { public partial class Form1 : Form { string serialDataIn; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { btnOpen.Enabled = true; btnClose.Enabled = false; btnSend.Enabled = false; lblStatus.Text = "Déconnecté"; lblStatus.ForeColor = Color.Red; cbBaudRate.Text = "9600"; string[] portLists = SerialPort.GetPortNames(); cbComPort.Items.AddRange(portLists); } private void btnOpen_Click(object sender, EventArgs e) { try { serialPort1.PortName = cbComPort.Text; serialPort1.BaudRate = Convert.ToInt32(cbBaudRate.Text); serialPort1.Open(); btnOpen.Enabled = false; btnClose.Enabled = true; btnSend.Enabled = true; lblStatus.Text = "Connecté"; lblStatus.ForeColor = Color.Green; } catch(Exception error) { MessageBox.Show(error.Message); } } private void btnClose_Click(object sender, EventArgs e) { if(serialPort1.IsOpen) { try { serialPort1.Close(); btnOpen.Enabled = true; btnClose.Enabled = false; btnSend.Enabled = false; lblStatus.Text = "Déconnecté"; lblStatus.ForeColor = Color.Red; } catch (Exception error) { MessageBox.Show(error.Message); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { serialPort1.Close(); } catch (Exception error) { MessageBox.Show(error.Message); } } private void btnSend_Click(object sender, EventArgs e) { try { serialPort1.Write(tbSend.Text + "#"); } catch (Exception error) { MessageBox.Show(error.Message); } } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { serialDataIn = serialPort1.ReadExisting(); this.Invoke(new EventHandler(ShowData)); } private void ShowData(object sender, EventArgs e) { rtbReceive.Text += serialDataIn; } private void rtbReceive_TextChanged(object sender, EventArgs e) { rtbReceive.SelectionStart = rtbReceive.Text.Length; rtbReceive.ScrollToCaret(); } } } And here is my arduino code :

Code: Select all

const byte Led = 13; char data; String appendSerialData; #define LedToggle digitalWrite(Led, !digitalRead(Led)) void setup() { Serial.begin(9600); } void loop() { while(Serial.available()>0) { data = Serial.read(); appendSerialData += data; LedToggle; } if(data == '#') { Serial.print("Arduino Echo : "); Serial.println(appendSerialData); appendSerialData=""; data=0; LedToggle; } } I have an Arduino Onu available on which I test my codes from a PC other than the LattePanda card and everything works perfectly.
I receive the characters that I send in Echo.

What would my mistakes be?
If you have any advice on serial communications, thank you.
Feel free to ask me for details if I'm not clear.

Thank you in advance for your answer and sorry for my English.

HEPIA_Matt.