Serial Communication - LattePanda Leonardo [SOLVED]

A place to talk about all the Operating Systems, Software & Hardware
Forum rules

- Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.

- Members should post in a way which is consistent with "normal writing". That is users should not post excessive numbers of emoticons, large, small or coloured text, etc. Similarly users should not SHOUT or use excessive punctuation (e.g. ! and ?) in topic titles or posts.

- Members should use an appropriate, descriptive subject when posting a new topic. Examples of bad subjects include; "Help me!", "I'm stuck!", "I've got an error!", etc. Examples of good subjects include; "Getting an TABLE FULL error upon login", "Cannot restore a backup".

- Members are asked to post in English only so that posts can be well received and properly monitored.

- Spam is not tolerated here under any circumstance. This includes offering hosting services (charged and free), installation services, solicitation etc. Recruiting members for your own projects (moderators, designers, professional MOD work, etc.) is not permitted and is also considered spam. Users posting spam will be warned and their post removed.

- Members should post in a way that is respectful of other users. Flaming or abusing users in any way will not be tolerated and will lead to a warning. Vulgar language and inappropriate material is not allowed and will be removed. We try to maintain a family friendly atmosphere whenever it is possible within the main subject matter of this community. Please keep this in mind when participating.

- Each user is allowed to create one account. If you would like to change your username, please contact us and, most likely, we can do it for you while you can keep all of your profile data, posts, and other content.

- If a user violates our guidelines and shows a disregard for them, our staff and our community, they run the risk of losing their account. We reserve the right to deactivate any account and to edit or remove any content without warning. These guidelines are subject to change at any time without notice.

Thank you for visiting LattePanda.com!
User avatar
atestani
Posts: 19
Joined: Mon Aug 14, 2017 7:27 pm

Sat Aug 26, 2017 9:21 pm

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);
      }
    }
  }
}

Last edited by atestani on Thu Sep 07, 2017 9:27 pm, edited 1 time in total.

User avatar
ChicagoBob123
Posts: 37
Joined: Fri May 19, 2017 9:56 pm

Sat Aug 26, 2017 10:08 pm

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.

User avatar
atestani
Posts: 19
Joined: Mon Aug 14, 2017 7:27 pm

Sun Aug 27, 2017 3:28 am

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

User avatar
Lutz
Posts: 214
Joined: Mon Apr 18, 2016 4:30 am
Location: Germany, NRW

Sun Aug 27, 2017 10:12 am

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 ;)

User avatar
atestani
Posts: 19
Joined: Mon Aug 14, 2017 7:27 pm

Sun Aug 27, 2017 5:50 pm

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.

User avatar
Lutz
Posts: 214
Joined: Mon Apr 18, 2016 4:30 am
Location: Germany, NRW

Sun Aug 27, 2017 7:11 pm

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.

User avatar
atestani
Posts: 19
Joined: Mon Aug 14, 2017 7:27 pm

Sun Aug 27, 2017 11:16 pm

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.

User avatar
Lutz
Posts: 214
Joined: Mon Apr 18, 2016 4:30 am
Location: Germany, NRW

Mon Aug 28, 2017 3:29 am

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.

User avatar
ChicagoBob123
Posts: 37
Joined: Fri May 19, 2017 9:56 pm

Mon Aug 28, 2017 2:21 pm

Yes make sure its Leonardo and write it to the Arduino and please let everyone know if its OK or not.

User avatar
atestani
Posts: 19
Joined: Mon Aug 14, 2017 7:27 pm

Mon Aug 28, 2017 9:32 pm

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?

Post Reply