Mercurial > hg > fxanalyse
comparison kk-data-provider.c @ 102:4f1f353e84f5
Add timeout to FX_Recv() function. Improve error handling in KKDataProvider
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Mon, 08 Apr 2013 16:18:05 +0200 |
parents | 4a11331eacbf |
children | 84fb0796936b |
comparison
equal
deleted
inserted
replaced
101:8b90fbba59ef | 102:4f1f353e84f5 |
---|---|
9 #include "data-provider.h" | 9 #include "data-provider.h" |
10 #include "KKFX80E.h" | 10 #include "KKFX80E.h" |
11 | 11 |
12 #define CONFIGFILE "FXAnalise.ini" | 12 #define CONFIGFILE "FXAnalise.ini" |
13 #define DEFAULTPORT "COM4:115200" | 13 #define DEFAULTPORT "COM4:115200" |
14 #define TIMEOUT 1 /* seconds */ | |
14 | 15 |
15 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) | 16 #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) |
16 | 17 |
17 int CVICALLBACK KKDataProvider (void *functionData) | 18 int CVICALLBACK KKDataProvider (void *functionData) |
18 { | 19 { |
35 IniText configuration = Ini_New(TRUE); | 36 IniText configuration = Ini_New(TRUE); |
36 Ini_ReadFromFile(configuration, pathname); | 37 Ini_ReadFromFile(configuration, pathname); |
37 | 38 |
38 /* get serial port name configuration */ | 39 /* get serial port name configuration */ |
39 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port)); | 40 rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port)); |
40 if (rv == 0) | 41 if (! rv) |
41 strncpy(port, DEFAULTPORT, sizeof(port)); | 42 strncpy(port, DEFAULTPORT, sizeof(port)); |
42 | 43 |
43 /* free */ | 44 /* free */ |
44 Ini_Dispose(configuration); | 45 Ini_Dispose(configuration); |
45 | 46 |
46 /* initialize library */ | 47 /* initialize library */ |
47 FX_Init(); | 48 FX_Init(); |
49 | |
48 /* connect to KK FX80E counter */ | 50 /* connect to KK FX80E counter */ |
49 rv = FX_Open(port); | 51 rv = FX_Open(port); |
52 if (! rv) { | |
53 SendMessage(mainThreadId, FX_Error()); | |
54 goto error; | |
55 } | |
50 | 56 |
51 /* clear transmit buffer */ | 57 /* clear transmit buffer */ |
52 FX_Send("\x80"); | 58 rv = FX_Send("\x80"); |
53 FX_Recv(&resp); | 59 if (! rv) { |
60 SendMessage(mainThreadId, FX_Error()); | |
61 goto error; | |
62 } | |
63 rv = FX_Recv(&resp, TIMEOUT); | |
64 if (! rv) { | |
65 SendMessage(mainThreadId, FX_Error()); | |
66 goto error; | |
67 } | |
68 | |
54 /* set report interval 1sec */ | 69 /* set report interval 1sec */ |
55 FX_Send("\x29"); | 70 rv = FX_Send("\x29"); |
56 FX_Recv(&resp); | 71 if (! rv) { |
72 SendMessage(mainThreadId, FX_Error()); | |
73 goto error; | |
74 } | |
75 rv = FX_Recv(&resp, TIMEOUT); | |
76 if (! rv) { | |
77 SendMessage(mainThreadId, FX_Error()); | |
78 goto error; | |
79 } | |
80 | |
57 /* read 4 channels */ | 81 /* read 4 channels */ |
58 FX_Send("\x34"); | 82 rv = FX_Send("\x34"); |
59 FX_Recv(&resp); | 83 if (! rv) { |
84 SendMessage(mainThreadId, FX_Error()); | |
85 goto error; | |
86 } | |
87 rv = FX_Recv(&resp, TIMEOUT); | |
88 if (! rv) { | |
89 SendMessage(mainThreadId, FX_Error()); | |
90 goto error; | |
91 } | |
92 | |
60 /* set mode to instantaneous frequency measurement */ | 93 /* set mode to instantaneous frequency measurement */ |
61 FX_Send("\x42"); | 94 rv = FX_Send("\x42"); |
62 FX_Recv(&resp); | 95 if (! rv) { |
96 SendMessage(mainThreadId, FX_Error()); | |
97 goto error; | |
98 } | |
99 rv = FX_Recv(&resp, TIMEOUT); | |
100 if (! rv) { | |
101 SendMessage(mainThreadId, FX_Error()); | |
102 goto error; | |
103 } | |
104 | |
63 /* switch scrambler off */ | 105 /* switch scrambler off */ |
64 FX_Send("\x50"); | 106 rv = FX_Send("\x50"); |
65 FX_Recv(&resp); | 107 if (! rv) { |
108 SendMessage(mainThreadId, FX_Error()); | |
109 goto error; | |
110 } | |
111 rv = FX_Recv(&resp, TIMEOUT); | |
112 if (! rv) { | |
113 SendMessage(mainThreadId, FX_Error()); | |
114 goto error; | |
115 } | |
66 | 116 |
67 while (acquiring) { | 117 while (acquiring) { |
68 /* receive data from counter */ | 118 /* receive data from counter */ |
69 FX_Recv(&resp); | 119 FX_Recv(&resp, TIMEOUT); |
120 if (! resp) { | |
121 SendMessage(mainThreadId, FX_Error()); | |
122 break; | |
123 } | |
70 | 124 |
71 if (strneq(resp, "2900;", 5)) { | 125 if (strneq(resp, "2900", 4)) { |
72 | 126 |
73 /* timestamp */ | 127 /* timestamp */ |
74 gettimeofday(&event.time, NULL); | 128 gettimeofday(&event.time, NULL); |
75 | 129 |
76 /* parse received data */ | 130 /* parse received data */ |
84 event.data[3] *= 1000.0; | 138 event.data[3] *= 1000.0; |
85 | 139 |
86 /* push data into the data queue */ | 140 /* push data into the data queue */ |
87 CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0); | 141 CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0); |
88 | 142 |
89 } else if (strneq(resp, "7000;", 5)) { | 143 } else if (strneq(resp, "7000", 4)) { |
90 /* ignore heart beat packet */ | 144 /* ignore heart beat packet */ |
91 } else if (strneq(resp, "7015;", 5)) { | 145 } else if (strneq(resp, "7015", 4)) { |
92 /* ignore undocumented packet. it is sent by newer kk counters | 146 /* ignore undocumented packet. it is sent by newer kk counters |
93 for each data packet. it contains a sample count along with | 147 for each data packet. it probably contains a sample count along |
94 other information */ | 148 with some other information */ |
149 } else if (strneq(resp, "7020", 4)) { | |
150 /* undocumented packet. it probably reports the header for | |
151 subsequent data packets. match it against expected value */ | |
152 if (strcmp(resp + 6, "$2900")) { | |
153 SendMessage(mainThreadId, "KK Counter: %s", resp); | |
154 } | |
95 } else { | 155 } else { |
96 /* send message to the main thread */ | 156 /* send message to the main thread */ |
97 SendMessage(mainThreadId, resp); | 157 SendMessage(mainThreadId, "KK Counter: %s", resp); |
98 } | 158 } |
99 } | 159 } |
100 | 160 |
161 error: | |
101 /* close serial port */ | 162 /* close serial port */ |
102 FX_Close(); | 163 FX_Close(); |
103 /* free allocated resources */ | 164 /* free allocated resources */ |
104 FX_Free(); | 165 FX_Free(); |
105 | 166 |