I have to connect to another system by using UDP protocol but the port that used to connect is randomly and generated by another system. When I connected, the PFC200 need to use the port follow port number that generated from another system. How can I find the port number or how to scan the port which one is opened.
Hello, how can you know what port is for what communication ?
If your device A has a UDP server which port is random, i don’t think there is a possibility to find this port without having to test / scan all port and this can take a long time.
Another possibility is the device A send to the PFC on a known UDP port his opened port number so the PFC can connect to it.
I captured the message via wireshark and find out, the device A will random the source port and need the PFC send message back at the same port. but the destination port is fixed at port 44045.
And the number of source port will send via UDP message but I used the UDP NBS library it show only the data, i not found the message of port number for source port.
You will need to set the fix destination port in codesys like 5000. Then you need to write the code for dynamicly accept all incoming requests via udp ip and port address. Maybe you can find a basic example which created with node-red and also you can do it in codesys as well. The thing that ı have mentioned is like that;
(Receive UDP data from any sender)
IF Error = 0 THEN
BytesReceived := SysSockRecvFrom(UdpSocket, ADR(ReceiveBuffer), SIZEOF(ReceiveBuffer), 0, ADR(ClientAddr));
IF BytesReceived > 0 THEN
(ClientAddr now contains the sender's IP and dynamic port)
(* Step 5: Send response to the sender *)
BytesSent := SysSockSendTo(UdpSocket, ADR(SendBuffer), SIZEOF(SendBuffer), 0, ADR(ClientAddr));
END_IF
You might think about trying Node Red running on the PFC to receive the port number from Device A. Then you can use NVL’s to get the UDP port number into the Codesys runtime.