view kk-data-provider.c @ 99:dad4414051c4

Add 'KK_FX80E.dll' to the distributed files
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 26 Mar 2013 18:23:29 +0100
parents 4a11331eacbf
children 4f1f353e84f5
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 <inifile.h>

#include "data-provider.h"
#include "KKFX80E.h"

#define CONFIGFILE "FXAnalise.ini"
#define DEFAULTPORT "COM4:115200"

#define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)

int CVICALLBACK KKDataProvider (void *functionData)
{
	int mainThreadId;
	int rv;
	char *resp;
	struct event event;
	char port[256];
	
	/* get main thread id to post messages to it */
	mainThreadId = CmtGetMainThreadID();
	
	/* construct configuration file path */ 
	char pathname[MAX_PATHNAME_LEN];
	char project[MAX_PATHNAME_LEN];
	GetProjectDir(project); 
	MakePathname(project, CONFIGFILE, pathname);

	/* load configuration file */ 
	IniText configuration = Ini_New(TRUE);
	Ini_ReadFromFile(configuration, pathname);

	/* get serial port name configuration */
	rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
	if (rv == 0)
		strncpy(port, DEFAULTPORT, sizeof(port));
	
	/* free */ 
	Ini_Dispose(configuration);

	/* initialize library */
	FX_Init();
	/* connect to KK FX80E counter */
	rv = FX_Open(port);
	
	/* 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 (strneq(resp, "2900;", 5)) {
			
			/* 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 (strneq(resp, "7000;", 5)) {
			/* ignore heart beat packet */
		} else if (strneq(resp, "7015;", 5)) {
			/* ignore undocumented packet. it is sent by newer kk counters
			   for each data packet. it contains a sample count along with
			   other information */
		} else {
			/* send message to the main thread */
			SendMessage(mainThreadId, resp);
		}
	}
	
	/* close serial port */
	FX_Close();
	/* free allocated resources */
	FX_Free();
	
	return 0;
}