view kk-data-provider.c @ 197:79db2c87d1b0

New build. Do not install configuration file.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:37 +0200
parents 4b74b81e0c79
children 040658369850
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 "config.h"
#include "data-provider.h"
#include "KKFX80E.h"

#define DEFAULT_PORT "COM4:115200"
#define DEFAULT_NCHAN 4
#define TIMEOUT 1 /* seconds */
#define MAXRETRY 20

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


static inline int FX_Command(char cmd)
{
	char *resp;
	int rv, retry = 0;
	
	/* reset counter buffer */
	cmd = cmd + 0x80;
	
	/* send command */
	rv = FX_Send(cmd);
	if (! rv)
		return rv;
	
	/* wait successfull reply */
	do {
		rv = FX_Recv(&resp, TIMEOUT);
		if (! rv)
			return rv;
		if (retry++ > MAXRETRY)
			return 0;
	} while (strneq(resp, "7020", 4));
	
	return rv;
}


int CVICALLBACK KKDataProvider (void *functionData)
{
	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 */
	mainThreadId = CmtGetMainThreadID();
	
	/* configuration file path */ 
	char path[MAX_PATHNAME_LEN];
	GetIniFilePath(path); 

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

	/* get serial port name configuration */
	rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
	if (rv < 1)
		strncpy(port, DEFAULT_PORT, sizeof(port));
	
	/* channel number */
	rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
	if (rv < 1)
		nchan = DEFAULT_NCHAN;
	
	/* enable scrambler */
	rv = Ini_GetInt(configuration, "KK", "scrambler", &scrambler);
	if (rv < 1)
		scrambler = 0;
	
	/* free */ 
	Ini_Dispose(configuration);

	/* initialize library */
	FX_Init();
	
	/* connect to KK FX80E counter */
	rv = FX_Open(port);
	if (! rv) {
		SendMessage(mainThreadId, FX_Error());
		goto error;
	}

	/* get counter hardware version string */
	rv = FX_Send(0x81);
	if (! rv) {
		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) {
		SendMessage(mainThreadId, FX_Error());
		goto error;
	}
	
	/* set mode to instantaneous frequency measurement */
	rv = FX_Command(0x42);
	if (! rv) {
		SendMessage(mainThreadId, FX_Error());
		goto error;
	}

	/* scrambler */
	rv = FX_Command(0x50 + scrambler);
	if (! rv) {
		SendMessage(mainThreadId, FX_Error());
		goto error;
	}
	
	/* enable synchronization */
	rv = FX_Command(0x0F);
	if (! rv) {
		SendMessage(mainThreadId, FX_Error());
		goto error;
	}
	
	while (acquiring) {
		/* receive data from counter */
		FX_Recv(&resp, TIMEOUT);
		if (! resp) {
			SendMessage(mainThreadId, FX_Error());
			break;
		}
		
		/* 2900 with scrambler off or 2910 with scrambler on */
		if (strneq(resp, "29", 2)) {
			
			/* timestamp */
			gettimeofday(&event.time, NULL);
			
			/* parse received data */
			rv = Scan(resp + 6, "%f; %f; %f; %f; %f; %f; %f; %f",
				&event.data[0], &event.data[1], &event.data[2], &event.data[3],
				&event.data[4], &event.data[5], &event.data[6], &event.data[7]);
			if (rv != nchan) {
				SendMessage(mainThreadId, "KK Counter: data conversion error: %d != %d", rv, nchan);
				goto error;
			}
			
			/* convert from kHz to Hz */
			for (int i = 0; i < nchan; i++)
				event.data[i] *= 1000.0;
			
			/* push data into the data queue */
			CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
		
		} else if (strneq(resp, "7000", 4)) {
			/* ignore heart beat packet */
		} else if (strneq(resp, "7015", 4)) {
			/* ignore undocumented packet. it is sent by newer kk counters
			   for each data packet. it probably contains a sample count along
			   with some other information */
		} else if (strneq(resp, "7020", 4)) {
			/* undocumented packet. it probably reports the header for
			   subsequent data packets */
			SendMessage(mainThreadId, "KK Counter packet header: %s", resp + 7);
		} else if (strneq(resp, "7F51", 4)) {
			/* measurement interval synchronized */
			SendMessage(mainThreadId, "KK Counter measurement interval synchronized");
		} else {
			/* send message to the main thread */
			SendMessage(mainThreadId, "KK Counter: %s", resp);
		}
	}
	
error:
	/* close serial port */
	FX_Close();
	/* free allocated resources */
	FX_Free();
	
	return 0;
}