Mercurial > hg > fxanalyse
comparison kk-data-provider.c @ 194:4b74b81e0c79
Fix KK data provider. Wait for command reply before sending the next one.
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 31 Mar 2014 17:03:32 +0200 |
parents | f105ac22da05 |
children | 040658369850 |
comparison
equal
deleted
inserted
replaced
193:3427013e4f70 | 194:4b74b81e0c79 |
---|---|
11 #include "KKFX80E.h" | 11 #include "KKFX80E.h" |
12 | 12 |
13 #define DEFAULT_PORT "COM4:115200" | 13 #define DEFAULT_PORT "COM4:115200" |
14 #define DEFAULT_NCHAN 4 | 14 #define DEFAULT_NCHAN 4 |
15 #define TIMEOUT 1 /* seconds */ | 15 #define TIMEOUT 1 /* seconds */ |
16 #define MAXRETRY 20 | |
16 | 17 |
17 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) | 18 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) |
19 | |
20 | |
21 static inline int FX_Command(char cmd) | |
22 { | |
23 char *resp; | |
24 int rv, retry = 0; | |
25 | |
26 /* reset counter buffer */ | |
27 cmd = cmd + 0x80; | |
28 | |
29 /* send command */ | |
30 rv = FX_Send(cmd); | |
31 if (! rv) | |
32 return rv; | |
33 | |
34 /* wait successfull reply */ | |
35 do { | |
36 rv = FX_Recv(&resp, TIMEOUT); | |
37 if (! rv) | |
38 return rv; | |
39 if (retry++ > MAXRETRY) | |
40 return 0; | |
41 } while (strneq(resp, "7020", 4)); | |
42 | |
43 return rv; | |
44 } | |
45 | |
18 | 46 |
19 int CVICALLBACK KKDataProvider (void *functionData) | 47 int CVICALLBACK KKDataProvider (void *functionData) |
20 { | 48 { |
21 int mainThreadId; | 49 int mainThreadId; |
22 int rv; | 50 int rv; |
23 char *cmd, *resp; | 51 char cmd; |
52 char *resp; | |
24 struct event event; | 53 struct event event; |
25 int nchan; | 54 int nchan; |
26 int scrambler; | 55 int scrambler; |
27 char port[256]; | 56 char port[256]; |
28 | 57 |
62 rv = FX_Open(port); | 91 rv = FX_Open(port); |
63 if (! rv) { | 92 if (! rv) { |
64 SendMessage(mainThreadId, FX_Error()); | 93 SendMessage(mainThreadId, FX_Error()); |
65 goto error; | 94 goto error; |
66 } | 95 } |
67 | 96 |
68 /* clear transmit buffer */ | |
69 rv = FX_Send("\x80"); | |
70 if (! rv) { | |
71 SendMessage(mainThreadId, FX_Error()); | |
72 goto error; | |
73 } | |
74 rv = FX_Recv(&resp, TIMEOUT); | |
75 if (! rv) { | |
76 SendMessage(mainThreadId, FX_Error()); | |
77 goto error; | |
78 } | |
79 | |
80 /* get counter hardware version string */ | 97 /* get counter hardware version string */ |
81 rv = FX_Send("\x81"); | 98 rv = FX_Send(0x81); |
82 if (! rv) { | |
83 SendMessage(mainThreadId, FX_Error()); | |
84 goto error; | |
85 } | |
86 rv = FX_Recv(&resp, TIMEOUT); | |
87 if (! rv) { | 99 if (! rv) { |
88 SendMessage(mainThreadId, FX_Error()); | 100 SendMessage(mainThreadId, FX_Error()); |
89 goto error; | 101 goto error; |
90 } | 102 } |
91 /* wait for response */ | 103 /* wait for response */ |
92 while (strneq(resp, "7000", 4)) { | 104 do { |
93 rv = FX_Recv(&resp, TIMEOUT); | 105 rv = FX_Recv(&resp, TIMEOUT); |
94 if (! rv) { | 106 if (! rv) { |
95 SendMessage(mainThreadId, FX_Error()); | 107 SendMessage(mainThreadId, FX_Error()); |
96 goto error; | 108 goto error; |
97 } | 109 } |
98 } | 110 } while (! strneq(resp, "7001", 4)); |
99 if (strneq(resp, "7001", 4)) | 111 SendMessage(mainThreadId, "KK Counter version: %s", resp + 4); |
100 SendMessage(mainThreadId, "KK Counter version: %s", resp + 4); | |
101 | 112 |
102 /* set report interval 1sec */ | 113 /* set report interval 1sec */ |
103 rv = FX_Send("\x29"); | 114 rv = FX_Command(0x29); |
104 if (! rv) { | |
105 SendMessage(mainThreadId, FX_Error()); | |
106 goto error; | |
107 } | |
108 rv = FX_Recv(&resp, TIMEOUT); | |
109 if (! rv) { | 115 if (! rv) { |
110 SendMessage(mainThreadId, FX_Error()); | 116 SendMessage(mainThreadId, FX_Error()); |
111 goto error; | 117 goto error; |
112 } | 118 } |
113 | 119 |
114 /* read nchan channels */ | 120 /* read nchan channels */ |
115 cmd = "\x30"; | 121 cmd = 0x30 + nchan; |
116 cmd[0] += nchan; | 122 rv = FX_Command(cmd); |
117 rv = FX_Send(cmd); | |
118 if (! rv) { | |
119 SendMessage(mainThreadId, FX_Error()); | |
120 goto error; | |
121 } | |
122 rv = FX_Recv(&resp, TIMEOUT); | |
123 if (! rv) { | 123 if (! rv) { |
124 SendMessage(mainThreadId, FX_Error()); | 124 SendMessage(mainThreadId, FX_Error()); |
125 goto error; | 125 goto error; |
126 } | 126 } |
127 | 127 |
128 /* set mode to instantaneous frequency measurement */ | 128 /* set mode to instantaneous frequency measurement */ |
129 rv = FX_Send("\x42"); | 129 rv = FX_Command(0x42); |
130 if (! rv) { | 130 if (! rv) { |
131 SendMessage(mainThreadId, FX_Error()); | 131 SendMessage(mainThreadId, FX_Error()); |
132 goto error; | 132 goto error; |
133 } | 133 } |
134 rv = FX_Recv(&resp, TIMEOUT); | 134 |
135 if (! rv) { | |
136 SendMessage(mainThreadId, FX_Error()); | |
137 goto error; | |
138 } | |
139 | |
140 /* scrambler */ | 135 /* scrambler */ |
141 cmd = "\x50"; | 136 rv = FX_Command(0x50 + scrambler); |
142 cmd[0] += scrambler; | |
143 rv = FX_Send(cmd); | |
144 if (! rv) { | |
145 SendMessage(mainThreadId, FX_Error()); | |
146 goto error; | |
147 } | |
148 rv = FX_Recv(&resp, TIMEOUT); | |
149 if (! rv) { | 137 if (! rv) { |
150 SendMessage(mainThreadId, FX_Error()); | 138 SendMessage(mainThreadId, FX_Error()); |
151 goto error; | 139 goto error; |
152 } | 140 } |
153 | 141 |
154 /* enable synchronization */ | 142 /* enable synchronization */ |
155 rv = FX_Send("\x0F"); | 143 rv = FX_Command(0x0F); |
156 if (! rv) { | |
157 SendMessage(mainThreadId, FX_Error()); | |
158 goto error; | |
159 } | |
160 rv = FX_Recv(&resp, TIMEOUT); | |
161 if (! rv) { | 144 if (! rv) { |
162 SendMessage(mainThreadId, FX_Error()); | 145 SendMessage(mainThreadId, FX_Error()); |
163 goto error; | 146 goto error; |
164 } | 147 } |
165 | 148 |
180 /* parse received data */ | 163 /* parse received data */ |
181 rv = Scan(resp + 6, "%f; %f; %f; %f; %f; %f; %f; %f", | 164 rv = Scan(resp + 6, "%f; %f; %f; %f; %f; %f; %f; %f", |
182 &event.data[0], &event.data[1], &event.data[2], &event.data[3], | 165 &event.data[0], &event.data[1], &event.data[2], &event.data[3], |
183 &event.data[4], &event.data[5], &event.data[6], &event.data[7]); | 166 &event.data[4], &event.data[5], &event.data[6], &event.data[7]); |
184 if (rv != nchan) { | 167 if (rv != nchan) { |
185 SendMessage(mainThreadId, "KK Counter: data conversion error"); | 168 SendMessage(mainThreadId, "KK Counter: data conversion error: %d != %d", rv, nchan); |
186 goto error; | 169 goto error; |
187 } | 170 } |
188 | 171 |
189 /* convert from kHz to Hz */ | 172 /* convert from kHz to Hz */ |
190 for (int i = 0; i < nchan; i++) | 173 for (int i = 0; i < nchan; i++) |