Mercurial > hg > fxanalyse
view kk-data-provider.c @ 205:31093786d41d
Fix Sr beatnote sign detection
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 31 Mar 2014 17:03:38 +0200 |
parents | d7f91b9fb515 |
children | ec81395bf08d |
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 100 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value) { char *resp; int rv, retry; unsigned int header; /* send command */ rv = FX_Send(cmd); if (! rv) return NULL; /* wait successfull reply */ for (retry = 0; retry < MAXRETRY; retry++) { rv = FX_Recv(&resp, TIMEOUT); if (! rv) return NULL; header = strtoul(resp, NULL, 16); if ((header & mask) == value) return resp; } /* max retry reached */ return NULL; } int CVICALLBACK KKDataProvider (void *functionData) { int mainThreadId; int rv; char *resp; struct event event; int nchan; 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; /* 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 */ resp = FX_Command(0x81, 0xFFFF, 0x7001); if (! resp) { SendMessage(mainThreadId, FX_Error()); goto error; } SendMessage(mainThreadId, "KK Counter version: %s", resp + 4); /* set report interval 1sec */ resp = FX_Command(0x29, 0x0F00, 0x0900); if (! resp) { SendMessage(mainThreadId, FX_Error()); goto error; } /* set mode to instantaneous frequency measurement */ resp = FX_Command(0x42, 0x6F00, 0x2900); if (! resp) { SendMessage(mainThreadId, FX_Error()); goto error; } /* disable scrambler */ resp = FX_Command(0x50, 0x6FFF, 0x2900); if (! resp) { SendMessage(mainThreadId, FX_Error()); goto error; } /* read nchan channels */ resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900); if (! resp) { SendMessage(mainThreadId, FX_Error()); goto error; } /* enable synchronization */ resp = FX_Command(0x0F, 0x00, 0x00); if (! resp) { SendMessage(mainThreadId, FX_Error()); goto error; } while (acquiring) { /* receive data from counter */ FX_Recv(&resp, TIMEOUT); if (! resp) { SendMessage(mainThreadId, FX_Error()); break; } /* data packets */ if (strneq(resp, "2900", 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; }