annotate kk-data-provider.c @ 202:040658369850

Fix KK data provider setup
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:37 +0200
parents 4b74b81e0c79
children d7f91b9fb515
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 */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
16 #define MAXRETRY 20
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
17
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
18 #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
19
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
20
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
21 static inline int FX_Command(char cmd)
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
22 {
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
23 char *resp;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
24 int rv, retry = 0;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
25
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
26 /* reset counter buffer */
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
27 cmd = cmd + 0x80;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
28
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
29 /* send command */
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
30 rv = FX_Send(cmd);
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
31 if (! rv)
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
32 return rv;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
33
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
34 /* wait successfull reply */
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
35 do {
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
36 rv = FX_Recv(&resp, TIMEOUT);
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
37 if (! rv)
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
38 return rv;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
39 if (retry++ > MAXRETRY)
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
40 return 0;
202
040658369850 Fix KK data provider setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 194
diff changeset
41 } while (! strneq(resp, "7015", 4));
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
42
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
43 return rv;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
44 }
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
45
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
46
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47 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
48 {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 int mainThreadId;
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
50 int rv;
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
51 char cmd;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
52 char *resp;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
53 struct event event;
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
54 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
55 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
56 char port[256];
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 /* 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
59 mainThreadId = CmtGetMainThreadID();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
61 /* configuration file path */
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
62 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
63 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
64
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
65 /* 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
66 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
67 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
68
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
69 /* 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
70 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
71 if (rv < 1)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
72 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
73
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
74 /* channel number */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
75 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
76 if (rv < 1)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
77 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
78
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
79 /* enable scrambler */
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
80 rv = Ini_GetInt(configuration, "KK", "scrambler", &scrambler);
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
81 if (rv < 1)
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
82 scrambler = 0;
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
83
94
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
84 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
85 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
86
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
88 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
89
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
90 /* 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
91 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
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 }
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
96
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
97 /* get counter hardware version string */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
98 rv = FX_Send(0x81);
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
99 if (! rv) {
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, FX_Error());
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
101 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
102 }
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
103 /* wait for response */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
104 do {
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
105 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
106 if (! rv) {
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
107 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
108 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
109 }
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
110 } while (! strneq(resp, "7001", 4));
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
111 SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
112
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
113 /* set report interval 1sec */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
114 rv = FX_Command(0x29);
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
115 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
116 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
117 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
118 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
119
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
120 /* read nchan channels */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
121 cmd = 0x30 + nchan;
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
122 rv = FX_Command(cmd);
102
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 */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
129 rv = FX_Command(0x42);
102
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 }
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
134
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
135 /* scrambler */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
136 rv = FX_Command(0x50 + scrambler);
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
137 if (! rv) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
138 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
139 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
140 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
141
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
142 /* enable synchronization */
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
143 rv = FX_Command(0x0F);
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
144 if (! rv) {
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
145 SendMessage(mainThreadId, FX_Error());
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
146 goto error;
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
147 }
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
148
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
149 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
150 /* 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
151 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
152 if (! resp) {
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
153 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
154 break;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
155 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
156
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
157 /* 2900 with scrambler off or 2910 with scrambler on */
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
158 if (strneq(resp, "29", 2)) {
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 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
161 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
162
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
163 /* parse received data */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
164 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
165 &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
166 &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
167 if (rv != nchan) {
194
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
168 SendMessage(mainThreadId, "KK Counter: data conversion error: %d != %d", rv, nchan);
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
169 goto error;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
170 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
171
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
172 /* 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
173 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
174 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
175
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
176 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
177 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
178
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
179 } 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
180 /* 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
181 } 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
182 /* 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
183 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
184 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
185 } 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
186 /* undocumented packet. it probably reports the header for
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
187 subsequent data packets */
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
188 SendMessage(mainThreadId, "KK Counter packet header: %s", resp + 7);
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
189 } 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
190 /* measurement interval synchronized */
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
191 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
192 } else {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
193 /* 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
194 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
195 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
196 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
197
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
198 error:
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
199 /* close serial port */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
200 FX_Close();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
201 /* free allocated resources */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
202 FX_Free();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
203
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
204 return 0;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
205 }