Go Down

Topic: C# serial communication (Read 1 time) previous topic - next topic

TheRedColossus

Jul 25, 2017, 11:25 pm Last Edit: Jul 25, 2017, 11:31 pm by TheRedColossus
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) :

Code: [Select]
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 :

Code: [Select]
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 !  :smiley-cry:
Thanks.  :D

spycatcher2k

#1
Jul 25, 2017, 11:31 pm Last Edit: Jul 25, 2017, 11:31 pm by spycatcher2k
This is a C# question - better suited a C# forum (or google has plenty of answers - C# Serial communication)
Mrs Drew
-=-
UK Based PCB Manufacturing - PM for details
I will design & code for you, but I will also charge you (PM me)
Use Google or search this site BEFORE asking!

TheRedColossus

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

spycatcher2k

With YOUR 'C#' Program . . . See where this is going?
Mrs Drew
-=-
UK Based PCB Manufacturing - PM for details
I will design & code for you, but I will also charge you (PM me)
Use Google or search this site BEFORE asking!

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

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.

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 !

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

TheRedColossus

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

TheRedColossus

#9
Jul 26, 2017, 01:51 pm Last Edit: Jul 26, 2017, 01:57 pm by TheRedColossus
Yes but I need a C# program because my final project is to control a rocket in the game KSP (devlopped in C#).

spycatcher2k

Then YOU need to learn C# or go to a forum that supports it!

I can't make it any more clear for you.
Mrs Drew
-=-
UK Based PCB Manufacturing - PM for details
I will design & code for you, but I will also charge you (PM me)
Use Google or search this site BEFORE asking!

bobcousins

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.

Code: [Select]
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();
        }
    }
}
Please ask questions in the forum so everyone can benefit. PM me for paid work.

TheRedColossus

#12
Jul 26, 2017, 04:46 pm Last Edit: Jul 26, 2017, 04:51 pm by TheRedColossus
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 !

Go Up