annotate kk-data-provider.c @ 258:5296f3bcd160

Implement DDS clients reconnect On network send() failures try to reconnect to the server before returning an error. This allows to restart the network servers controlling the DDSes wiothout having to restart the clients.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 16 Jun 2015 14:31:35 +0200
parents 9b35a2b2c759
children
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
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
13
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
14 #define FX_MODE_PI 0x02
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
15 #define FX_MODE_LAMBDA 0x03
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
16
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
17 #define DEFAULT_PORT "COM4:115200"
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
18 #define DEFAULT_NCHAN 4
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
19 #define DEFAULT_MODE FX_MODE_PI
102
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
20 #define TIMEOUT 1 /* seconds */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
21 #define MAXRETRY 100
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
22
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
23 #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
24
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
25
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
26 const char * FX_ModeString(int mode)
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
27 {
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
28 const static char *modes[] = {
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
29 [FX_MODE_PI] = "PI",
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
30 [FX_MODE_LAMBDA] = "LAMBDA",
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
31 };
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
32
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
33 return modes[mode];
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
34 }
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
35
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
36
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
37 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
38 {
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 char *resp;
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
40 int rv, retry;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
41 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
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 /* 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
44 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
45 if (! rv)
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
46 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
47
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
48 /* wait successfull reply */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
49 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
50 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
51 if (! rv)
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
52 return NULL;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
53 header = strtoul(resp, NULL, 16);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
54 if ((header & mask) == value)
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
55 return resp;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
56 }
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
57
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
58 /* max retry reached */
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
59 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
60 }
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
61
4b74b81e0c79 Fix KK data provider. Wait for command reply before sending the next one.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 192
diff changeset
62
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63 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
64 {
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
65 int main_thread_id;
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
66 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
67 char *resp;
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
68 int nchan = DEFAULT_NCHAN;
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
69 int mode = DEFAULT_MODE;
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
70 char port[256];
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
71 char buffer[16];
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
72 char header[16];
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
73 struct event event;
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 /* 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
76 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
77
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
78 /* configuration file path */
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
79 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
80 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
81
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
82 /* 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
83 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
84 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
85
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
86 /* serial port name */
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
87 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port));
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
88 if (rv < 1)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
89 strncpy(port, DEFAULT_PORT, sizeof(port));
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
90
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
91 /* gate type */
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
92 rv = Ini_GetStringIntoBuffer(configuration, "KK", "mode", buffer, sizeof(buffer));
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
93 if (rv < 1)
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
94 mode = DEFAULT_MODE;
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
95 else if (stricmp(buffer, "PI") == 0)
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
96 mode = FX_MODE_PI;
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
97 else if (stricmp(buffer, "LAMBDA") == 0)
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
98 mode = FX_MODE_LAMBDA;
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
99 else
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
100 send_message(main_thread_id, "KK Counter: unrecognised mode parameter '%s'", buffer);
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
101
181
c96f5f64c946 Armonize configuration file path construction in KK data provider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 177
diff changeset
102 /* channel number */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
103 rv = Ini_GetInt(configuration, "KK", "nchan", &nchan);
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
104 if (rv < 1)
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
105 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
106
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
107 /* free */
b7ae2d947617 Change default KK port to COM4. Make port configurable via .ini file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 91
diff changeset
108 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
109
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
110 /* initialize library */
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
111 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
112
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
113 /* 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
114 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
115 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
116 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
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 }
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
119
185
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
120 /* get counter hardware version string */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
121 resp = FX_Command(0x81, 0xFFFF, 0x7001);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
122 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
123 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
124 goto error;
bc980095a24d Add scrambler option to KK data provider. Report KK counter version.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 181
diff changeset
125 }
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
126 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
127
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
128 /* set report interval 1sec */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
129 resp = FX_Command(0x29, 0x0F00, 0x0900);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
130 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
131 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
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
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
135 /* set mode */
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
136 resp = FX_Command(0x40 + mode, 0x7000, (mode << 12));
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
137 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
138 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
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 }
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
141 send_message(main_thread_id, "KK Counter: set mode %s", FX_ModeString(mode));
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
142
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
143 /* disable scrambler */
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
144 resp = FX_Command(0x50, 0xFFFF, (mode << 12) + 0x0900);
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
145 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
146 send_message(main_thread_id, FX_Error());
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
147 goto error;
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
148 }
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
149
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
150 /* read nchan channels */
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
151 resp = FX_Command(0x30 + nchan, 0xFFFF, (mode << 12) + 0x0900);
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
152 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
153 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
154 goto error;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
155 }
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
156
120
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
157 /* enable synchronization */
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
158 resp = FX_Command(0x0F, 0x00, 0x00);
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
159 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
160 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
161 goto error;
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
162 }
84fb0796936b Add KK counter measurement interval synchronization to PPS trigger
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 102
diff changeset
163
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
164 /* expected packet header */
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
165 snprintf(header, sizeof(header), "%X", (mode << 12) + 0x0900);
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
166
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
167 while (acquiring) {
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
168 /* 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
169 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
170 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
171 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
172 break;
4f1f353e84f5 Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 96
diff changeset
173 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
174
204
d7f91b9fb515 Fix KK counter setup
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 202
diff changeset
175 /* data packets */
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
176 if (strneq(resp, header, 4)) {
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
177
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
178 /* timestamp */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
179 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
180
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
181 /* parse received data */
177
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
182 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
183 &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
184 &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
185 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
186 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
187 goto error;
5b4b7f37bd3b Make number of KK counter channels read configurable
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 163
diff changeset
188 }
89
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
189
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
190 /* 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
191 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
192 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
193
c9e4f63c2033 Implement data acquisition through direct communication with the KK counter
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
194 /* push data into the data queue */
91
4102fe614df2 Fix timestamping. Cleanup data providers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 89
diff changeset
195 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
196
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, "7000", 4)) {
96
4a11331eacbf Ignore packet with id '7015' in KK driver
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 94
diff changeset
198 /* 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
199 } 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
200 /* 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
201 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
202 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
203 } 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
204 /* undocumented packet. it probably reports the header for
192
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 185
diff changeset
205 subsequent data packets */
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
206 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
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 */
250
9b35a2b2c759 Implement selection of gate type (PI or LAMBDA) in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
209 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
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 */
239
ec81395bf08d Solve name collisions and other problems caused by including Windows headers
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 204
diff changeset
212 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
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 }