comparison kk-data-provider.c @ 89:c9e4f63c2033

Implement data acquisition through direct communication with the KK counter
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 21 Mar 2013 19:00:22 +0100
parents
children 4102fe614df2
comparison
equal deleted inserted replaced
88:9b7588cd4013 89:c9e4f63c2033
1 /* FXAnalise data provider which directly interfaces with the KK FX80E counter */
2
3 #include <ansi_c.h>
4 #include <userint.h>
5 #include <formatio.h>
6 #include <utility.h>
7 #include "KKFX80E.h"
8
9 #define SERIAL "COM1:115200"
10
11 /* data acquisition flag */
12 extern int acquiring;
13 /* data queue */
14 extern CmtTSQHandle dataQueue;
15 /* callback receiving messages in the main thread */
16 void CVICALLBACK MessageCB (void *callbackData);
17 /* message */
18 char message[1024];
19
20 int CVICALLBACK KKDataProvider (void *functionData)
21 {
22 int mainThreadId;
23 char *resp;
24 double data[5];
25
26 /* get main thread id to post messages to it */
27 mainThreadId = CmtGetMainThreadID();
28
29 /* initialize library */
30 FX_Init();
31 /* connect to KK FX80E counter */
32 FX_Open(SERIAL);
33
34 /* clear transmit buffer */
35 FX_Send("\x80");
36 FX_Recv(&resp);
37 /* set report interval 1sec */
38 FX_Send("\x29");
39 FX_Recv(&resp);
40 /* read 4 channels */
41 FX_Send("\x34");
42 FX_Recv(&resp);
43 /* set mode to instantaneous frequency measurement */
44 FX_Send("\x42");
45 FX_Recv(&resp);
46 /* switch scrambler off */
47 FX_Send("\x50");
48 FX_Recv(&resp);
49
50 while (acquiring) {
51 /* receive data from counter */
52 FX_Recv(&resp);
53
54 if (strncmp(resp, "2900;", 5) == 0) {
55
56 /* timestamp */
57 GetCurrentDateTime(&data[0]);
58
59 /* parse received data */
60 Scan(resp, "2900; %f; %f; %f; %f", &data[1], &data[2], &data[3], &data[4]);
61
62 /* convert from kHz to Hz */
63 data[1] = data[1] * 1000.0;
64 data[2] = data[2] * 1000.0;
65 data[3] = data[3] * 1000.0;
66 data[4] = data[4] * 1000.0;
67
68 /* push data into the data queue */
69 CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
70
71 } else if (strncmp(resp, "7000;", 5) == 0) {
72 /* ignore heart beat response */
73 } else {
74 /* send message to the main thread */
75 strncpy(message, resp, sizeof(message));
76 PostDeferredCallToThread(MessageCB, message, mainThreadId);
77 }
78 }
79
80 /* close serial port */
81 FX_Close();
82 /* free allocated resources */
83 FX_Free();
84
85 return 0;
86 }