Go Down

Topic: cannot read the data from arduino to the c# (Read 5764 times) previous topic - next topic

arduino_learners

here is my program is c#
Code: [Select]
   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.

Grumpy_Mike

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.

billroy

Quote

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

arduino_learners

i have initialized the serial port.
Code: [Select]
SP1 = new SerialPort("COM3" ,9600,Parity.None,8,StopBits.One);

PaulS



PaulS

This is what the first part of my connect button's callback code looks like:
Code: [Select]
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.)

arduino_learners

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

PaulS

Quote
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?

arduino_learners

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.

PaulS

Quote
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.

Grumpy_Mike


Quote

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.

PaulS

Quote
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.

Grumpy_Mike

Quote
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.

arduino_learners

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

Go Up