annotate kk-data-provider.c @ 97:675cb8d5e8a7

Write raw counters data to disk
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 26 Mar 2013 18:18:38 +0100
parents 4a11331eacbf
children 4f1f353e84f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 /* FXAnalise data provider which directly interfaces with the KK FX80E counter */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
2
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
3 #include <ansi_c.h>
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4 #include <userint.h>
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5 #include <formatio.h>
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #include <utility.h>
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
7 #include <inifile.h>
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
8
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
9 #include "data-provider.h"
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
10 #include "KKFX80E.h"
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
11
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
12 #define CONFIGFILE "FXAnalise.ini"
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
13 #define DEFAULTPORT "COM4:115200"
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
15 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0)
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
16
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 int CVICALLBACK KKDataProvider (void *functionData)
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19 int mainThreadId;
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
20 int rv;
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 char *resp;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
22 struct event event;
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
23 char port[256];
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 /* get main thread id to post messages to it */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 mainThreadId = CmtGetMainThreadID();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
28 /* construct configuration file path */
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
29 char pathname[MAX_PATHNAME_LEN];
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
30 char project[MAX_PATHNAME_LEN];
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
31 GetProjectDir(project);
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
32 MakePathname(project, CONFIGFILE, pathname);
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
33
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
34 /* load configuration file */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
35 IniText configuration = Ini_New(TRUE);
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
36 Ini_ReadFromFile(configuration, pathname);
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
37
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
38 /* get serial port name configuration */
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
39 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
40 if (rv == 0)
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
41 strncpy(port, DEFAULTPORT, sizeof(port));
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
42
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
43 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
44 Ini_Dispose(configuration);
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
45
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
46 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47 FX_Init();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
48 /* connect to KK FX80E counter */
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
49 rv = FX_Open(port);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51 /* clear transmit buffer */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 FX_Send("\x80");
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54 /* set report interval 1sec */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 FX_Send("\x29");
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57 /* read 4 channels */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 FX_Send("\x34");
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 /* set mode to instantaneous frequency measurement */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 FX_Send("\x42");
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63 /* switch scrambler off */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 FX_Send("\x50");
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68 /* receive data from counter */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
71 if (strneq(resp, "2900;", 5)) {
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
74 gettimeofday(&event.time, NULL);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76 /* parse received data */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
77 Scan(resp, "2900; %f; %f; %f; %f",
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
78 &event.data[0], &event.data[1], &event.data[2], &event.data[3]);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
79
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80 /* convert from kHz to Hz */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
81 event.data[0] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
82 event.data[1] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
83 event.data[2] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
84 event.data[3] *= 1000.0;
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
85
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
86 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
87 CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0);
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
88
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
89 } else if (strneq(resp, "7000;", 5)) {
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
90 /* ignore heart beat packet */
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
91 } else if (strneq(resp, "7015;", 5)) {
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
92 /* ignore undocumented packet. it is sent by newer kk counters
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
93 for each data packet. it contains a sample count along with
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
94 other information */
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
95 } else {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96 /* send message to the main thread */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
97 SendMessage(mainThreadId, resp);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
98 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
99 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
100
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
101 /* close serial port */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
102 FX_Close();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
103 /* free allocated resources */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
104 FX_Free();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
105
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
106 return 0;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
107 }