0

I have to communicate to a PLC via C# UdpClient class.

IPEndPoint target = new IPEndPoint(IPAddress.Parse(ip), port);
udpClient = new UdpClient(target.AddressFamily);
udpClient.Connect(target);

var command = CreateWriteHeader(dm, values);
udpClient.Send(command, command.Length);

var data = udpClient.Receive(ref target);

The byte sequence I'm sending is correct, the PLC is sending me back a respose, so I'm sure the communication is happening. The problem is that the response I'm receiving contains an error code.

Checking the protocol manual, it seems it's a "cannot write" error code, where I'm sending a read request. Contacting the PLC support they say that for UDP connections the local port on my pc must be the same of the target port I'm connecting to (9600), otherwise the PLC refuses to work.

How can I specify the port I'm connecting from? I see I can set a port to the UdpClient object, but it is a listening port, right? Not the port I'm connecting from...

WaveMax
  • 191
  • 4
  • 15

0 Answers0