UDP communication between PLC S7-1200 and Simulink

19 views (last 30 days)
Jakub
Jakub on 28 Oct 2025 at 9:00
Edited: Tridib on 3 Nov 2025 at 12:11
Hi, I have a problem with UDP communication between a PLC S7-1200 and Simulink. From Simulink to the PLC I send 29 bytes of data (6 double values and an 8-bit array). The PLC sends the same amount of data back to Simulink every 100 ms, but with an additional 2-byte header that my MATLAB Function uses to detect the start of the packet. The issue is that many packets are lost (display missed), and the number of packets received from the PLC is much higher than the number of packets sent from Simulink (red frames). I would like to reduce packet loss and have a similar number of packets sent and received on both sides. I tried changing the step size and sample time in the UDP Receive block, and also changing the PLC send frequency, but it did not fix the issue. Reducing the PLC frequency only reduced the number of packets from Simulink proportionally. Any help or ideas on what could be the cause and how to improve the communication would be very appreciated.

Answers (1)

Tridib
Tridib on 3 Nov 2025 at 11:56
Edited: Tridib on 3 Nov 2025 at 12:11
Hi @Jakub,
UDP does not guarantee that messages will arrive, come in the right order, or not get repeated. It is normal to lose some packets, especially if you send them quickly or the network is busy. If you set the sample time for the UDP Receive block too high, you might miss packets because the buffer fills up. If it is too low, you could be checking too often, which is not efficient. Network issues like congestion, switches, or firewalls can also cause delays or lost packets.
Here are some troubleshooting steps which might help resolve the issue:
  • Lower the sample time a bit more. Also, try increasing the buffer size in the UDP Receive block to handle more data at once.
  • Test your setup with a direct Ethernet cable, without any switches or routers in between. Use a tool like Wireshark to see if packets are being lost.
  • Ensure the PLC and Simulink are synchronized in terms of send/receive timing. For example, if the PLC sends data every 100 ms, set Simulink’s step size to 100 ms too. You might also want to add a small pause on the PLC after each packet is sent.
  • Double-check your MATLAB Function block that looks for headers. Make sure it is not skipping valid packets because of parsing mistakes.
Hope this helps!

Categories

Find more on Simulink PLC Coder in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!