annotate kk-data-provider.c @ 94:b7ae2d947617

Change default KK port to COM4. Make port configurable via .ini file
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 22 Mar 2013 18:46:13 +0100
parents 4102fe614df2
children 4a11331eacbf
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
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 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
16 {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17 int mainThreadId;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
18 char *resp;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
19 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
20 char port[256];
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
22 /* 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
23 mainThreadId = CmtGetMainThreadID();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
25 /* path for the 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
26 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
27 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
28 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
29 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
30
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
31 /* 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
32 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
33 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
34
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
35 /* get serial port name configuration */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
36 int rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, 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
37 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
38 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
39
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
40 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
41 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
42
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44 FX_Init();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45 /* connect to KK FX80E counter */
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
46 FX_Open(port);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
48 /* clear transmit buffer */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 FX_Send("\x80");
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51 /* set report interval 1sec */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 FX_Send("\x29");
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 /* read 4 channels */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 FX_Send("\x34");
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 /* 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
58 FX_Send("\x42");
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 /* switch scrambler off */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 FX_Send("\x50");
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
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 /* receive data from counter */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66 FX_Recv(&resp);
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
67
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68 if (strncmp(resp, "2900;", 5) == 0) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
71 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
72
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 /* parse received data */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
74 Scan(resp, "2900; %f; %f; %f; %f",
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
75 &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
76
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77 /* convert from kHz to Hz */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
78 event.data[0] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
79 event.data[1] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
80 event.data[2] *= 1000.0;
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
81 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
82
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
83 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
84 CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 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 } else if (strncmp(resp, "7000;", 5) == 0) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87 /* ignore heart beat response */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88 } else {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
89 /* send message to the main thread */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
90 SendMessage(mainThreadId, resp);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
91 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
92 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
93
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
94 /* close serial port */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
95 FX_Close();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
96 /* free allocated resources */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
97 FX_Free();
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 return 0;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
100 }