1

I'm trying to send a string from my arduino(leonardo) to a C# program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace ConsoleApplication2
{
class Program
{

    static void Main(string[] args)
    {
        SerialPort mySerialPort = new SerialPort("COM7");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;


        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}
}

This is my code wich I copied from the msdn example to try and understand what it does. My arduino code below just sends hello world over te com port with a delay of 1000.

void setup ()
{

 Serial.begin(9600);

 }


void loop(){

 Serial.println("Hello World");
 delay(1000);
}

My arduino is using the COM7 like I defined in the C# program. When I run bot programs, The C# program never comes within the datareceived event handler. So no data is received. I really want tis to work :)

Kind regards

0

2 Answers 2

2

I Switched the code to a windows form application, it still was not working. Then i found a topic about serial communication with C# about arduino leonardo here

I had to do this:

        serial.DtrEnable = true;
        serial.RtsEnable = true;

I consider my problem as solved.

Sign up to request clarification or add additional context in comments.

2 Comments

meta.stackexchange.com/a/5235/222049 Now, you can't accept your own answer until two days have passed, but you should take the time to come back and do so. This flags the question as resolved and complete.
Wow, thank you. It was working on my Uno, but then changed to Leonardo and stopped working. This seems to be the ticket 🙂
0

Console applications do not have a message loop so naturally they don't respond to events. You have one thread and it will be stuck blocking at Console.ReadKey(). Either use synchronous reads from the serial port or, if you wish to stick to an event-based model, move this code to a windows-based applications.

For a synchronous example, see this MSDN example :

while (_continue)
{
    try
    {
        string message = _serialPort.ReadLine();
        Console.WriteLine(message);
    }
    catch (TimeoutException) { }
}

The above is only an excerpt - the full example demonstrates setting up the timeout values, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.