This C# app Stucks ....Can any one say Why??

I develop a C# programme to get real time data From sensor ...
its working .. But i cant close it or click another button in form after i start reading ports...
so can any one tell me why is that ???
any ideas to correct the code ..

This is arduino Code

#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(1000);
}

This is C# Code

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);
      
        string[] data;

        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                port.Open();
            }
            catch
            {
                MessageBox.Show("Error of Opening tjhe Ports", "Atention");
            }
            timer1.Start();
            
           


            
            

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            while (port.IsOpen)
            {
                data = port.ReadLine().Split(',');

                textBox1.Text = data[0];
                textBox2.Text = data[1];
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            port.Close();
        }
    }
}

This is arduino Code

There are stickies at the top of the forum, describing how to post code. Nowhere in them does it suggest that green is a suitable color. Go read those posts, and try again.

i think now its ok .. any help ?

C# - Microsoft - says it all.

Mark

damith14:
so can any one tell me why is that ???

            while (port.IsOpen)

This code in your timer handler will prevent the handler from returning, so the calling thread will be blocked and not available to handle any other events. It would work better if you only stayed in the loop while there was input available on the port, rather than as long as the port remained open.

  Serial.print(int(dis));
  Serial.print(",");
  Serial.print(int(pulse));

Why do you feel that it is necessary to cast the values to int?

But i cant close it or click another button in form after i start reading ports...

Because, as PeterH points out, you wrote the code that way.

damith14:
... i cant close it or click another button in form after i start reading ports...
so can any one tell me why is that ???

This is really a c# problem: It's because you are running these three lines of code inside the while(port.IsOpen) loop and never leaving the loop to run anything else.

            while (port.IsOpen)

{
               data = port.ReadLine().Split(',');

textBox1.Text = data[0];
               textBox2.Text = data[1];
           }

c# provides Application.DoEvents() to allow other portions of your program to have some processing time when you are running a tight loop as above.

            while (port.IsOpen)
            {
                data = port.ReadLine().Split(',');

                textBox1.Text = data[0];
                textBox2.Text = data[1];

                Application.DoEvents();  ///Allow other parts of your program to receive messages/events.
            }

c# provides Application.DoEvents() to allow other portions of your program to have some processing time when you are running a tight loop as above.

You don't need a tight loop like that, and you don't need a timer to trigger the function. The SerialPort class has events. Subscribe to the right ones, and the callback will happen only when there is serial data to read.

True.

But as it is not directly pertinent to Arduino I did not feel the need to elaborate.

thanx for the advice guys ... its very help full ...
i will change the code ... thanx thanx
I love arduino communality :slight_smile: :smiley:

The thing you need to remember with the windows program is that the windows environment is a very complex multi threaded place. The key is to make your serial comms event driven. I have posted on your other thread with a breif example. If it would help people I will write a guide on serial programming in c#.

Chris

If it would help people I will write a guide on serial programming in c#.

It's been done before, but there is no harm in repeating it. I, for one, would like to see how you tackled the problem. I can then compare that to my method.

Yeah it would be help full....
If you can write it from A to Z .
It will be very helpful to newbies like me ...