Serial Communication - LattePanda Leonardo [SOLVED]

userHead atestani 2017-08-27 05:21:29 25335 Views28 Replies
I have spent hours trying to get communication from the LattePanda CPU (LP-CPU) and onboard Arduino to work with a test program prior to writing a more complex application. A C# test program sends a string to the Arduino which is to echo the string and the C# program is to display it. I am using the USB CDC port on the Arduino (i.e. Serial, not Serial1). I have not tried physically connecting the LP-CPU UART pins to the Arduino UART pins and use Serial1.

The Arduino sketch can be loaded on an external Arduino board (Mega 2560) and the C# program on the LP-CPU, and it works fine. My problem is communicating with the onboard Arduino (Leonardo). More specifically, have the string received by the Arduino, i.e. if I put code in the Arduino to continually send the string to the LP-CPU, it is received and displayed by the C# program.

I can zip up the entire project if it is needed but I am listing the code below in hopes that someone will spot what I am doing wrong!

Arduino sketch:

Code: Select all

//******* MACROS ******* #define set(xpin) (digitalWrite(xpin, HIGH)) #define clr(xpin) (digitalWrite(xpin, LOW)) //******* GLOBAL VARIABLES ******* String inputString; boolean stringComplete = false; //******* PIN DEFINITIONS ******* int LED = 13; //----------------------------------------------- void flashLED(int t) //----------------------------------------------- { set(LED); delay(t); clr(LED); } //----------------------------------------------- void serialEvent() //----------------------------------------------- { while (Serial.available()) { char inChar = (char)Serial.read(); if (inChar == '\n') { stringComplete = true; return; } inputString += inChar; } } //----------------------------------------------- void setup() //----------------------------------------------- { int i; pinMode(LED, OUTPUT); Serial.begin(115200); while (!Serial) { delay(1); if (i++ > 1500) break; } inputString.reserve(64); inputString = ""; for (i = 0; i < 5; i++) { set(LED); delay(150); clr(LED); delay(150); } } //----------------------------------------------- void loop() //----------------------------------------------- { if (stringComplete) { Serial.print("Echo: |" + inputString + "|\n"); inputString = ""; stringComplete = false; flashLED(10); } } The C# form contains the following controls:
  • Available serial port combo box
    COM port open button
    Single line Textbox for the string to send to the Arduino
    Send button
    Multiline Textbox to display the echoed strings
C# form1.cs file:

Code: Select all

using System; using System.IO.Ports; using System.Windows.Forms; namespace LPSerialTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); string[] ports = SerialPort.GetPortNames(); PortCB.Items.AddRange(ports); } //------------------------------------------------------- private bool SendMsg(string str) //------------------------------------------------------- { if (COMPort.IsOpen) { COMPort.WriteLine(str); return (true); } else { MessageBox.Show("ERROR: COM Port is not open"); return (false); } } //------------------------------------------------------- private delegate void processValueDelegate(string value); private void COMPort_DataReceived_1(object sender, SerialDataReceivedEventArgs e) //------------------------------------------------------- { SerialPort sp = (SerialPort)sender; string value = sp.ReadLine(); processValue(value); } //------------------------------------------------------- private void processValue(string value) //------------------------------------------------------- { if (this.InvokeRequired) { this.BeginInvoke(new processValueDelegate(processValue), new object[] { value }); } else recBox.Text += value +"\r\n"; } //------------------------------------------------------- private void sendBtn_Click(object sender, EventArgs e) //------------------------------------------------------- { SendMsg(sendBox.Text); } //------------------------------------------------------- private void OpenBtn_Click(object sender, EventArgs e) //------------------------------------------------------- { try { if (COMPort.IsOpen) { COMPort.Close(); } COMPort.PortName = PortCB.Text; COMPort.Open(); if (COMPort.IsOpen) { COMPort.DtrEnable = true; OpenBtn.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message, "There is a problem opening the COM Port", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }