我目前正在尝试使用Modbus / TCP与PLC通信但是我甚至认为我可以读取PLC发送的Modbus帧我必须给端口502的Modbus响应和我使用的流发送到由PLC发送帧。我尝试使用两个流来接收和发送但是当我关闭第一个时,PLC将其作为超时通信然后拒绝第二个连接。 如果它可以帮到你这里是我使用的代码,但现在除了允许我测试连接之外它几乎没有做任何事情。
#define _WIN32_WINNT 0x0501
#include <iostream>
#include <boost/asio.hpp>
#include <boost/system/config.hpp>
#include <string>
using namespace std;
using boost::asio::ip::tcp;
int main()
{
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), 502);
tcp::acceptor acceptor(io_service, endpoint);
while (1)
{
int i =0;
string buff;
char buf[30];
tcp::iostream stream;
tcp::iostream s("192.168.10.150", "502");
acceptor.accept(*stream.rdbuf());
getline(stream, buff);
cout<<buff<<endl;
s <<buff;
}
}
如果您有任何建议。