Page 1 of 3

Serial Communication - LattePanda Leonardo [SOLVED]

Posted: Sat Aug 26, 2017 9:21 pm
by atestani
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);
      }
    }
  }
}


Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Sat Aug 26, 2017 10:08 pm
by ChicagoBob123
On the Arduino side you need to read the serial data
then you can act on it. On the C# side you need to pick the right com port
and send the data to the board.
The baud rate is set on the Arduino side when you set up reading the serial loop.
Look for the serial example in the Arduino code it will help.
It works and once you understand that there is a serial connection between the
CPU and Arduino and its the only way they communicate.

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Sun Aug 27, 2017 3:28 am
by atestani
Thanks for replying...

Would you elaborate on what you are saying with respect to the code I posted? I think I am doing all of that but clearly am not doing something right.

Also, what example are you referring to? I have searched a lot and couldn't find an example that is applicable.

Thanks

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Sun Aug 27, 2017 10:12 am
by Lutz
Oh yes, serial connection could be very hard work... but any other connection is much worst for a programmer...
This is what I always do in such case :
1. Use a standard terminal program to try a connection to the arduino and improve your program on arduino.
2. If 1. is working, connect the terminal program with your PC and wrote program to connect in C#.
3. If 1.+2. is working you have enough learned to connect a C# program with arduino ;)

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Sun Aug 27, 2017 5:50 pm
by atestani
Thanks for the reply. I had verified the problem exists with a terminal emulator.

Note that I have all of the code working perfectly with an external Arduino which includes the Arduino code as well as my C# program. The terminal emulator also works fine with the external Arduino.

The problem I am having boils down to the inability to transmit from the LattePanda CPU to the onboard Arduino. I can receive data the Arduino is sending to the LattaPanda CPU.

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Sun Aug 27, 2017 7:11 pm
by Lutz
I found this in my own arduino.cs :

Code: Select all

        /// <summary>
        /// Opens the serial port connection, should it be required. By default the port is
        /// opened when the object is first created.
        /// </summary>
        public void Open()
        {
            _serialPort.Open();

            //MODRTS
            _serialPort.RtsEnable = true;
            _serialPort.DtrEnable = true;
            //END MODRTS

Hope it will help.

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Sun Aug 27, 2017 11:16 pm
by atestani
Thanks...

I had high hopes when I saw that you had enabled RTS but it still didn't fix my problem.

I also tried my software pn another LattePanda with the same result.

Actually, I just found that I even have the problem even (Arduino not receiving data) even using the Serial Monitor. This says the problem is in the Arduino code or hardware since it all works even with the monitor with the external Arduino.

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Mon Aug 28, 2017 3:29 am
by Lutz
I have read your first post again.
Your external Arduino is not a "Leonardo" ?
Maybe here we have the difference ?

My proposition : Start a new project with the original "firmata" software on the Leonardo. If that works your hardware is OK and the problem is the software on the Leonardo.

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Mon Aug 28, 2017 2:21 pm
by ChicagoBob123
Yes make sure its Leonardo and write it to the Arduino and please let everyone know if its OK or not.

Re: Serial Communication - LattePanda CPU and Onboard Arduin

Posted: Mon Aug 28, 2017 9:32 pm
by atestani
The code is definitely set up as Leonardo. The problem occurs even with the Serial Monitor so I know the peoblem is with the Arduino. I also have made sure DTR (and RTS) are enabled.

I have the problem on two separate LP's so I don't think it is broken hardware. However, testing with Firmata is a good idea. I did look thru the Firmata code and the only difference I see is it used a Stream.

Does anyone have a sketch that successfully receives from the Serial Monitor?