I am new in PLCs, and I would like to can send data from the PLC to a PC application that it would be listening and the it could act in consecuence.
For example, the PLC detects an alarm and the it would send this alarm to the PC. The PC receive this message and then does something like send an email, save the data in a database or another things.
I have read the documentation about libraries like Sharp7, S7Net, S7Net Plus and others, but the examples that I see is that the PC reads data from the PLC, but it doesn't receive data from the PLC.
I see an example in which the PC is reading in while / true loop the variables in the PLC, but I guess it is a worse solution buecause it requieres more resources, I think it is better if the PLC could notify the alarm and then PC is just waiting if something happend and handle it when it is notify.
Thanks.
There are a number of ways to do this.
You can use OPC/UA and subscribe to PLC data. You will need to purchase an OPC/UA license to install on the PLC. Here is a guide, with an example Visual Studio project, which has a subscription feature: Creating of OPC UA clients with .NET and helper class
You can use MQTT as mentioned by dwpessoa. Here's a link to the SIOS page related to the link he posted, including a sample TIA Portal project: Use the SIMATIC controller as an MQTT client
You could use TCP sockets. The PLC transmits strings of data to the PC. You can set an event for incoming data in your code. Search for help on the TCON / TSEND instructions, or for a rather thorough example, see here: Basic Examples for Open User Communication (OUC)
You can even write to a MS SQL database on the PC if you like: Connecting a S7-1200 PLC / S7-1500 PLC to a SQL Database
And there are more...