Problem communicating from C# app to Arduino Mega

Hi.

I am trying to resolve a serial communication issue. I’ve found that other people have had this same problem and I’ve tried many different solutions with luck. The board is a Mega, and I’m trying to build an app in C#. I’ve discovered that in C# the Handshaking property has to be “none” and DtsEnabled and RtsEnabled properties have to be set to true. But, this didn’t solve the issue. I know the Arduino sketch works because the Arduino serial monitor sends data with not problem. My suspicion is that C# is sending too many characters. I have tried 9600 and 115200 baudrates, and checked and rechecked all of the port properties. I know the C# app does connect to Arduino because the Arduino LED flashes when it does. But, then it times out (I think).

Any ideas or insight is greatly appreciated.

C# code:

private void INIT_button_Click(object sender, EventArgs e)
{
//code_window.Items.Clear(); // code_window is a listbox
serialPort.Handshake = Handshake.None;
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
serialPort.Open();
Task.Delay(1000);
serialPort.Write(“100”);
}

Arduino sketch:

#include <TimeInterrupt.h>
#include <SoftwareSerial.h>

volatile char current_input_char;
bool initialized = false;
String incoming_string;

void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for USB port to connect.
}
}

void loop()
{
initialize();
if (initialized)
{
blinker();
}
}

void initialize()
{
if (Serial.available() > 0)
{
incoming_string = Serial.readString();
}
if (incoming_string.toInt() == 100)
{
initialized = true;
//Serial.write(“1. CONNECTED\n”); // I tried this with and without
}
delay(100);
}

void blinker()
{
digitalWrite(13, HIGH);
delay(1500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(500);
}

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

PS … when posting code please use the code button </> to make it easier for people to help you.

Last summer I wrote several VB.NET programs to communicate with the Arduino Mega.
I know for a fact DTR and RTS were not used and not necessary.

Edit:
Opening the serial port with every button click is horrible.

.

Some remarks about your c# code:
Declare the port and portName and open the port at the beginning of your program
Keep it open until you close the program.

This is an example:

using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace Serial_example
{
    public partial class Form1 : Form
    {
        //Replace "COMYourport" with the port in use (COM1 or COMwhatever).
        //new SerialPort(string portName, int Badrate, Parity parity, int dataBits, StopBits stopBits)

        private SerialPort SerialPort = new SerialPort("COMYourport", 9600, Parity.None, 8, StopBits.One);

        public Form1()
        {
            InitializeComponent();

            // Always use try catch to prevent crashing if port doesn't exist and
            // check if port is already open.

            try
            {
                if (SerialPort.IsOpen)
                {
                    SerialPort.Close();
                }
                SerialPort.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void INIT_button_Click(object sender, EventArgs e)
        {
            SerialPort.Write("100");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SerialPort.Close();
        }
    }
}

Thanks for all of the input.

I had already read through the serial basic. But, I read through it again…

I am not sure why, but I believe that opening the port when the form loaded is what made the difference.

It works with and without Dts and Rts enabled.

The code button </>. Got it!

Once again, thank you.

peternic:
I am not sure why, but I believe that opening the port when the form loaded is what made the difference.

Normally the Arduino resets when the PC program opens the serial port and the PC program must allow a few seconds for the reset to complete before sending data. This also means that the PC program should open the serial port and keep it open until it is completely finished with the Arduino.

...R

peternic:
I am not sure why, but I believe that opening the port when the form loaded is what made the difference.

What if you have a small configuration interface to set baudrate etc? Using form events are a bad choice from that perspective as it's not flexible.

peternic:
It works with and without Dts and Rts enabled.

It's the DtrEnable property that defines if the Arduino resets or not.

Robin2:
Normally the Arduino resets when the PC program opens the serial port and the PC program must allow a few seconds for the reset to complete before sending data. This also means that the PC program should open the serial port and keep it open until it is completely finished with the Arduino.
…R

I’m using the software Br@ys Terminal as serial monitor.
Bray’s terminal is configurable to even automatically open/close the serial connection through winning/loosing focus (=beeing the window in the foreground/backround) without resetting the Arduino-Board.
This is very convenieant if you use another editor for the code as f.e. notepad++

On the internet you will find version 2014. unfortunately in this version autoscroll does not work
so I attach version 2012 where autoscroll works

you can also log the received content it has macros and even a scripting language for simulating the partner-device
best regards Stefan

Brays-Terminal-2012.zip (321 KB)