Pages: [1] 2 3   Go Down
Author Topic: cannot read the data from arduino to the c#  (Read 5205 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34502
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which Arduino are you using?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

arduino uno r3 compatible.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

* CommunicateWithArduino.zip (42.11 KB - downloaded 21 times.)
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34502
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley smiley
But as you say it is most appropriate.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 34502
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: