Click here to Skip to main content
15,791,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an application in which Lets says 50-60 Modbus supporting devices (Slaves) are connected to a Com Port which are communicating with my application in request response mechanism.


I want after every 15 min. request should be sent to every meter and response to be received from meter one by one.
communicating with multiple slave (Modbus protocol based)

For this i am making the use of System.Timers.timer to call the method lets say ReadAllSlave() after every 15 min.
In ReadAllSlave() i have used For loop to send the request and to receive response and using thread.sleep to maintain the delay..! but it seems that its not working and loop is executing in damn wired way.

C#
private void StartPoll()
       {
          double txtSampleRate = 15 * 60 * 1000;
          timer.Interval = txtSampleRate;
          timer.AutoReset = true;
          timer.Start();
       }



C#
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
     for(int index = 0; index<meterCount; Index++)
     {
           //Sending request to connected meter..  
           mb.SendFc3(m_slaveID[0], m_startRegAdd[0], m_noOfReg[0], ref value_meter);
           if (mb.modbusStatus == "Read successful")
               {
               
                       //Some code for writing the values in SQL Express database
               }

           //Wait for some time so that will not get timeout error for the next 
           //request..
           Thread.Sleep(10000);

      }
}



Can any one please suggest me the best approach to implement the same.

Thanks in advance.
Posted
Updated 2-Mar-13 4:54am
v3
Comments
OriginalGriff 2-Mar-13 9:23am    
We can't answer that!
"loop is executing in damn wired way" means absolutely nothing without being able to see both the code, and your screen! And we can't see either of them.
Please, give us enough details to work out what you are doing, and a better idea of what is going wrong!
Use the "Improve question" widget to edit your question and provide better information.
harshal samdani 2-Mar-13 9:46am    
Thanks for your feedback.. I have improved my question as per your instruction ..
Hope it will be enough for you to understand the problem..!
Looking for your valuable reply for the same.

1 solution

The first thing that springs to mind (apart from "Don't do that!" - but I'll come back to that later) is that I doubt very much if this approach is going to work. The problem is that unless your SendFC3 method waits for a response before returning, the communications medium must be pretty much instantaneous in order to get a read response in time to ever pass your if condition. And if it does, why are you sleeping for ten seconds afterwards?

The "Don't do that!" bit is pretty simple - why would you pause the main thread for long periods, when that just makes the user experience slow and awkward?

I'm not sure how your system works, but the way I would do it is to use a 10 second timer instead of 15 minutes, and each ten seconds kick off a background worker task to read a value from one meter. Each task would either time out itself (and respond that way, probably via the progress mechanism) or succeed and report that. That way, the communications are not blocking the user thread and each poll/response pair is kept more separate.

If your units time out then probably I would set the task up to send the request, wait five seconds and check for a response. If there isn't one, then timeout - if there is, save the results.
 
Share this answer
 
Comments
harshal samdani 2-Mar-13 10:26am    
Thanks for your feedback..!!

I too wondering that before returning from the sendFC3 method the loop is iterated step ahead for the next meter.. that's the reason i am causing the thread to sleep so that all communication media and meters are become ready for the next request.

If the response if of 4-5 bytes everything is working fine but if response size goes high in bytes than the problem is persistent. :(
OriginalGriff 2-Mar-13 10:34am    
Yes - communications takes time, especially if you have to wait for a remote device to wake up, work out what you are saying, decide on a response and talk back to you - so you can't just talk and expect a response immediately. MODBUS is not a fast comms medium, so depending on your speed and data size then it could take the best part of a real world second to get a response completely.

Try what I suggested - a reasonable delay before checking may solve your problem.

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