Tutorial
This tutorial illustrates the work steps from an empty TwinCAT project to a dispatched message. It depicts the properties of the TwinCAT 3 EventLogger described in the Technical Introduction section in the work sequence.
Creating an event class in the data types of the TMC file in the C++ project
- A new TwinCAT C++ project exists with the module from the wizard "TwinCAT Module Class with Cyclic IO".
- 1. Click twice on the TMC file in the C++ project to open the TMC editor. Select the command Add new event class… from the context menu of Data Types.
- The TMC editor opens the event class.
- 2. Give the event class a name and optionally enter a display text.
- 3. An event is already created below the event class. Give the event a name and enter a display text and the severity.
- 4. Use the event class in the previously created module.
- 5. In addition, create an input bSend in order to send the event on a rising edge.
- 6. Generate the source code of the module.
Creating a C++ program
- 7. Add headers to the Untitled1Interfaces.h in the C++ program:
#include "TcRouterInterfaces.h"
#include "TcEventLoggerInterfaces.h"
- 8. You need the following declarations in the Module1.h:
UINT m_counter;
ITcEventLoggerPtr m_spEventLogger;
ITcMessagePtr m_spMessage;
BOOL m_OldSend;
- 9. Initialize the following values in the constructor of the module in Module1.cpp:
CModule1::CModule1()
: m_Trace(m_TraceLevelMax, m_spSrv)
, m_counter(0)
{
///<AutoGeneratedContent id="MemberInitialization">
m_TraceLevelMax = tlAlways;
memset(&m_Parameter, 0, sizeof(m_Parameter));
memset(&m_Inputs, 0, sizeof(m_Inputs));
memset(&m_Outputs, 0, sizeof(m_Outputs));
///</AutoGeneratedContent>
m_spEventLogger = 0;
m_spMessage = 0;
m_OldSend = FALSE;
m_Inputs.bSend = TRUE;
}
- 10. In addition, carry out the following initialization in the method SetObjStateSO():
// TODO: Add any additional initialization
m_spEventLogger.SetOID(OID_TCEVENTLOGGER);
hr = FAILED(hr) ? hr : m_spSrv->TcQuerySmartObjectInterface(m_spEventLogger);
hr = FAILED(hr) ? hr : m_spEventLogger->CreateMessage(TcEvents::TutorialClass::EventClass, TcEvents::TutorialClass::TutorialEvent.nEventId, TcEvents::TutorialClass::TutorialEvent.eSeverity, 0, &m_spMessage);
- 11. If the call of the method AddModuleToCaller() fails, carry out a deinitialization:
// Cleanup if transition failed at some stage
if ( FAILED(hr) )
{
RemoveModuleFromCaller();
m_spEventLogger = NULL;
m_spMessage = NULL;
}
- 12. Carry out a deinitialization in the method SetObjStateOS():
// TODO: Add any additional deinitialization
m_spEventLogger = NULL;
m_spMessage = NULL;
- 13. Extend the cyclic code of the module by the sending of the message:
// TODO: Replace the sample with your cyclic code
if (m_Inputs.bSend && ! m_OldSend) // raising edge
{
m_spMessage->Send(0);
}
m_OldSend = m_Inputs.bSend;
- 14. Create a module instance and link it with a task.
- 15. Create the C++ project and start TwinCAT.
- The result is shown in the LoggedEvents window in the TwinCAT 3 Engineering.