Learn the basics of using the UDP protocol for communication in TwinCAT3 because of the needs of the project. This makes a simple note, convenient for later inquiries.
1 overview
In order to achieve the direct access network card from the real-time environment (network cards) specifically provides a function "Tcp/udp Realtime", this access is either from the PLC (61131-3), or from C + +. This function provides support for communication using the following protocols:
This function is used in TC3 to implement network communication as follows:
Regardless of the protocol used, the implementation of the communication connection between the project based on the protocol and the TC3 must be implemented through a pair of interfaces (interfaces):
- An interface pointer provides support for functions such as sending data and establishing a connection
- Implements a receiver interface based on a callback form (callbacks) that provides feedback to the project in the form of events or data
The communication component is a Tccom object--tcp/udp RT, which is instantiated and configured through a network card.
2 configuration of the TCP/UDP RT Module
1) Create the TCP/UDP RT module Under the real-time Ethernet Adapter
2) Select the corresponding module
3) Parameterization of previously created module instances
At this point, we have completed the instantiation of the TCP/UDP RT module.
3 Implementing a routine that has a reflection service
1) Create a TC3 solution under VS2012
2) Add a task under tasks. Then implement an interface of type ITCIOUDPPROTOCOLRECV in TMC files. Through the implementation of this interface, TC3 generates a method. This method is called when the UDP packet arrives.
The TCP/UDP RT module requires an interface pointer itcioudpprotocol that contains a reference to the Tcp/udp RT object.
3) Create an interface pointer with the Itcioudpprotocol type named Udpport
4) Generate TMC code. Next we highlight a code implementation detail
The Cycleupdate () method and Checkreceived () method in the TCP/UDP RT module must be called as part of the module.
5) The Cycleupdate () method is implemented as follows:
1 ///<autogeneratedcontent id= "Implementationof_itccyclic" >2HRESULT cmodule1::cycleupdate (itctask* iptask, itcunknown*Ipcaller, ulong_ptr context)3 {4HRESULT hr =S_OK;5m_counter+=M_inputs.value;6M_outputs.value=M_counter;7M_spudpprot->checkreceived ();//ADDED8 returnhr;9}
Receiveddata () method is created through the implementation of the interface, and this method is called repeatedly by checkreceived ().
6) Reveivedata () method to send the information and data as input parameters
1 ///<autogeneratedcontent id= "Implementationof_itcioudpprotocolrecv" >2HRESULT Cmodule1::receivedata (ulong ipaddr, USHORT udpdestport, USHORT udpsrcport, ULONG ndata, PVOID pData, Etype_vlan_h eader*PVlan)3 {4HRESULT hr =S_OK;5 //Mirror incomming Data6hr = M_spudpprot->senddata (ipaddr, Udpsrcport, Udpdestport, Ndata, PData,true);7M_trace.log (Tlinfo, Fleavea"UDP receivedata:ip:%d.%d.%d.%d udpsrcport:%d datasize:%d (hr2=%x) \ n", 8((pbyte) &ipaddr) [3], ((pbyte) &ipaddr) [2], ((pbyte) &ipaddr) [1], ((pbyte) &ipaddr) [0], 9 Udpsrcport, Ndata, hr);Ten returnhr; One } A ///</AutoGeneratedContent>
In the process of opening and terminating, a reference to a Udpprotocol interface must be set.
7) port Open is triggered during Safeop to OP,Registerreceiver opens a UDP port to receive data
1 HRESULT Cmodule1::setobjstateso ()2 {3HRESULT hr =S_OK;4 //START EDITING5 if(SUCCEEDED (HR) &&m_spudpprot.hasoid ())6 {7M_trace.log (Tlinfo, Fleavea"Register Udpprot");8 if(succeeded_dbg (hr = m_spsrv->Tcquerysmartobjectinterface (M_spudpprot)))9 {TenM_trace.log (Tlinfo, Fleavea"Server:udpprot Listen to Port:%d",10000); One if(FAILED (hr = M_spudpprot->registerreceiver (10000, A this_cast (ITCIOUDPPROTOCOLRECV) ))) - { -M_trace.log (Tlerror, Fleavea"Server:udpprotregisterreceiver failed on Port:%d",10000); theM_spudpprot =NULL; - } - } - } + - //If Following call is successful the Cycleupdate method would be + called, A //eventually even before method has a been left. athr = FAILED (HR)?Hr:addmoduletocaller (); - //Cleanup If transition failed at some stage - if(FAILED (HR)) - { - if(M_spudpprot! =NULL) -M_spudpprot->unregisterreceiver (10000); inM_spudpprot =NULL; - Removemodulefromcaller (); to } + //END EDITING -M_trace.log (Tlverbose, Fleavea"hr=0x%08x", hr); the returnhr; *}
8) Stop operation is performed during OP to Safeop
1 HRESULT Cmodule1::setobjstateos ()2 {3 M_trace.log (Tlverbose, Fentera);4HRESULT hr =S_OK;5 6 if(M_spudpprot! =NULL)7M_spudpprot->unregisterreceiver (10000);8M_spudpprot =NULL;9M_trace.log (Tlverbose, Fleavea"hr=0x%08x", hr);Ten returnhr; One}
At this point, the TCP/UDP RT module is instantiated and configured to complete.
C + + implementation of communication based on UDP protocol in TwinCAT 3