view kk-data-provider.c @ 91:4102fe614df2

Fix timestamping. Cleanup data providers
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 22 Mar 2013 16:32:15 +0100
parents c9e4f63c2033
children b7ae2d947617
line wrap: on
line source

/* 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 "data-provider.h"
#include "KKFX80E.h"

#define SERIAL "COM1:115200"

int CVICALLBACK KKDataProvider (void *functionData)
{
	int mainThreadId;
	char *resp;
	struct event event;
	
	/* 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 */
			gettimeofday(&event.time, NULL);
			
			/* parse received data */
			Scan(resp, "2900; %f; %f; %f; %f",
				&event.data[0], &event.data[1], &event.data[2], &event.data[3]);
			
			/* convert from kHz to Hz */
			event.data[0] *= 1000.0;
			event.data[1] *= 1000.0;
			event.data[2] *= 1000.0;
			event.data[3] *= 1000.0;
			
			/* push data into the data queue */
			CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
			
		} else if (strncmp(resp, "7000;", 5) == 0) {
			/* ignore heart beat response */
		} else {
			/* send message to the main thread */
			SendMessage(mainThreadId, resp);
		}
	}
	
	/* close serial port */
	FX_Close();
	/* free allocated resources */
	FX_Free();
	
	return 0;
}