Offline
Newbie
Karma: 0
Posts: 39
|
 |
« on: January 09, 2013, 07:53:59 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #1 on: January 09, 2013, 08:06:36 am » |
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
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #2 on: January 09, 2013, 08:10:19 am » |
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
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #3 on: January 09, 2013, 08:12:43 am » |
i have initialized the serial port. SP1 = new SerialPort("COM3" ,9600,Parity.None,8,StopBits.One);
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #4 on: January 09, 2013, 09:07:53 am » |
Which Arduino are you using?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #5 on: January 09, 2013, 09:24:47 am » |
arduino uno r3 compatible.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #6 on: January 09, 2013, 09:29:21 am » |
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.)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #7 on: January 09, 2013, 09:37:37 am » |
so the coding for writeline and readline cannot read the data from arduino?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #8 on: January 09, 2013, 09:45:43 am » |
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
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #9 on: January 09, 2013, 09:58:43 am » |
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
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #10 on: January 09, 2013, 10:27:46 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #11 on: January 09, 2013, 10:47:53 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
|
 |
« Reply #12 on: January 09, 2013, 10:50:52 am » |
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
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #13 on: January 09, 2013, 12:34:13 pm » |
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
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #14 on: January 09, 2013, 06:06:09 pm » |
that's means it will not read the data into gui?
|
|
|
|
|
Logged
|
|
|
|
|
|