Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

Updated 27-Jun-17 8:32am

Dont use sleep. Send the data in a function and set up a timer callback function, which checks the result.
Share this answer
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.
Share this answer

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