Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I am implementing Modbus communication using .net 2010 using c# as coding. I have achieved polling technique with help of the code available from this website only. So as of now polling process is going on well, but the problem is when i send write requests to a particular slave polling stops or hangs the UI. So how to proceed for write function in Modbus. The following is my code.

C#
private void btnTest_Click_1(object sender, EventArgs e)
       {                    
           if (flagtest == 0)
           {
                   ushort start = 0;
                   short[] value = new short[1];                

                   try
                   {
                       while (!mb.SendFc06(SlvID1, start, (ushort)2, value)) ;
                   }
                   catch (Exception err)
                   {
                       DoGUIStatus(Error in write function:err.Message);
                   }
                 
                   btnTest.BackgroundImage = global::AnnunciatorMonitoringTool.Properties.Resources.lightbulb;
                  
                   flagtest = 1;
           }
           else
           {
               flagtest = 0;
               btnTest.BackgroundImage = global::AnnunciatorMonitoringTool.Properties.Resources.lightbulb_off;
              
           }


The problem is whenever i click on the btntest button during polling process to perform some action, the polling process completely stops or sometimes the write request is sent to slave & response is also obtained. But after this the polling doesn't continue. Why is that happening..? How do i send write requests safely to slaves & get response from it & also continue with polling..?
Posted
Updated 24-Mar-13 21:20pm
v2

1 solution

The above code works fine only if polling is handled properly. Earlier polling was done using timers because of which the above stated problem was occurring, so when threads were used to perform the same task as that of timers the write request problem got resolved. So use right method to get right solution.
 
Share this answer
 
v2

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