diff kk-data-provider.c @ 204:d7f91b9fb515

Fix KK counter setup
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:38 +0200
parents 040658369850
children ec81395bf08d
line wrap: on
line diff
--- a/kk-data-provider.c	Mon Mar 31 17:03:38 2014 +0200
+++ b/kk-data-provider.c	Mon Mar 31 17:03:38 2014 +0200
@@ -13,34 +13,34 @@
 #define DEFAULT_PORT "COM4:115200"
 #define DEFAULT_NCHAN 4
 #define TIMEOUT 1 /* seconds */
-#define MAXRETRY 20
+#define MAXRETRY 100
 
 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)
 
 
-static inline int FX_Command(char cmd)
+static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value)
 {
 	char *resp;
-	int rv, retry = 0;
-	
-	/* reset counter buffer */
-	cmd = cmd + 0x80;
+	int rv, retry;
+	unsigned int header;
 	
 	/* send command */
 	rv = FX_Send(cmd);
 	if (! rv)
-		return rv;
+		return NULL;
 	
 	/* wait successfull reply */
-	do {
+	for (retry = 0; retry < MAXRETRY; retry++) {
 		rv = FX_Recv(&resp, TIMEOUT);
 		if (! rv)
-			return rv;
-		if (retry++ > MAXRETRY)
-			return 0;
-	} while (! strneq(resp, "7015", 4));
+			return NULL;
+		header = strtoul(resp, NULL, 16);
+		if ((header & mask) == value)
+			return resp;
+	}
 	
-	return rv;
+	/* max retry reached */
+	return NULL;
 }
 
 
@@ -48,11 +48,9 @@
 {
 	int mainThreadId;
 	int rv;
-	char cmd;
 	char *resp;
 	struct event event;
 	int nchan;
-	int scrambler;
 	char port[256];
 	
 	/* get main thread id to post messages to it */
@@ -76,11 +74,6 @@
 	if (rv < 1)
 		nchan = DEFAULT_NCHAN;
 	
-	/* enable scrambler */
-	rv = Ini_GetInt(configuration, "KK", "scrambler", &scrambler);
-	if (rv < 1)
-		scrambler = 0;
-	
 	/* free */ 
 	Ini_Dispose(configuration);
 
@@ -95,53 +88,44 @@
 	}
 
 	/* get counter hardware version string */
-	rv = FX_Send(0x81);
-	if (! rv) {
+	resp = FX_Command(0x81, 0xFFFF, 0x7001);
+	if (! resp) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
-	/* wait for response */
-	do {
-		rv = FX_Recv(&resp, TIMEOUT);
-		if (! rv) {
-			SendMessage(mainThreadId, FX_Error());
-			goto error;
-		}
-	} while (! strneq(resp, "7001", 4));
 	SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
 	
 	/* set report interval 1sec */
-	rv = FX_Command(0x29);
-	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
-		goto error;
-	}
-	
-	/* read nchan channels */
-	cmd = 0x30 + nchan;
-	rv = FX_Command(cmd);
-	if (! rv) {
+	resp = FX_Command(0x29, 0x0F00, 0x0900);
+	if (! resp) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
 	
 	/* set mode to instantaneous frequency measurement */
-	rv = FX_Command(0x42);
-	if (! rv) {
+	resp = FX_Command(0x42, 0x6F00, 0x2900);
+	if (! resp) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
 
-	/* scrambler */
-	rv = FX_Command(0x50 + scrambler);
-	if (! rv) {
+	/* disable scrambler */
+	resp = FX_Command(0x50, 0x6FFF, 0x2900);
+	if (! resp) {
+ 		SendMessage(mainThreadId, FX_Error());
+ 		goto error;
+ 	}
+	
+	/* read nchan channels */
+	resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900);
+	if (! resp) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
-	
+
 	/* enable synchronization */
-	rv = FX_Command(0x0F);
-	if (! rv) {
+	resp = FX_Command(0x0F, 0x00, 0x00);
+	if (! resp) {
 		SendMessage(mainThreadId, FX_Error());
 		goto error;
 	}
@@ -154,8 +138,8 @@
 			break;
 		}
 		
-		/* 2900 with scrambler off or 2910 with scrambler on */
-		if (strneq(resp, "29", 2)) {
+		/* data packets */
+		if (strneq(resp, "2900", 2)) {
 			
 			/* timestamp */
 			gettimeofday(&event.time, NULL);