locked
Serial port Communication problem RRS feed

  • Question

  • Hello , I am using Visual C# to develop program which will read information send via Serial Port with Arduino uno board , where is uploaded program which send number from 0 to 1023 delaying one second ( depending on input signal ) and my program has to read this signal and then display in listbox . I have declared an infinite loop ( for ) , which permanently reads the input value .

    my written code is working perfectly , but when i press button , which includes this code below , the program is hanging ( it doesn't functioning any more ) , but values in listbox are being added , but i can't press any other buttons , i even can't close program .

    code looks like this :

    string input = ""; arduinoPort.Open (); for (int i = 0;;i++) { info = arduinoPort.ReadLine(); listBox1.Items.Add (info); } arduinoPort.Close();

    Thursday, May 3, 2012 7:17 PM

Answers

  • Hi,

    To avoid hanging use Application.DoEvents()

    eg:

    string input = "";
    arduinoPort
    .Open ();
    for (int i = 0;;i++)
    {
       info
    = arduinoPort.ReadLine();
       listBox1
    .Items.Add (info);

      Application.DoEvents();
    }
    arduinoPort
    .Close();


    PS.Shakeer Hussain

    Friday, May 4, 2012 12:13 PM

All replies

  • Put the code in a Timer.Tick event.  You're blocking the thread.
    Thursday, May 3, 2012 8:24 PM
  • Sorry but ,can you explain me more clearly with example code please .
    Friday, May 4, 2012 11:03 AM
  • Hi,

    To avoid hanging use Application.DoEvents()

    eg:

    string input = "";
    arduinoPort
    .Open ();
    for (int i = 0;;i++)
    {
       info
    = arduinoPort.ReadLine();
       listBox1
    .Items.Add (info);

      Application.DoEvents();
    }
    arduinoPort
    .Close();


    PS.Shakeer Hussain

    Friday, May 4, 2012 12:13 PM
  • thank you , it worked , can you explain me what this one line makes ?
    Friday, May 4, 2012 5:29 PM
  • thank you , it worked , can you explain me what this one line makes ?
    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

       


    Friday, May 4, 2012 5:33 PM