view kk-data-provider.c @ 95:e437666ab710

New build
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 22 Mar 2013 18:55:08 +0100
parents b7ae2d947617
children 4a11331eacbf
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"

int CVICALLBACK KKDataProvider (void *functionData)
{
	int mainThreadId;
	char *resp;
	struct event event;
	char port[256];
	
	/* get main thread id to post messages to it */
	mainThreadId = CmtGetMainThreadID();
	
	/* path for the configuration file */ 
	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 */
	int 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 */
	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 (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;
}