Arduino PC data transfer system,C# app

I'm working on C# app for ARDUINO Robot Project..
i have 2 Ultrasonic sensors .so I want to get sensor data to my pc Via Serial Connection ..
I try to develop C# app for get data and control robot.
My app cant read Sensor data separately.They all mixing up ..
I think the method i use for serial communication is wrong ..
Can any One tell me how send data from multiple sensors to computer Via Serial Connection..

My app cant read Sensor data separately.They all mixing up ..

Your code?

I think the method i use for serial communication is wrong ..

Your code?

Can any One tell me how send data from multiple sensors to computer Via Serial Connection..

Yes, but it would make far more sense in the context of your sensor data in your code.

This is Arduino Code... Actually there is One sensor because im still testing
#define echo 7
#define trig 12
long dis,pulse;

void setup()
{
pinMode(echo,INPUT);
pinMode(trig,OUTPUT);
Serial.begin(9600);

}
void loop()
{
digitalWrite(trig,LOW);
delayMicroseconds(10);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
pulse=pulseIn(echo,HIGH);
dis=pulse/52.8;
Serial.print(int(dis));
Serial.print(",");
Serial.print(int(pulse));
Serial.print("\n");

delay(500);
}

THIS IS THE C# CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort("COM15", 9600);

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
port.Open();
}
catch
{
}

textBox1.Text = port.ReadExisting();
textBox2.Text = port.ReadExisting();

}
}
}

[ code]
your code
[ /code]

please!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

dis and pulse are type long, signed 32 bit integers.
They should be unsigned long since pulseIn() returns UL and your data can't ever be negative.

pulseIn() returns an unsigned long

52.8 is a float.
There are more accurate ways to scale integers, like with unsigned long multiply by 528UL then divide by 10UL.
If nothing else, integer math is much faster than floating point math on a no-FPU Arduino.

When you do mixed type math with no casting at all, don't expect good results always unless you like being wrong.

And that's just part of it.

BTW, speed of sound changes with temperature.

In the forum post edit window there are click buttons over the smileys. The # button makes code tags.

You should give MegunoLink a go. It can receive your data no problem. It can also log, plot, and you can use it to control the robot using a custom GUI.

Phil

thanx For the advice guys ... I will develop another programme . I'll try .... thanx again

Have you tried just printing distance to Serial monitor to check your Arduino output?
If it is right then the C# code likely has the error.

yeah im developing it .. thanx

Hi There,

Just had a quick look at your c# code and I belive that is where your problem is. The way you have it at the moment you are only opening the serial connection and looking for data then you click on a button.
The solution is to ensure that your c# program is event driven, based on receiving data.
What you need to do is have the button connect the serial port and then use the "DataReceived" event from the serial port object to read the information.

Here is a really simple serial terminal program in c# which you can base your code on:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] ports = SerialPort.GetPortNames();
            listBox1.DataSource = ports;
            disconnectButton.Enabled = false;
            sendButton.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
            {
                serialPort1.PortName = listBox1.SelectedItem.ToString();
                try
                {
                    serialPort1.Open();
                }
                catch
                {
                    MessageBox.Show("Unable to open the requested serial port");
                    return;
                }
                sendButton.Enabled = true;
                disconnectButton.Enabled = true;
                connectButton.Enabled = false;
 
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Close();
                }
                catch
                {
                    MessageBox.Show("Unable to close the serial port");
                }
            }
            sendButton.Enabled = false;
            connectButton.Enabled = true;
            disconnectButton.Enabled = false;
        }


        private void serialDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            incoming.Invoke(new EventHandler (delegate{
               incoming.AppendText(serialPort1.ReadExisting()); 
            }));

        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(sendString.Text);
            sendString.Clear();
        }


    }

There are a few things you can do to make your life easier. When you sent the data over the serial connection, if you use the end of line character (like you have) you can change serialPort1.ReadExisting() to serialPort1.ReadLine() which will wait for a newline character before processing the incoming data. The downside of this is that it will block the program until the newline arrives, so make sure you send it! (or set a readTimeout).

Here is a link where you can download the above as a working project (assuming you are using VisualStudio)
http://miselph.co.uk/arduino/Serial%20Terminal.zip

Hope that helps.

thank you very much .... its really helpful

which will wait for a newline character before processing the incoming data. The downside of this is that it will block the program until the newline arrives, so make sure you send it! (or set a readTimeout).

Hi guys, I could really use your help with something.. What I want to do is every time I press a button I want to read the serial port but only once... and I thoght I could take advantage of the read.Timeout , but i don't really know how and where to use it... Can anyone help?

moux:
Hi guys, I could really use your help with something.. What I want to do is every time I press a button I want to read the serial port but only once... and I thoght I could take advantage of the read.Timeout , but i don't really know how and where to use it... Can anyone help?

This presupposes that there is something to read when you press the button - how do you manage to coordinate your button presses with transmissions from the Arduino?

I suggest one of two approaches ...

Have your PC program receive data continuously but just update the display when you press the button

OR

When you press the button have the PC send a command to the Arduino that causes the Arduino to send stuff back.

...R

At which end? I don't know C# but from the look of it I don't see non-blocking real time (event) code but rather a reliance on system and blocking functions. There's a different approach that avoids all that and it starts with time-triggered code which you then add even triggers like

if ( Serial.available() )
{
// you get ONE character from serial here and either process it or store it in a char array buffer until it is '\n'.
// when you find '\n' that is a newline char that marks EOL (end of line), you set a variable to trigger the next
// stage function that processes the line of data.
// OR you process every char here and trigger other steps on a character by character basis which is more
// responsive, but funny how often it's learn the harder (buffer then use string manipulation) way before you
// can see better.
}

See what Nick Gammon is doing on this page, his explanation is down to Earth simple. He explains blocking and how to avoid it as part of a flowing, real time approach: How to do multiple things at once ... like cook bacon and eggs

This is a key to the door, make no mistake. The base idea is simple, the power is in the view of no longer holding up the whole program in a step by step top to bottom sequence. It's the key to writing code as segments that run when needed, not because that's the order they were put in. When you can do that, as he shows, you're free.
First you learn to trigger on time, then to trigger on events and the values of variables comes easy. The hard part is to let go of top-down and reliance on an operating system through 'do it for you' functions.

hey ...really really thanks for the advice ...its very helpful ..thanks again

There's the inkey$ command in BASIC for keyboard event-driven code.It was at the heart of a lot of amateur RT games back in the 80's, like Pac-Man clones back when magazines listed source.

Because syntax can be a pain, do you have bookmarks for the reference sections of the Arduino and AVR_LibC sites?

thnx a lot guys!!! I figured it out without using those commands!