How can I figure out in the MessageWritten event handler, which controller threw the event? Below is shown briefly (in C#), how I have tried to do that.
Any help would be much appreciated!
controller1.EventLog.MessageWritten +=
new EventHandler(MsgWritten);
controller2.EventLog.MessageWritten +=
new EventHandler(MsgWritten);
…
private void MsgWritten(object sender, MessageWrittenEventArgs e)
{
// Trial 1:
EventLog evlog = (EventLog)sender;
if (controller1.EventLog == evlog) { // never goes here }
if (controller2.EventLog == evlog) { // always goes here! }
// Trial 2:
// EventLog messages are iterated and e.Message is searched from
// there using Timestamp and SequenceNumber fields.
// For simplicity, all code is not shown, but here the same occurs:
// message is found always from controller2.
bool bFound1 = FindMessage(e.Message, controller1.EventLog); // never here
bool bFound2 = FindMessage(e.Message, controller2.EventLog); // always here!
if you are running on a FP SDK, they are the same Controller.
if you are running on a PC SDK, the best alternative is to connect each Event log to a different event handler, and then make a call to another routine that handles the message for your appliaction. This is some pseudo-code as a work around:
controller1.EventLog.MessageWritten += new EventHandler(MsgWritten_1);
controller2.EventLog.MessageWritten += new EventHandler(MsgWritten)2);
No, it is not fixed currently. The event handler get as parameter the “object sender” (which is an “EventLog”); this variable contains controller information, but this is declared as private and thus not usable in your own code.
That is OK, but reflection is slow - by its very nature and if connected to 100’s of controllers it is not great. Would be better if ABB made the controller available
Have you tried with a lambda expression .. (pseudo code)
controller1.EventLog.MessageWritten += (o, e) =>
{
// Here you can call your method, and provide the instance of the controller (since you have access to it at this point). You can also pass the EvengLog (o) if you need it
MyEventLog_MessageWritten(controller1, e);
};
private void My MyEventLog_MessageWritten(Controller theController, ABB.Robotics.Controllers.EventLogDomain.MessageWrittenEventArgs e)
{