Click here to Skip to main content
15,897,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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
Updated 12-Mar-13 21:30pm
v2
Comments
Karthik Harve 13-Mar-13 3:30am    
[Edit] pre tags added.
Bernhard Hiller 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 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

Remove all the timers & use threads to achieve the task. Threads increase the efficiency & speed of execution.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900