VC++/ CLR 2010 , VC# 2010 Serial Port using arduino

HI Guys,

I am trying to read data using an app designed by C# or VC++ /CLR / 2010, unfortunately no success. I know there is a lot of examples out there but non of them is working for me. It seems to be that the problem is when i use readline( ) It times out. I am posting my code and my arduino code hoe someone can help!

Arduino code :

int SensorPin = A0; // A0 is the sensor input
int SensorValue = 0;
float outputValue = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//analogReference(INTERNAL);
//Keyboard.begin();

}

void loop() {
// put your main code here, to run repeatedly:
// Read Values fro sensor
SensorValue = analogRead(SensorPin);
// map it to the range of the analog out:
outputValue = map(SensorValue, 0, 1023, 0, 5000);outputValue=outputValue/1000;

//outputValue = (SensorValue*5.0)/1023.0;

// Print value

Serial.print("sensor = " );
Serial.print(outputValue);
Serial.print("\t ");
Serial.println();

// delay a millesecond
delay(10) ;

}

VC++ / CLR code

private void button5_Click(object sender, EventArgs e)
{

ArduinoPort = new SerialPort();
ArduinoPort.BaudRate = 9600;
ArduinoPort.PortName = textBox2.Text;
ArduinoPort.Parity = Parity.None;
ArduinoPort.DataBits = 8;
ArduinoPort.StopBits = StopBits.One;
//ArduinoPort.Encoding = System.Text.Encoding.Default;
ArduinoPort.Open();
// ArduinoPort.WriteLine("analogRead(A0);");
// Data = ArduinoPort.ReadExisting();
//Data = ArduinoPort.ReadTo("\n");
timer1.Enabled = true;

timer1.Start();

}

private void timer1_Tick(object sender, EventArgs e)
{
Array REDCHR;
Data = ArduinoPort.Read(REDCHR 0, 5);
textBox1.Text = Data;
textBox1.Update();

}
Note :-- timer interval is 10 msec.
Also I read about the DATArecive handelr but I am not sure how to use it , if someone out there can explain it will be great

Thanks Guys !!

Correction the VC++ code is

VC++ / CLR code

private void button5_Click(object sender, EventArgs e)
{

ArduinoPort = new SerialPort();
ArduinoPort.BaudRate = 9600;
ArduinoPort.PortName = textBox2.Text;
ArduinoPort.Parity = Parity.None;
ArduinoPort.DataBits = 8;
ArduinoPort.StopBits = StopBits.One;
ArduinoPort.Open();
timer1.Enabled = true;

timer1.Start();

}

private void timer1_Tick(object sender, EventArgs e)
{

Data = ArduinoPort.Readline();
textBox1.Text = Data;
textBox1.Update();

}

First, please read Nick Gammon's post at the top of this Forum on the proper way to post source code here using code tags.

Next, I believe that your C# code will open a port but, seeing the Arduino COM port busy, will select a different COM port. Likewise, I see nothing in the Arduino code to check and see if something has come into the Serial object (e.g., Serial.available()). Use the control panel to find out if they are the same. If not, you need to work out a way to share the same COM port, or a least read/write from the known COM ports. Take a look at Robin2's discussion of Serial port usage.

...and the scale factor is 1024 not 1023.

econjack sorry I am new to the forums , I make sure to do it correct next time.

I think you are right it opens the port and I see the light on the Arduino turn on when I sen open port command.
But I am not sure how using Serial.available() will help in this case ?

I also don;t think there is a way to share the com port with both of them ? Why would I need to share the ports tho ?

thanks

Guys thanks a lot I figured it out thank alot
and here is the solution for any one else who is having the same problem

VC++:

You need to enable the following
ARPort.DtrEnable = true;
ARPort.RtsEnable = true;

For C#
you need to enable
ARPort.DtrEnable = true;
ARPort.RtsEnable = true;
also you need to invoke the TextBox1.Text when displaying data

thanks
guys