cannot read the data from arduino to the c#

here is my program is c#

   public bool Connect()
        {
        button1.Enabled = false;

            try
            {
                SP1.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid Serial Port");
                return false;
            }

            if (SP1.IsOpen)
            {
                System.Threading.Thread.Sleep(500);
                string data = SP1.ReadExisting();
                SP1.WriteLine(data);
                System.Threading.Thread.Sleep(500);

however i cannot receive the data from arduino through serial port into the c#..how the readexisting functions?does the writeline function enable the data to be write into the c#?sorry,im new in c# and arduino.hope all of u can help.

Not done any C# myself but I can't help but notice that no where do you specify the baud rate. You need to do that so look at the C# manure to tell you how to do it.

look at the C# manure

I have heard many terms for documentation before, but surely this is the most accurate.

arduino_learners: in addition to the baud rate you probably have to tell it which COM: port to use.

For what it's worth, there is a little C# info here in the Playground: http://playground.arduino.cc/Interfacing/csharp

-br

i have initialized the serial port.

 SP1 = new SerialPort("COM3" ,9600,Parity.None,8,StopBits.One);

Which Arduino are you using?

arduino uno r3 compatible.

This is what the first part of my connect button's callback code looks like:

        private void connect_Click(object sender, EventArgs e)
        {
            System.ComponentModel.IContainer components =
                new System.ComponentModel.Container();
            port = new System.IO.Ports.SerialPort(components);
            port.PortName = comPort.SelectedItem.ToString();
            port.BaudRate = Int32.Parse(baudRate.SelectedItem.ToString());
            port.DtrEnable = true;
            port.ReadTimeout = 5000;
            port.WriteTimeout = 500;
            port.Open();

The form has drop down lists for the com port and baud rate.

With this code, I'm able to send to/read from all the Arduinos I have (Mega, Leonardo, and Duemilanove).

(port is a global instance of the SerialPort class.)

so the coding for writeline and readline cannot read the data from arduino?

so the coding for writeline and readline cannot read the data from arduino?

I don't understand this statement/question. Of course it is possible to write to the serial port that the Arduino is connected to, and read from that serial port.

Of course, the Arduino must be sending data for there to be something to read and the Arduino must be reading the data for there to be a reason to send it.

You seem to have nothing on the PC end more than an echoer of what the Arduino sent, so, how do you know it isn't working?

i am able to receive the da ta on the arduino because i have check it using the SerialMonitor of the arduino.however, i cannot get those data received to be transferred to my GUI using c#.nothing appeared on the GUI side.

i cannot get those data received to be transferred to my GUI using c#.nothing appeared on the GUI side.

That is MUCH different that what you were first complaining about. The serial data is handled in one thread. The GUI is handled in another thread.

The attached application creates a new thread to handle the serial data, and appropriate methods to share data between threads. Perhaps you could study it.

CommunicateWithArduino.zip (42.1 KB)

billroy:

look at the C# manure

I have heard many terms for documentation before, but surely this is the most accurate.

Yes the auto corrector on the iPad strikes again. :) :) But as you say it is most appropriate.

But as you say it is most appropriate.

Hey, now. If you've never used C#, how do you know that?

Well, aside from the fact that it is from miCroSofT, to whom case is irrelevant.

Hey, now. If you've never used C#, how do you know that?

Because I have heard engineers working for me deride it and call it "I can't believe it's not Java" and yes it's from Micro$oft.

that's means it will not read the data into gui?

arduino_learners: that's means it will not read the data into gui?

No it will read data if you send it data and if you write the code correctly.

PaulS, my problem now, the serialport is open..however the data cannnot be uploaded in the WPF? if every function im doing the threading part..is it works to received the data in synchronous manner? im handle the serial port class in one threads and the data in another threads..

Did you try running my application? Did you look at how it handles the serial data in one thread and communicates with the GUI thread?

yes.n i have take a look on the threading..when i click on the button..it says "The port is already open". However there is no data appeared on the WPF.it appeared in the console application and arduino serial monitor but not in the WPF C# application.

when i click on the button..it says "The port is already open". However there is no data appeared on the WPF.

Are you trying to run my application and the serial monitor at the same time?

If my application can't open the serial port, is it any wonder why no serial data appear on the form?