# HG changeset patch # User Daniele Nicolodi # Date 1363888822 -3600 # Node ID c9e4f63c20339983dc00e3790307ad6e7c5d76f7 # Parent 9b7588cd401367c8109198003433a3810843e296 Implement data acquisition through direct communication with the KK counter diff -r 9b7588cd4013 -r c9e4f63c2033 FXAnalyse.c --- a/FXAnalyse.c Thu Mar 21 18:24:45 2013 +0100 +++ b/FXAnalyse.c Thu Mar 21 19:00:22 2013 +0100 @@ -34,9 +34,10 @@ // data providers int CVICALLBACK FakeDataProvider (void *functionData); int CVICALLBACK FileDataProvider (void *functionData); +int CVICALLBACK KKDataProvider (void *functionData); // select which data provider to use -#define DataProvider FileDataProvider +#define DataProvider KKDataProvider double utc; @@ -540,6 +541,13 @@ } +void CVICALLBACK MessageCB (void *msg) +{ + if (msg != NULL) + logmsg(msg); +} + + void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int event, int value, void *callbackData) { diff -r 9b7588cd4013 -r c9e4f63c2033 FXAnalyse.prj --- a/FXAnalyse.prj Thu Mar 21 18:24:45 2013 +0100 +++ b/FXAnalyse.prj Thu Mar 21 19:00:22 2013 +0100 @@ -11,7 +11,7 @@ VXIplug&play Framework Dir = "/C/Program Files/IVI Foundation/VISA/winnt" IVI Standard Root 64-bit Dir = "/C/Program Files/IVI Foundation/IVI" VXIplug&play Framework 64-bit Dir = "/C/Program Files/IVI Foundation/VISA/win64" -Number of Files = 23 +Number of Files = 27 Target Type = "Executable" Flags = 16 Copied From Locked InstrDrv Directory = False @@ -116,6 +116,30 @@ Res Id = 9 Path Is Rel = True Path Rel To = "Project" +Path Rel Path = "fake-data-provider.c" +Path = "/p/nicolodi/FXAnalyse/fake-data-provider.c" +Exclude = False +Compile Into Object File = False +Project Flags = 0 +Folder = "Source Files" + +[File 0010] +File Type = "CSource" +Res Id = 10 +Path Is Rel = True +Path Rel To = "Project" +Path Rel Path = "file-data-provider.c" +Path = "/p/nicolodi/FXAnalyse/file-data-provider.c" +Exclude = False +Compile Into Object File = False +Project Flags = 0 +Folder = "Source Files" + +[File 0011] +File Type = "CSource" +Res Id = 11 +Path Is Rel = True +Path Rel To = "Project" Path Rel Path = "future.c" Path = "/p/nicolodi/FXAnalyse/future.c" Exclude = False @@ -123,9 +147,9 @@ Project Flags = 0 Folder = "Source Files" -[File 0010] +[File 0012] File Type = "Include" -Res Id = 10 +Res Id = 12 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "future.h" @@ -134,9 +158,9 @@ Project Flags = 0 Folder = "Include Files" -[File 0011] +[File 0013] File Type = "Include" -Res Id = 11 +Res Id = 13 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "FXAllan.h" @@ -145,9 +169,9 @@ Project Flags = 0 Folder = "Include Files" -[File 0012] +[File 0014] File Type = "User Interface Resource" -Res Id = 12 +Res Id = 14 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "FXAllan.uir" @@ -156,9 +180,9 @@ Project Flags = 0 Folder = "User Interface Files" -[File 0013] +[File 0015] File Type = "CSource" -Res Id = 13 +Res Id = 15 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "FXAnalyse.c" @@ -168,35 +192,13 @@ Project Flags = 0 Folder = "Source Files" -[File 0014] -File Type = "Include" -Res Id = 14 -Path Is Rel = True -Path Rel To = "Project" -Path Rel Path = "FXAnalyse.h" -Path = "/p/nicolodi/FXAnalyse/FXAnalyse.h" -Exclude = False -Project Flags = 0 -Folder = "Include Files" - -[File 0015] -File Type = "User Interface Resource" -Res Id = 15 -Path Is Rel = True -Path Rel To = "Project" -Path Rel Path = "FXAnalyse.uir" -Path = "/p/nicolodi/FXAnalyse/FXAnalyse.uir" -Exclude = False -Project Flags = 0 -Folder = "User Interface Files" - [File 0016] File Type = "Include" Res Id = 16 Path Is Rel = True Path Rel To = "Project" -Path Rel Path = "FXPlot.h" -Path = "/p/nicolodi/FXAnalyse/FXPlot.h" +Path Rel Path = "FXAnalyse.h" +Path = "/p/nicolodi/FXAnalyse/FXAnalyse.h" Exclude = False Project Flags = 0 Folder = "Include Files" @@ -206,15 +208,61 @@ Res Id = 17 Path Is Rel = True Path Rel To = "Project" +Path Rel Path = "FXAnalyse.uir" +Path = "/p/nicolodi/FXAnalyse/FXAnalyse.uir" +Exclude = False +Project Flags = 0 +Folder = "User Interface Files" + +[File 0018] +File Type = "Include" +Res Id = 18 +Path Is Rel = True +Path Rel To = "Project" +Path Rel Path = "FXPlot.h" +Path = "/p/nicolodi/FXAnalyse/FXPlot.h" +Exclude = False +Project Flags = 0 +Folder = "Include Files" + +[File 0019] +File Type = "User Interface Resource" +Res Id = 19 +Path Is Rel = True +Path Rel To = "Project" Path Rel Path = "FXPlot.uir" Path = "/p/nicolodi/FXAnalyse/FXPlot.uir" Exclude = False Project Flags = 0 Folder = "User Interface Files" -[File 0018] +[File 0020] +File Type = "CSource" +Res Id = 20 +Path Is Rel = True +Path Rel To = "Project" +Path Rel Path = "kk-data-provider.c" +Path = "/p/nicolodi/FXAnalyse/kk-data-provider.c" +Exclude = False +Compile Into Object File = False +Project Flags = 0 +Folder = "Source Files" + +[File 0021] +File Type = "CSource" +Res Id = 21 +Path Is Rel = True +Path Rel To = "Project" +Path Rel Path = "KKFX80E.c" +Path = "/p/nicolodi/FXAnalyse/KKFX80E.c" +Exclude = False +Compile Into Object File = False +Project Flags = 0 +Folder = "Source Files" + +[File 0022] File Type = "Library" -Res Id = 18 +Res Id = 22 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "muParser.lib" @@ -223,9 +271,9 @@ Project Flags = 0 Folder = "Librairy Files" -[File 0019] +[File 0023] File Type = "Include" -Res Id = 19 +Res Id = 23 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "muParserDLL.h" @@ -234,9 +282,9 @@ Project Flags = 0 Folder = "Include Files" -[File 0020] +[File 0024] File Type = "CSource" -Res Id = 20 +Res Id = 24 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "Plot.c" @@ -246,9 +294,9 @@ Project Flags = 0 Folder = "Source Files" -[File 0021] +[File 0025] File Type = "Include" -Res Id = 21 +Res Id = 25 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "Plot.h" @@ -257,9 +305,9 @@ Project Flags = 0 Folder = "Include Files" -[File 0022] +[File 0026] File Type = "CSource" -Res Id = 22 +Res Id = 26 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "YLCStuff.c" @@ -269,9 +317,9 @@ Project Flags = 0 Folder = "Source Files" -[File 0023] +[File 0027] File Type = "Include" -Res Id = 23 +Res Id = 27 Path Is Rel = True Path Rel To = "Project" Path Rel Path = "YLCStuff.h" diff -r 9b7588cd4013 -r c9e4f63c2033 KKFX80E.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KKFX80E.c Thu Mar 21 19:00:22 2013 +0100 @@ -0,0 +1,110 @@ +#include +#include + +int (_stdcall *FX_OpenPort)(char **Port) = NULL; +void (_stdcall *FX_ClosePort)(void) = NULL; +int (_stdcall *FX_SendCommand)(char **Command) = NULL; +int (_stdcall *FX_GetReport)(char **Data) = NULL; +void (_stdcall *FX_Debug)(BOOL DbgFlag) = NULL; +// int (_stdcall *GetFXM)(char **Data) = NULL; +// void (_stdcall *Log)(int DbgNum) = NULL; +// int (_stdcall *ReadMonitor)(char **Data) = NULL; +// BOOL (_stdcall *EnumeratePorts)(char **PortName, char *PortNamePrefix) = NULL; + +static HMODULE module; + + +int FX_Init(void) +{ + FARPROC funcp; + + module = LoadLibrary("KK_FX80E.dll"); + if (! module) + return 0; + + funcp = GetProcAddress(module, "FX_OpenPort"); + if (! funcp) + goto error; + FX_OpenPort = (void *)funcp; + + funcp = GetProcAddress(module, "FX_ClosePort"); + if (! funcp) + goto error; + FX_ClosePort = (void *)funcp; + + funcp = GetProcAddress(module, "FX_SendCommand"); + if (! funcp) + goto error; + FX_SendCommand = (void *)funcp; + + funcp = GetProcAddress(module, "FX_GetReport"); + if (! funcp) + goto error; + FX_GetReport = (void *)funcp; + + funcp = GetProcAddress(module, "FX_Debug"); + if (! funcp) + goto error; + FX_Debug = (void *)funcp; + + return 1; + +error: + FreeLibrary(module); + return 0; +} + + +void FX_Free(void) +{ + FreeLibrary(module); +} + + +int FX_Open(char *port) +{ + int rv = FX_OpenPort(&port); +#ifdef DEBUG + if (! rv) + fprintf(stderr, "%s\n", *port); +#endif + return rv; +} + + +void FX_Close(void) +{ + FX_ClosePort(); +} + + +int FX_Send(char *cmd) +{ + int rv = FX_SendCommand(&cmd); +#ifdef DEBUG + if (! rv) + fprintf(stderr, "%s\n", cmd); +#endif + return rv; +} + + +char * FX_Recv(char **ret) +{ + int rv; + char *data; + do { + data = "."; + rv = FX_GetReport(&data); + } while (! data); +#ifdef DEBUG + if (! rv) + fprintf(stderr, "%s\n", data); +#endif + if (! rv) + return NULL; + if (ret) + *ret = data; + + return data; +} diff -r 9b7588cd4013 -r c9e4f63c2033 KKFX80E.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KKFX80E.h Thu Mar 21 19:00:22 2013 +0100 @@ -0,0 +1,25 @@ +#ifndef __KKFX80E_H__ +#define __KKFX80E_H__ + +#ifdef __cplusplus + extern "C" { +#endif + +int FX_Init(void); +void FX_Free(void); +int FX_OpenPort(char **port); +void FX_ClosePort(void); +int FX_SendCommand(char **cmd); +int FX_GetReport(char **sep); +void FX_Debug(BOOL value); + +int FX_Open(char *port); +void FX_Close(void); +int FX_Send(char *cmd); +char * FX_Recv(char **data); + +#ifdef __cplusplus + } +#endif + +#endif /* ndef __KKFX80E_H__ */ diff -r 9b7588cd4013 -r c9e4f63c2033 KK_FX80E.dll Binary file KK_FX80E.dll has changed diff -r 9b7588cd4013 -r c9e4f63c2033 kk-data-provider.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kk-data-provider.c Thu Mar 21 19:00:22 2013 +0100 @@ -0,0 +1,86 @@ +/* FXAnalise data provider which directly interfaces with the KK FX80E counter */ + +#include +#include +#include +#include +#include "KKFX80E.h" + +#define SERIAL "COM1:115200" + +/* data acquisition flag */ +extern int acquiring; +/* data queue */ +extern CmtTSQHandle dataQueue; +/* callback receiving messages in the main thread */ +void CVICALLBACK MessageCB (void *callbackData); +/* message */ +char message[1024]; + +int CVICALLBACK KKDataProvider (void *functionData) +{ + int mainThreadId; + char *resp; + double data[5]; + + /* 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 */ + GetCurrentDateTime(&data[0]); + + /* parse received data */ + Scan(resp, "2900; %f; %f; %f; %f", &data[1], &data[2], &data[3], &data[4]); + + /* convert from kHz to Hz */ + data[1] = data[1] * 1000.0; + data[2] = data[2] * 1000.0; + data[3] = data[3] * 1000.0; + data[4] = data[4] * 1000.0; + + /* push data into the data queue */ + CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0); + + } else if (strncmp(resp, "7000;", 5) == 0) { + /* ignore heart beat response */ + } else { + /* send message to the main thread */ + strncpy(message, resp, sizeof(message)); + PostDeferredCallToThread(MessageCB, message, mainThreadId); + } + } + + /* close serial port */ + FX_Close(); + /* free allocated resources */ + FX_Free(); + + return 0; +}