TC3:FB_TcAlarm多实例创建异常问题咨询
Hey Adam, let's break down what's causing your alarm creation issue and get those per-device alarms working properly.
The Root Cause
When you call CreateEx with the same dwInstanceID (you're passing 0 for all iterations) and the same EventClass trigger (TC_Events.BankPressureAlarm.Trigger), you're trying to create multiple FB_TcAlarm instances tied to the same underlying event instance. The TC3 Event Logger only allows one instance per EventClass+InstanceID pair, so the first call succeeds, and every subsequent call throws ERROR_ALREADY_EXISTS (16#B7). That's also why all your TcSourceInfo.nId values are identical—they're pointing to the single event instance that was actually created.
The Fix: Use Unique Instance IDs
To create separate alarm instances for each device, you need to pass a unique dwInstanceID for each FB_TcAlarm in your array. The loop counter (nLoopCtr) is a perfect candidate for this, since it's unique for each device. Here's how to modify your code:
// Bank Global Pressure Limit Alarms FOR nLoopCtr := 1 TO nNumBanks BY 1 DO // Pass unique InstanceID using loop counter arrFbBankPressHresult[nLoopCtr] := arrFbBankPressAlm[nLoopCtr].CreateEx( TC_Events.BankPressureAlarm.Trigger, FALSE, nLoopCtr // Unique ID for each bank's alarm instance ); // Optional: Set a human-readable source name for HMI differentiation IF arrFbBankPressHresult[nLoopCtr] = S_OK THEN arrFbBankPressAlm[nLoopCtr].IpArguments.szSourceName := CONCAT('Bank_', INT_TO_STRING(nLoopCtr)); // You can also set custom parameters here (dwParam1, szParam1, etc.) // arrFbBankPressAlm[nLoopCtr].IpArguments.dwParam1 := nLoopCtr; END_IF END_FOR
Key Notes for HMI Differentiation
- In your HMI EventGrid, add columns for
szSourceNameor the custom parameters you set (likedwParam1). This will let operators immediately see which bank triggered the alarm. - When you need to trigger a specific bank's alarm later, just call
arrFbBankPressAlm[<BankNumber>].Raise()—each instance is independent and tied to its own unique event.
Quick Checks to Ensure Success
- Verify your EventClass (
BankPressureAlarm) in the TwinCAT Type System isn't set to "Single Instance" (this is disabled by default, but it's worth confirming). - Make sure your
arrFbBankPressAlmarray is declared as an array of independent FB_TcAlarm instances (not a reference to a single instance).
Give this a shot, and you should see all your alarm instances get created successfully with unique IDs. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Adam

