Index: trunk/FACT++/scripts/CheckUnderflow.js
===================================================================
--- trunk/FACT++/scripts/CheckUnderflow.js	(revision 15093)
+++ trunk/FACT++/scripts/CheckUnderflow.js	(revision 15094)
@@ -79,20 +79,28 @@
     //console.out("Most probablay the camera has not been checked for underflows yet.");
 
+    dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
+    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
+
     var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
     var event = service_event.get(5000, false);
 
+    // This needs a better solution! The problem is that the
+    // event builder might still be writing a file (WritingData),
+    // but even if the file is closed (CLOSE_OPEN_FILES) the
+    // thread sending data (EVENT_DATA) might still be processing
+    // events. Thus there is no real guarantee that we do not receive
+    // data of the previous run after we have started a new one.
+    dim.wait("Connected", 10000);
+    v8.sleep(1000); // Let 'timeout' in SendRawData pass
+
     var event_counter = event.counter;
 
-    dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
-    dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
+    console.out("Starting drs-gain");
+    dim.send("MCP/START", -1, 5, "drs-gain");
 
-    console.out("Starting drs-gain");
-    dim.send("MCP/START", -1, 3, "drs-gain");
+    console.out("Waiting for new event");
 
-    // FIXME: add a timeout here (10s)
-    console.out("Waiting for new event");
     var d = new Date();
-    while (event_counter == service_event.get(0, false).counter)
-        v8.sleep();
+    v8.timeout(10000, function() { if (service_event.get(0, false).counter>event_counter) return true; });
 
     console.out("Event received: "+(new Date()-d)+"ms");
@@ -163,5 +171,5 @@
     {
         if (stat0.count>8)
-            throw new Error("Underflow condition detected in about "+stat0.count/9+" DRS.");
+            throw new Error("Underflow condition detected in about "+parseInt(stat0.count/9+.5)+" DRS.");
 
         log.warn("There is probably an underflow condition in one DRS... please check manually.");
