I am currently trying to communicate with a PLC by using Modbus/TCP but even thought I can read the Modbus frame send by the PLC I must give the Modbus response to port 502 and the stream I use send to the port used by the PLC to send the frame. I tried using two stream to receive and send but when i close the first one the PLC get that as a timed out communication and then refuse the second connection. If it can help you here is the code I use but for now it barely does anything else than allowing me to test the connection.
#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;
}
}
If you have any suggestion.