Go Down

Topic: how to make arduino send signal to csharp (Read 1 time) previous topic - next topic

xzeno

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: [Select]
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");
}


PeterH

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

Code: [Select]

       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
       }
I only provide help via the forum - please do not contact me for private consultancy.

xzeno

#3
Sep 23, 2012, 08:15 pm Last Edit: Sep 23, 2012, 08:17 pm by xzeno Reason: 1
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: [Select]
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) { }
       }

   }
}

PaulS

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.

Go Up