C# serial communication

I apology for my bad English (I don’t speak it fluently).

Hi !

I’m new in the Arduino universe and I would like to communicate to my Arduino Mega 2560 with a C# program.
I already achieved to transmit data from my C# program to my Arduino but I can’t do the reverse.

Here is my Arduino program (it sends “test” when I push a button) :

unsigned long buttonCooldown;
int button = 50;

void setup (){

  Serial.begin (9600);
  pinMode (button, INPUT);
}

void loop (){

  operateInputs ();
  delay (10);
}

void operateInputs (){

  if (digitalRead (button) == 1){

    if (millis()-buttonCooldown > 100) Serial.print ("test");
    buttonCooldown = millis();
  }
}

It works on the Arduino console but it doesn’t on my C# program or in another C# program made by PaulS (I download it from this topic).

My C# program :

using System;
using OpenNETCF.IO.Ports;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            port.Open();
            System.Threading.Thread.Sleep(1000); // waiting for Arduino reset
            
            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine (port.BytesToRead); // it write on the console only zeros, so no data is received
                System.Threading.Thread.Sleep(100);
            }

            port.Close();
        }
    }
}

Please help me ! It’s the third day that I work on this problem ! :sob:
Thanks. :smiley:

(deleted)

Yes.
I did a lot of research and I already post in a C# forum but my problem is still there.

(deleted)

I don't use Windows and I don't know C# but I see port.close() in your code. That suggests to me your program is continually opening and closing the Serial port. Each time the Arduino will reset. Your PC program should open the Serial port and keep it open until it is completely finished with the Arduino.

In my programs I get the Arduino to send a short message from setup(), for example Serial.println("Arduino is ready"); and my PC progrram listens for that before trying to do anything else.

This Python - Arduino demo illustrates what I am saying.

...R

The C# program open the port, wait 1 second for Arduino reset, check 1000 times in 100 seconds if data was received (it isn't the case) and close the port. So it's not continually opening and closing the port, it do this just 1 time.

To print "Arduino is ready" is my objectif but before do this I must achieve to received data from the Arduino.

Even a good program like this don't work for me. I don't understand anything !

TheRedColossus:
The C# program open the port, wait 1 second for Arduino reset, check 1000 times in 100 seconds if data was received (it isn’t the case) and close the port. So it’s not continually opening and closing the port, it do this just 1 time.

If you only want one piece of data from your Arduino then I think what you are doing should work. But if, when you get it working, you want to get other pieces of data you should not call port.close() until you have everthing.

It is possible that your port.open() is not actually causing the Arduino to reset. You may need to set the DTR and RTS settings for your serial port.

…R

Yes this program is just for check if data receiving works.

I already test to set the DTR and RST settings in many configurations :
DTR=false RTS=false
DTR=true RTS=true
DTR=true RTS=false
DTR=false RTS=true

But no one of these configurations works.

TheRedColossus:
But no one of these configurations works.

Then I have run out of ideas. As I said, I don't use Windows.

I believe my Python program will work with Windows if you edit the references to the serial port.

...R

Yes but I need a C# program because my final project is to control a rocket in the game KSP (devlopped in C#).

(deleted)

TheRedColossus:
My C# program :

Any reason you are using OpenNETCF.IO.Ports and not System.IO.Ports?

I changed to System.IO.Ports, port name and baud rate and it worked fine for me.

using System;
//using OpenNETCF.IO.Ports;
using System.IO.Ports;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort("COM43", 115200, Parity.None, 8, StopBits.One);
            port.Open();
            System.Threading.Thread.Sleep(1000); // waiting for Arduino reset

            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(port.BytesToRead); // it write on the console only zeros, so no data is received
                System.Threading.Thread.Sleep(100);
            }

            port.Close();
        }
    }
}

Bobcousins... you are a GENIUS !

I used OpenNETCF because for make this program I take an exemple on internet and when i saw that it didn't works I modified the program body without modify the imports.

Thanks a lot !