与多个从站连接到COM端口的通信(基于Modbus协议) [英] Communication with Multiple slave connected to com port (Modbus protocol based )

查看:424
本文介绍了与多个从站连接到COM端口的通信(基于Modbus协议)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中Lets说50-60 Modbus支持设备(从属)连接到一个Com端口,它在请求响应机制中与我的应用程序通信。





我想要每15分钟一次。请求应发送到每个仪表并响应从仪表逐个接收。

与多个从站通信(基于Modbus协议)



为此,我正在使用System.Timers.timer调用该方法,每15分钟后调用一次ReadAllSlave()。

在ReadAllSlave()中,我使用For循环发送请求并接收响应并使用thread.sleep来维持延迟..!但似乎它不工作并且循环正在以有线方式执行。



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.

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










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.

推荐答案

第一件事想起来(除了不要这样做! - 但我会稍后回过头来说)我非常怀疑这种方法是否有效。问题是,除非您的SendFC3方法在返回之前等待响应,否则通信介质必须非常即时,以便及时获得读取响应以通过您的if条件。如果确实如此,为什么你之后会睡十秒钟?



别这么做! bit非常简单 - 为什么你会长时间暂停主线程,这只会让用户体验变得缓慢而尴尬?



我不知道怎么样你的系统工作正常,但我这样做的方法是使用10秒计时器而不是15分钟,每10秒启动一个后台工作人员任务来读取一米的值。每个任务要么超时(并以可能通过进度机制的方式响应),要么成功并报告。这样,通信不会阻塞用户线程,并且每个轮询/响应对保持更加分离。



如果你的单位超时那么我可能会设置任务最多发送请求,等待五秒钟并检查响应。如果没有,则超时 - 如果有,则保存结果。
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.


这篇关于与多个从站连接到COM端口的通信(基于Modbus协议)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆