annotate kk-data-provider.c @ 187:a5c70eae1e0b

Change data logging to 16 digits precision
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 24 Feb 2014 15:17:53 +0100
parents bc980095a24d
children f105ac22da05
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
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
9 #include "config.h"
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
10 #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
11 #include "KKFX80E.h"
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12
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;
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
23 char *cmd, *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;
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
26 int scrambler;
94
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 port[256];
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
28
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
29 /* 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
30 mainThreadId = CmtGetMainThreadID();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
31
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
32 /* configuration file path */
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
33 char path[MAX_PATHNAME_LEN];
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
34 GetIniFilePath(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
35
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
36 /* 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
37 IniText configuration = Ini_New(TRUE);
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
38 Ini_ReadFromFile(configuration, 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
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 /* 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
41 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
42 if (! rv)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
43 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
44
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
45 /* channel number */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
46 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
47 if (! rv)
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
48 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
49
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
50 /* enable scrambler */
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
51 rv = Ini_GetInt(configuration, "KK", "scrambler", &scrambler);
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
52 if (! rv)
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
53 scrambler = FALSE;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
54
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
55 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
56 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
57
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 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
60
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 /* 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
62 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
63 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
64 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
65 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
66 }
89
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 /* 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
69 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
70 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
71 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
72 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
73 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
74 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
75 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
76 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
77 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
78 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
79
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
80 /* get counter hardware version string */
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
81 rv = FX_Send("\x81");
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
82 if (! rv) {
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
83 SendMessage(mainThreadId, FX_Error());
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
84 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
85 }
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
86 rv = FX_Recv(&resp, TIMEOUT);
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
87 if (! rv) {
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
88 SendMessage(mainThreadId, FX_Error());
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
89 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
90 }
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
91 /* wait for response */
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
92 while (strneq(resp, "7000", 4)) {
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
93 rv = FX_Recv(&resp, TIMEOUT);
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
94 if (! rv) {
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
95 SendMessage(mainThreadId, FX_Error());
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
96 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
97 }
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
98 }
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
99 if (strneq(resp, "7001", 4))
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
100 SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
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 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
103 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
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
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
114 /* read nchan channels */
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
115 cmd = "\x30";
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
116 cmd[0] += nchan;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
117 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
118 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
119 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
120 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
121 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
122 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
123 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
124 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
125 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
126 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
127
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
128 /* 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
129 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
130 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
131 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
132 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
133 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
134 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
135 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
136 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
137 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
138 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
139
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
140 /* scrambler */
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
141 cmd = "\x50";
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
142 cmd[0] += scrambler;
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
143 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
144 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
145 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
146 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
147 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
148 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
149 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
150 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
151 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
152 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
153
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
154 /* enable synchronization */
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
155 rv = FX_Send("\x0F");
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
156 if (! rv) {
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
157 SendMessage(mainThreadId, FX_Error());
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
158 goto error;
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
159 }
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
160 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
161 if (! rv) {
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
162 SendMessage(mainThreadId, FX_Error());
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
163 goto error;
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
164 }
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
165
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
166 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
167 /* 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
168 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
169 if (! resp) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
170 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
171 break;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
172 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
173
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
174 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
175
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
176 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
177 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
178
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
179 /* parse received data */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
180 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
181 &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
182 &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
183 if (rv != nchan) {
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
184 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
185 goto error;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
186 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
187
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
188 /* 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
189 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
190 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
191
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
192 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
193 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
194
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
195 } 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
196 /* 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
197 } 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
198 /* 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
199 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
200 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
201 } 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
202 /* 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
203 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
204 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
205 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
206 }
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
207 } 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
208 /* measurement interval synchronized */
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
209 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
210 } else {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
211 /* 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
212 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
213 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
214 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
215
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
216 error:
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
217 /* close serial port */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
218 FX_Close();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
219 /* free allocated resources */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
220 FX_Free();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
221
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
222 return 0;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
223 }