Modbus TCP主站发送消息 [英] Modbus TCP master send messages
问题描述
最近用C ++写了一个modbus tcp主应用程序。
但是有一个问题要问:如果我有一层应用程序A,B,C ,D四(或更多)用于请求modbus tcp slaver数据。
当我调用send()时,如何控制消息的发送间隔,我现在使用sleep(),但是感觉很蠢!如果我设置sleep(100),那么我收到的D消息请求数据将是300MS,如果消息更多,这个时间会更长,根本无法隐藏实时数据读取。
在这个问答tcp通讯中,如何控制消息连续发送?
我的尝试:
////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////////////////
Recently used C++ to write a modbus tcp master application.
but there is a problem to ask: If I have a layer of application A, B, C, D four (or more) used to request modbus tcp slaver data.
when I call send() , How to control the sending interval of the message, I now use the sleep(), but the feeling is stupid! If I set sleep (100), then I received the D message request data will be 300MS, if the message more, this time will be longer, simply can not hide the real-time data read.
That in this question and answer tcp communication, how to control the message to send continuously?
What I have tried:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
推荐答案
不要使用睡眠。在函数中发送数据并设置计时器回调函数检查结果。
Dont use sleep. Send the data in a function and set up a timer callback function, which checks the result.
有很多方法可以处理与PLC和其他智能设备的通信。在可能的情况下,我喜欢在连续的轮询/响应循环中设置它们。确切的机制将取决于您是否需要同步通信,或者您可以通过异步来逃避它。根据我使用Modbus TCP / IP的经验,它是一个非常快速的协议,因此我的轮询/响应时间约为20mS,这可能会因短暂的睡眠而受到限制。我总是做一个可调参数,通常设置为10毫秒。我实现这个的方式是我创建了一个单独的线程,只能读写数据块。数据块是包含缓冲区,起始地址和大小的数据结构。 comm线程将写入其发送块,休眠几毫秒,读取其接收块,再睡一会儿,然后重复此操作。有时,通信是由一个单独的进程完成的,该进程的数据块在共享内存中,因此多个客户端进程可以访问数据。无论哪种方式,实现它都是相当简单的事情。
There are lots of ways to handle communications with PLCs and other intelligent devices. When ever possible, I like to set them up in continuous poll/response loops. The exact mechanism will depend on whether you need synchronous communication or you can get away with it being asynchronous. In my experience with Modbus TCP/IP, it is a very fast protocol so I could have a poll/response period of about 20mS and this could be throttled with a short sleep. I always made that an adjustable parameter and it was usually set to 10mS. The way I implemented this is I made a separate thread that would only read and write data blocks. The data blocks were data structures that contained a buffer, a starting address, and a size. The comm thread would write its send block, sleep a few milliseconds, read its receive block, sleep a little more, and then repeat this. Sometimes, the communications was done by a separate process that had its data blocks in shared memory so multiple client processes could access the data. Either way, it's fairly simple matter to implement this.
这篇关于Modbus TCP主站发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!