diff kk-data-provider.c @ 89:c9e4f63c2033

Implement data acquisition through direct communication with the KK counter
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 21 Mar 2013 19:00:22 +0100
parents
children 4102fe614df2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kk-data-provider.c	Thu Mar 21 19:00:22 2013 +0100
@@ -0,0 +1,86 @@
+/* FXAnalise data provider which directly interfaces with the KK FX80E counter */
+
+#include <ansi_c.h>
+#include <userint.h>
+#include <formatio.h>
+#include <utility.h>
+#include "KKFX80E.h"
+
+#define SERIAL "COM1:115200"
+
+/* data acquisition flag */
+extern int acquiring;
+/* data queue */
+extern CmtTSQHandle dataQueue;
+/* callback receiving messages in the main thread */
+void CVICALLBACK MessageCB (void *callbackData);
+/* message */
+char message[1024];
+
+int CVICALLBACK KKDataProvider (void *functionData)
+{
+	int mainThreadId;
+	char *resp;
+	double data[5];
+	
+	/* get main thread id to post messages to it */
+	mainThreadId = CmtGetMainThreadID();
+	
+	/* initialize library */
+	FX_Init();
+	/* connect to KK FX80E counter */
+	FX_Open(SERIAL);
+	
+	/* clear transmit buffer */
+	FX_Send("\x80");
+	FX_Recv(&resp);
+	/* set report interval 1sec */
+	FX_Send("\x29");
+	FX_Recv(&resp);
+	/* read 4 channels */
+	FX_Send("\x34");
+	FX_Recv(&resp);
+	/* set mode to instantaneous frequency measurement */
+	FX_Send("\x42");
+	FX_Recv(&resp);
+	/* switch scrambler off */
+	FX_Send("\x50");
+	FX_Recv(&resp);
+	
+	while (acquiring) {
+		/* receive data from counter */
+		FX_Recv(&resp);
+		
+		if (strncmp(resp, "2900;", 5) == 0) {
+			
+			/* timestamp */
+			GetCurrentDateTime(&data[0]);
+			
+			/* parse received data */
+			Scan(resp, "2900; %f; %f; %f; %f", &data[1], &data[2], &data[3], &data[4]);
+			
+			/* convert from kHz to Hz */
+			data[1] = data[1] * 1000.0;
+			data[2] = data[2] * 1000.0;
+			data[3] = data[3] * 1000.0;
+			data[4] = data[4] * 1000.0;
+			
+			/* push data into the data queue */
+			CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
+			
+		} else if (strncmp(resp, "7000;", 5) == 0) {
+			/* ignore heart beat response */
+		} else {
+			/* send message to the main thread */
+			strncpy(message, resp, sizeof(message));
+			PostDeferredCallToThread(MessageCB, message, mainThreadId);
+		}
+	}
+	
+	/* close serial port */
+	FX_Close();
+	/* free allocated resources */
+	FX_Free();
+	
+	return 0;
+}