Pages: [1]   Go Down
Author Topic: how to make arduino send signal to csharp  (Read 1230 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
My problem is i dont really know how to make c# to read and interpret the signal from arduino. What im trying to do is an little c# app that sense if the light is on or off in a room and give a msg to the user. if any one could help me with this or give me a good tutorial for it.

My arduino code so far is

Code:
int sensePin = 0;
int ledPin = 13;

void setup()
{
  pinMode(ledPin, OUTPUT);
  analogReference(DEFAULT);
 
  Serial.begin(9600);
}

void loop ()
{
 
  delay(500);
 
  int val = analogRead(sensePin);
 
  if(val <4) digitalWrite(ledPin, HIGH);
  else digitalWrite(ledPin, LOW);
  if(val <4) Serial.print("1");
  else Serial.print("0");
}
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

see :    http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to find, open and read from the virtual COM port provided by the Arduino USB drivers. Here's an example:

Code:
       void Run()
        {
            // Create a new SerialPort object with default settings.
            SerialPort _serialPort = new SerialPort("COM8", 9600, Parity.None, 8);
            _serialPort.Handshake = Handshake.None;

            try
            {
                _serialPort.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine("Open failed: " + e);
                return;
            }

            while (true)
            {
                try
                {
                    string sample = _serialPort.ReadLine();
                    Handle(sample);
                }
                catch (TimeoutException) { }
            } // end while
        }
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Ok after som headache i wrote and made this piece of code to work in console but now comes my next problem i need to make it work in winform.

PS. Im pretty new to arduino and c#.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;

namespace ConsoleApplication1
{
    class Program
    {
      

        private static System.IO.Ports.SerialPort serialPort1;
        static void Main(string[] args)
        {

            System.ComponentModel.IContainer components = new System.ComponentModel.Container();
            serialPort1 = new System.IO.Ports.SerialPort(components);
            serialPort1.PortName = "COM5";
            serialPort1.BaudRate = 9600;

            serialPort1.Open();
            if (!serialPort1.IsOpen)
            {
                Console.WriteLine("somthing is wrong");
                return;
            }

            // this turns on !
            serialPort1.DtrEnable = true;

            // callback for text coming back from the arduino
            serialPort1.DataReceived += OnReceived;

            // give it 2 secs to start up the sketch
            System.Threading.Thread.Sleep(2000);

            using (serialPort1)
            {
                

                System.Threading.Thread.Sleep(15000);
            }

        }
        private static void OnReceived(object sender, SerialDataReceivedEventArgs c)
        {
            
            try
            {
                // write out text coming back from the arduino
                Console.Write(serialPort1.ReadExisting());
            }
            catch (Exception) { }
        }

    }
}
« Last Edit: September 23, 2012, 01:17:28 pm by xzeno » Logged

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

Quote
but now comes my next problem i need to make it work in winform.
The user interface code runs in one thread. The other parts of the application run in another thread. You'll need to add another thread for serial data processing, since that process is asynchronous. You'll need to add some code to allow the serial thread to communicate with the UI thread.

I have a C# application, using a form, that talks to the Arduino. PM me with an e-mail address, and I'll send you that application. You can use that as a starting point.
Logged

Pages: [1]   Go Up
Jump to: