Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Hi,
I have developed an application using C#. The application is developed to serve the need for MODBUS communication. Basically i am sending queries to the slaves & getting response using 1 timer & based on the data received i am trying to flash the button in the UI. My problem the process of flashing of button is too slow. So i wanted to know the probable reason for it. Following code shows the polling process & flashing event also.
 
private void StartPoll()
{
   pollCount = 0;
   slaves = 0;
 

   if (mb.Open(cboPort.SelectedItem.ToString(), Convert.ToInt32(cboBaud.SelectedItem.ToString()),
       8, Parity.None, StopBits.One))
   //Now open The Port
   {
       //Disable double starts:
       btnstart.Enabled = false;
       btnstop.Enabled = true;
 
       //Set polling flag:
       isPolling = true;
 
       //Start timer using provided values:
       timer.AutoReset = true;
       timer.Interval = 500;
       timer.Start();            
    }
 
}
 
 #region Timer Elapsed Event Handler
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    PollFunction();
}
#end region
 
# region Pollingfuntion
private void PollFunction()
{
   //Update GUI:
   DoGUIClear();
   pollCount++;
 
 
   //Create array to accept read values:
   string[] rs485data = new string[72];
 
   ushort pollStart;
   ushort pollLength;
 
   pollStart = 0;
   pollLength = 72;
 
   string stringTocheck0 = "0";
   string stringTocheck1 = "1";
   string stringTocheck2 = "2";
 
 
   SID = Convert.ToByte(slaveaddress[slaves]);
   add = slaves;
 

   //Read registers and display data in desired format:
   try
   {
       while (!mb.SendFc3(SID, pollStart, pollLength, ref values)) ;
   }
 
   catch (Exception err)
   {
       DoGUIStatus("Error in modbus read: " + err.Message);
   }
 
   slaves++;
 
   for (int s = 0; s < 72; s++)
   {
       rs485data[s] = Convert.ToString(values[s + 1]);
   }
 
   switch (dataType)
   {
 
       case "Decimal":
 
           if (rs485data.All(s =>; s.Contains(stringTocheck0)) == true)
           {
               errorIndexZero[add] = SID;
               this.Invoke(new EventHandler(ResetChangeBtnColor));
           }
 
           else if (rs485data.Any(s =>; s.Contains(stringTocheck1)) == true)
           {
               errorIndexOne[add] = SID;
               this.Invoke(new EventHandler(TestChangeBtnColor));                   
 
           }
           else if (rs485data.Any(s =>; s.Contains(stringTocheck2)) == true)
           {
               errorIndexTwo[add] = SID;
               this.Invoke(new EventHandler(AcceptChangeBtnColor));
           }
 
           break;
   }
 

   if (slaves == devicescount1)
   {
       slaves = 0;       
   }
}
#end region
 
public void TestChangeBtnColor(object sender, System.EventArgs e)
{
   timer2.Enabled = true;           
}
private void timer2_Tick(object sender, EventArgs e)
{
   for (int n = 0; n < btn.Length; n++)
   {
        
         if (errorIndexOne[n]!=0)
          {
              if (btn[n].BackColor == Color.LightGreen)
               {
                   btn[n].BackColor = Color.Red;
               }
               else
               {
                   btn[n].BackColor = Color.LightGreen;
               }
          }
 
      }
}
Posted 12-Mar-13 22:17pm
Edited 12-Mar-13 22:30pm
v2
Comments
Karthik Harve at 13-Mar-13 3:30am
   
[Edit] pre tags added.
Bernhard Hiller at 13-Mar-13 3:49am
   
What type of Timer do you use? There are timers which are running in the UI thread (to be avoided for your polling function) and timers running in a different thread.
Jagadisha_Ingenious at 13-Mar-13 4:35am
   
@Bernhard Hiller:I have used system timer for data processing event & windows form timer for flashing of button..

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Remove all the timers & use threads to achieve the task. Threads increase the efficiency & speed of execution.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 17-Jun-13 2:53am
   
Are you talking to yourself? Let me tell you: this one of your fake answers. Please stop it.
—SA
Jagadisha_Ingenious at 17-Jun-13 3:57am
   
@Sergey Alexandrovich Kryukov: When i had asked this question none answered it, so when i found a solution i added it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100