annotate kk-data-provider.c @ 177:5b4b7f37bd3b

Make number of KK counter channels read configurable
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 21 Feb 2014 18:38:44 +0100
parents 4ff3e468ab5f
children c96f5f64c946
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
163
4ff3e468ab5f Fix configuration file name
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 120
diff changeset
12 #define CONFIGFILE "FXAnalyse.ini"
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
13 #define DEFAULT_PORT "COM4:115200"
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
14 #define DEFAULT_NCHAN 4
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
15 #define TIMEOUT 1 /* seconds */
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
16
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
17 #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
18
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
19 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
20 {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
21 int mainThreadId;
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
22 int rv;
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23 char *resp;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
24 struct event event;
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
25 int nchan;
94
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 port[256];
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
27
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28 /* 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
29 mainThreadId = CmtGetMainThreadID();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
30
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
31 /* 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
32 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
33 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
34 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
35 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
36
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
37 /* 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
38 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
39 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
40
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
41 /* 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
42 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
43 if (! rv)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
44 strncpy(port, DEFAULT_PORT, sizeof(port));
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
45
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
46 /* channels number */
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
47 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
48 if (! rv)
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
49 nchan = DEFAULT_NCHAN;
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
50
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
51 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
52 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
53
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 FX_Init();
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
56
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57 /* 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
58 rv = FX_Open(port);
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
59 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
60 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
61 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
62 }
89
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 /* clear transmit buffer */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
65 rv = FX_Send("\x80");
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
66 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
67 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
68 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
69 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
70 rv = FX_Recv(&resp, TIMEOUT);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
71 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
72 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
73 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
74 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
75
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
76 /* set report interval 1sec */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
77 rv = FX_Send("\x29");
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
78 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
79 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
80 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
81 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
82 rv = FX_Recv(&resp, TIMEOUT);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
83 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
84 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
85 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
86 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
87
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
88 /* read nchan channels */
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
89 char *cmd = "\x30";
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
90 cmd[0] += nchan;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
91 rv = FX_Send(cmd);
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
92 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
93 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
94 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
95 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
96 rv = FX_Recv(&resp, TIMEOUT);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
97 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
98 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
99 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
100 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
101
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
102 /* set mode to instantaneous frequency measurement */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
103 rv = FX_Send("\x42");
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
104 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
105 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
106 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
107 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
108 rv = FX_Recv(&resp, TIMEOUT);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
109 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
110 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
111 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
112 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
113
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
114 /* switch scrambler off */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
115 rv = FX_Send("\x50");
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
116 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
117 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
118 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
119 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
120 rv = FX_Recv(&resp, TIMEOUT);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
121 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
122 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
123 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
124 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
125
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
126 /* enable synchronization */
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
127 rv = FX_Send("\x0F");
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
128 if (! rv) {
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
129 SendMessage(mainThreadId, FX_Error());
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
130 goto error;
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
131 }
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
132 rv = FX_Recv(&resp, TIMEOUT);
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
133 if (! rv) {
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
134 SendMessage(mainThreadId, FX_Error());
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
135 goto error;
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
136 }
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
137
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
138 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
139 /* receive data from counter */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
140 FX_Recv(&resp, TIMEOUT);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
141 if (! resp) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
142 SendMessage(mainThreadId, FX_Error());
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
143 break;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
144 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
145
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
146 if (strneq(resp, "2900", 4)) {
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
147
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
148 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
149 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
150
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
151 /* parse received data */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
152 rv = Scan(resp + 6, "%f; %f; %f; %f; %f; %f; %f; %f",
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
153 &event.data[0], &event.data[1], &event.data[2], &event.data[3],
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
154 &event.data[4], &event.data[5], &event.data[6], &event.data[7]);
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
155 if (rv != nchan) {
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
156 SendMessage(mainThreadId, "KK Counter: data conversion error");
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
157 goto error;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
158 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
159
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
160 /* convert from kHz to Hz */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
161 for (int i = 0; i < nchan; i++)
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
162 event.data[i] *= 1000.0;
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
163
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
164 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
165 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
166
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
167 } else if (strneq(resp, "7000", 4)) {
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
168 /* ignore heart beat packet */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
169 } else if (strneq(resp, "7015", 4)) {
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
170 /* ignore undocumented packet. it is sent by newer kk counters
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
171 for each data packet. it probably contains a sample count along
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
172 with some other information */
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
173 } else if (strneq(resp, "7020", 4)) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
174 /* undocumented packet. it probably reports the header for
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
175 subsequent data packets. match it against expected value */
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
176 if (strcmp(resp + 6, "$2900")) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
177 SendMessage(mainThreadId, "KK Counter: %s", resp);
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
178 }
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
179 } else if (strneq(resp, "7F51", 4)) {
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
180 /* measurement interval synchronized */
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
181 SendMessage(mainThreadId, "KK Counter measurement interval synchronized");
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
182 } else {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
183 /* send message to the main thread */
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
184 SendMessage(mainThreadId, "KK Counter: %s", resp);
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
185 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
186 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
187
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
188 error:
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
189 /* close serial port */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
190 FX_Close();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
191 /* free allocated resources */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
192 FX_Free();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
193
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
194 return 0;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
195 }