如何在轮询功能期间实现Modbus写请求 [英] How to implement Modbus write request during polling function

查看:1322
本文介绍了如何在轮询功能期间实现Modbus写请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用.net 2010实现Modbus通信,使用c#作为编码。我已经在本网站提供的代码的帮助下实现了轮询技术。因此截至目前,轮询过程进展顺利,但问题是当我向特定的从属轮询发送写入请求时停止或挂起UI。那么如何在Modbus中继续写入功能。以下是我的代码。



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.

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;
              
           }





问题是每当我在投票过程中点击btntest按钮执行某些操作时,轮询过程完全停止或有时写请求被发送到slave&也得到了回应。但在此之后,民意调查并未继续。为什么会这样?..?如何安全地向写入请求发送写入请求&得到它的回应&还继续进行轮询..?



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..?

推荐答案

上述代码只有在正确处理轮询时才能正常工作。早期的轮询是使用定时器完成的,因为上述问题正在发生,因此当使用线程执行与定时器相同的任务时,写请求问题得到解决。因此,使用正确的方法来获得正确的解决方案。
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.


这篇关于如何在轮询功能期间实现Modbus写请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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