We are developing a simple PC application using RAB PC SDK. The process is that:
PC application sent data to Raplid program and set “start” flag
Rapid program continues to execute a process when “start” flag is true
Rapid program increases a “PERS Num” variable by 1 if this process is finished. This value is subscribed by PC application, which informs the PC application that this process is done and step 1 starts again by sending a new data.
The question is that the subscribed event will be fired twice each time. Can anybody figure out why? This may cause communication problem.
During execution, I stop robot program and manually change the value of the flag via FP data view. The value change event is still fired twice. No idea what happened.
I developed a simple PC application (webwiz/2869/ValueChangeRAB.rar) using RAB 5.11 and VS 2005 (C#). This application is developed only for investigating the issue;
The objective of this application is to
search robot in local network
connect to selected robot
send a Rapid program (rapid/GetConfig.mod) to connected robot
start the Rapid program
send points to Rapid program one by one and check its reacheability given a configuration data.
Rapid variable registration and registered event function are in file “ConfigManager.cs”. You are able to find (from the list box in GUI or log file in executable path named “runner”) that whenever the variable “nPoint” in Rapid program is changed. The registered event function “rdPoint_ValueChanged” in ConfigManager.cs will fired twice.
Interesting app for being only for testing purposes …
Quick comments so you can quickly check this:
(1) The method to register event (RegisterCheckEvents) should check if event handlers have been connected. If you call the method twice, you will connect the event handler twice; then any time there is a change in the RAPID data, you will be called as many times as you connect
(2) Notice that in the ConfigManager.cs (Line 166 & 167) you connect to the same event handler twice (one through the ValueChanged event, other through the Subscribe method).
I had thought “subscribe” just set priority level. However, it subscribe the event handler again. So I remove line “166” and only “subscribe” event handler once now while keeping other code the same. But when we request mastership to send point, we get the exception “mastership already held”. Anything wrong? Is it because of the “subscribe” function?
Another stupid question is how I can check if an event handler is connected or not.
Even though both interfaces connects internally in the same way, the Subscribe method lets you set the priority, and the ValueChanged event sets the priority to Normal only.
The event mechanism does not interfere with the Mastership. The problem is that you are requesting the mastership twice. The first one to modify the position, this generates a change and call your event handler “rdPoint_ValueChanged”. Inside this event handler you modify the point again (using SendPoint), which in fact tries to request the mastership for the second time. You can avoid this by asking if you already have the masterhip by using the property IsMaster.
There is no way to monitor if you have subscribed to a variable or not. This is usually maintained at the client side.
I modified the code a little bit to check if mastership has been held. I still have the same problem. Enclosed please find a screen shot. No idea about why. However, it does happen all the time. The program sometimes works well.
You have two threads running this code at the same time; there is a race condition here, so it seems that both are passing the if at the same time, but onyl one will get the mastership. You can prevent this by using a lock statement.