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) :
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 !
Thanks.
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.
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.
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();
}
}
}
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.