Pages: [1]   Go Down
Author Topic: Read Water Level Sensor Data with c#  (Read 1077 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi all .
i want to read analog data from the water level sensor in arduino and in C# application to get data from the analog reading and display to richtextbox in C#

here is the C# code :
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace MenuUtama
{
    public partial class Form1 : Form
    {
        SerialPort ser = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] availablePorts;
            availablePorts = SerialPort.GetPortNames();
            for (int i = 0; i < availablePorts.Length; i++)
            {
                comboBox1.Items.Add(availablePorts[i]);
            }
            richTextBox1.Text = " WELCOME ";
        }

        private void button1_Click(object sender, EventArgs e)
        {
                    ser.PortName = comboBox1.Text;
                    ser.Open();
                    ser.Write("1");
                    ser.Close();
         }
          
        private void button2_Click(object sender, EventArgs e)
            {
                ser.PortName = comboBox1.Text;
                ser.Open();
                ser.Write("2");
                ser.Close();
            }
        
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        
    }
}


here is the Arduino code :
Code:
int Relay = 3;

void setup()
{
  pinMode(Relay, OUTPUT);
}

void loop()
{
  while (Serial.available() == 0);
  int nilai = Serial.read()-'0';
  
  if (nilai == 1)
  {
      Serial.println(analogRead(A10));
      delay(1000);
      digitalWrite(Relay, HIGH);
  }
  else if (nilai == 2)
  {
      digitalWrite(Relay, LOW);
  }
  
}

thanks before for your help smiley
« Last Edit: November 12, 2012, 11:28:25 pm by cyberz7 » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Please use the #button when posting code to apply the right tags
. The code is more readable and easier to debug etc
You can modify you post and select the code part (each separately) and press the # button ,

Thank you.

The problem is that the C# application does not read any serial data.

2 seconds of google
- http://www.dreamincode.net/forums/topic/35775-serial-port-communication-in-c%23/ -
- https://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx  - <<<<<<<< this is simpler !!!

« Last Edit: November 12, 2012, 03:43:47 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

hehe .
sorry .
i am new in this forum  smiley-sad

thank you very much for your help.

if i use serial.println in arduino code without input from c# app, i can read via serial monitor the value
but if i use c# app to run the arduino code, when i look at serial monitor, there is no value, empty
why is that ?


Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is probably because when the data is read from the serial port by an application it is removed and therfore
 it is not available any more for a second application that also (tries to) read from that port.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: