annotate kk-data-provider.c @ 239:ec81395bf08d

Solve name collisions and other problems caused by including Windows headers
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 12 Feb 2015 19:13:55 +0100
parents d7f91b9fb515
children 9b35a2b2c759
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 */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
16 #define MAXRETRY 100
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
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
21 static char * FX_Command(unsigned char cmd, unsigned mask, unsigned value)
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
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;
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
24 int rv, retry;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
25 unsigned int header;
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
26
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 /* 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
28 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
29 if (! rv)
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
30 return NULL;
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
31
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 /* wait successfull reply */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
33 for (retry = 0; retry < MAXRETRY; retry++) {
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
34 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
35 if (! rv)
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
36 return NULL;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
37 header = strtoul(resp, NULL, 16);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
38 if ((header & mask) == value)
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
39 return resp;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
40 }
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
41
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
42 /* max retry reached */
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
43 return NULL;
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
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 {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
49 int main_thread_id;
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 *resp;
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
52 struct event event;
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
53 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
54 char port[256];
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56 /* get main thread id to post messages to it */
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
57 main_thread_id = CmtGetMainThreadID();
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
59 /* configuration file path */
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
60 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
61 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
62
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
63 /* 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
64 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
65 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
66
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
67 /* 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
68 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
69 if (rv < 1)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
70 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
71
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
72 /* channel number */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
73 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
74 if (rv < 1)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
75 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
76
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
77 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
78 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
79
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
81 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
82
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
83 /* 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
84 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
85 if (! rv) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
86 send_message(main_thread_id, FX_Error());
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
87 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
88 }
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
89
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
90 /* get counter hardware version string */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
91 resp = FX_Command(0x81, 0xFFFF, 0x7001);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
92 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
93 send_message(main_thread_id, FX_Error());
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
94 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
95 }
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
96 send_message(main_thread_id, "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
97
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
98 /* set report interval 1sec */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
99 resp = FX_Command(0x29, 0x0F00, 0x0900);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
100 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
101 send_message(main_thread_id, FX_Error());
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
102 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
103 }
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
104
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
105 /* set mode to instantaneous frequency measurement */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
106 resp = FX_Command(0x42, 0x6F00, 0x2900);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
107 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
108 send_message(main_thread_id, FX_Error());
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
109 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
110 }
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
111
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
112 /* disable scrambler */
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
113 resp = FX_Command(0x50, 0x6FFF, 0x2900);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
114 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
115 send_message(main_thread_id, FX_Error());
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
116 goto error;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
117 }
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
118
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
119 /* read nchan channels */
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
120 resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
121 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
122 send_message(main_thread_id, FX_Error());
102
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 }
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
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 */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
127 resp = FX_Command(0x0F, 0x00, 0x00);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
128 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
129 send_message(main_thread_id, FX_Error());
120
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
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
133 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
134 /* 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
135 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
136 if (! resp) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
137 send_message(main_thread_id, FX_Error());
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
138 break;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
139 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
140
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
141 /* data packets */
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
142 if (strneq(resp, "2900", 2)) {
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
143
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
144 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
145 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
146
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
147 /* parse received data */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
148 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
149 &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
150 &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
151 if (rv != nchan) {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
152 send_message(main_thread_id, "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
153 goto error;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
154 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
155
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
156 /* 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
157 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
158 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
159
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
160 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
161 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
162
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
163 } 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
164 /* 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
165 } 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
166 /* 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
167 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
168 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
169 } 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
170 /* undocumented packet. it probably reports the header for
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
171 subsequent data packets */
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
172 send_message(main_thread_id, "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
173 } 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
174 /* measurement interval synchronized */
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
175 send_message(main_thread_id, "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
176 } else {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
177 /* send message to the main thread */
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
178 send_message(main_thread_id, "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
179 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
180 }
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
181
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
182 error:
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
183 /* close serial port */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
184 FX_Close();
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
185 /* free allocated resources */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
186 FX_Free();
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 return 0;
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
189 }