changeset 194:4b74b81e0c79

Fix KK data provider. Wait for command reply before sending the next one.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:32 +0200
parents 3427013e4f70
children d841dfb30b0b
files kk-data-provider.c
diffstat 1 files changed, 43 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/kk-data-provider.c	Mon Mar 31 17:03:32 2014 +0200
+++ b/kk-data-provider.c	Mon Mar 31 17:03:32 2014 +0200
@@ -13,14 +13,43 @@
 #define DEFAULT_PORT "COM4:115200"
 #define DEFAULT_NCHAN 4
 #define TIMEOUT 1 /* seconds */
+#define MAXRETRY 20
 
 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)
 
+
+static inline int FX_Command(char cmd)
+{
+	char *resp;
+	int rv, retry = 0;
+	
+	/* reset counter buffer */
+	cmd = cmd + 0x80;
+	
+	/* send command */
+	rv = FX_Send(cmd);
+	if (! rv)
+		return rv;
+	
+	/* wait successfull reply */
+	do {
+		rv = FX_Recv(&resp, TIMEOUT);
+		if (! rv)
+			return rv;
+		if (retry++ > MAXRETRY)
+			return 0;
+	} while (strneq(resp, "7020", 4));
+	
+	return rv;
+}
+
+
 int CVICALLBACK KKDataProvider (void *functionData)
 {
 	int mainThreadId;
 	int rv;
-	char *cmd, *resp;
+	char cmd;
+	char *resp;
 	struct event event;
 	int nchan;
 	int scrambler;
@@ -64,100 +93,54 @@
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
-	
-	/* clear transmit buffer */
-	rv = FX_Send("\x80");
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	
+
 	/* get counter hardware version string */
-	rv = FX_Send("\x81");
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
+	rv = FX_Send(0x81);
 	if (! rv) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
 	/* wait for response */
-	while (strneq(resp, "7000", 4)) {
+	do {
 		rv = FX_Recv(&resp, TIMEOUT);
 		if (! rv) {
 			SendMessage(mainThreadId, FX_Error());
 			goto error;
 		}
-	}
-	if (strneq(resp, "7001", 4))
-		SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
+	} while (! strneq(resp, "7001", 4));
+	SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
 	
 	/* set report interval 1sec */
-	rv = FX_Send("\x29");
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
+	rv = FX_Command(0x29);
 	if (! rv) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
 	
 	/* read nchan channels */
-	cmd = "\x30";
-	cmd[0] += nchan;
-	rv = FX_Send(cmd);
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
+	cmd = 0x30 + nchan;
+	rv = FX_Command(cmd);
 	if (! rv) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
 	
 	/* set mode to instantaneous frequency measurement */
-	rv = FX_Send("\x42");
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
+	rv = FX_Command(0x42);
 	if (! rv) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
-	
+
 	/* scrambler */
-	cmd = "\x50";
-	cmd[0] += scrambler;
-	rv = FX_Send(cmd);
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
+	rv = FX_Command(0x50 + scrambler);
 	if (! rv) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
 	
 	/* enable synchronization */
-	rv = FX_Send("\x0F");
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	rv = FX_Recv(&resp, TIMEOUT);
+	rv = FX_Command(0x0F);
 	if (! rv) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
@@ -182,7 +165,7 @@
 				&event.data[0], &event.data[1], &event.data[2], &event.data[3],
 				&event.data[4], &event.data[5], &event.data[6], &event.data[7]);
 			if (rv != nchan) {
-				SendMessage(mainThreadId, "KK Counter: data conversion error");
+				SendMessage(mainThreadId, "KK Counter: data conversion error: %d != %d", rv, nchan);
 				goto error;
 			}