事件机制
运行时系统使用“事件”来传达诸如 PLC 的启动/停止/复位、异常发生等事件。使用“事件机制”,可以在触发选定事件后立即接收消息。
转发以下与应用程序相关的事件:
EVT_StartDone
EVT_StopDone
EVT_ResetDone
EVT_ExitDone
EVT_AllBootprojectsLoaded
EVT_CmpApp_Exception
EVT_StateChanged
EVT_CmpDevice_InteractiveLogin
EVT_CmpMgr_LicenseState
交付的 uds_events.py
样品提供 CODESYS_EventHandler
实现事件注册的类。
这个类需要 eventid
以及 componentid of
要注册的事件,存储在 liEvents
列表:
liEvents = { "CmpApp_EVT_StartDone" : (0x00000002, 0x10000 + 2), "CmpApp_EVT_StopDone" : (0x00000002, 0x10000 + 4), "CmpApp_EVT_ResetDone" : (0x00000002, 0x10000 + 6), "CmpApp_EVT_ExitDone" : (0x00000002, 0x10000 + 15), "CmpApp_EVT_AllBootprojectsLoaded" : (0x00000002, 0x10000 + 25), "CmpApp_EVT_CmpApp_Exception" : (0x00000002, 0x00080000 + 28), "CmpApp_EVT_StateChanged" : (0x00000002,0x10000 + 43), "CmpDevice_EVT_CmpDevice_InteractiveLogin" : (0x0000000E, 0x00010000+ 1), "CmpMgr_EVT_LicenseState" : (0x00000001, 0x00010000 + 9) }
此外,必须指定一个回调函数,该函数将在事件发生时调用:
def callbackfunction_start(componentid, eventid): print("Start event occured")
在里面 Main
, 事件注册如下,并指定必要的回调函数:
componentid, eventid = liEvents["CmpApp_EVT_StartDone"] myEventHandler_Start = CODESYS_EventHandler(componentid, eventid, callbackfunction_start) myEventHandler_Start.start()
现在回调函数 callbackfunction_start
运行时系统更改为 Start 后立即调用。
底层机制如下图所示:

接口的使用仅作为 Linux 的成员 | 是的 |
流程分离 | 是的 |