comparison DDS_Fox.c @ 68:64a7a1d3d75c

Cleanup AD9956 DDS driver
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 29 Oct 2012 14:48:14 +0100
parents 2320f5fc66aa
children 8e6a2ec85169
comparison
equal deleted inserted replaced
67:0159abc1a9d8 68:64a7a1d3d75c
1 #include <tcpsupp.h> 1 #include <tcpsupp.h>
2 #include <ansi_c.h> 2 #include <ansi_c.h>
3 #include "DDS_Fox.h" 3 #include "DDS_Fox.h"
4 4
5 //#define DDS_IP "192.168.0.3" 5 // #define DEBUG 1
6 //#define TCP_PORT 6665
7
8 #define DIV 0 // divided by 1
9 #define CMD_SEP ";" 6 #define CMD_SEP ";"
10 #define BASE_TIME 0.01 7 #define BASE_TIME 0.01
11 8
12 #define CMD_CONFIG_REG1 1 // cmd d'acces direct au rergistre 1 de config de la DDS (voir datasheet) 9 #define CMD_CONFIG_REG1 1 // cmd d'acces direct au rergistre 1 de config de la DDS (voir datasheet)
13 #define CMD_CONFIG_REG2 2 // cmd d'acces direct au rergistre 1 de config de la DDS (voir datasheet) 10 #define CMD_CONFIG_REG2 2 // cmd d'acces direct au rergistre 1 de config de la DDS (voir datasheet)
23 #define CMD_START_SWEEP 25 20 #define CMD_START_SWEEP 25
24 #define CMD_STOP_SWEEP 26 21 #define CMD_STOP_SWEEP 26
25 #define CMD_GET_FREQ 28 22 #define CMD_GET_FREQ 28
26 #define CMD_SWEEP_SIGN 29 23 #define CMD_SWEEP_SIGN 29
27 24
28 25 typedef struct {
29 /************************* Global variables ***********************************/ 26 char* action;
30 //static char Request[255]; 27 char* canal;
31 28 char* command;
32 /************************* My own functions ***********************************/ 29 char* value0;
33 /*DDSParameter DDSGetParam (int panel, int control) { 30 char* value1;
34 DDSParameter Parameter ; 31 char* value2;
35 32 char* value3;
36 GetCtrlVal (panel, CONTROL_DDS_IP, Parameter->ip) ; 33 char* value4;
37 GetCtrlVal (panel, CONTROL_DDS_PORT, &(Parameter->Port)) ; 34 char* value5;
38 GetCtrlVal (panel, CONTROL_DDS_CLOCK, &(Parameter->Clock)) ; 35 char* value6;
39 GetCtrlVal (panel, CONTROL_DDS_RATE, &(Parameter->SweepRate)) ; 36 char* value7;
40 GetCtrlVal (panel, CONTROL_DDS_DELTA_T, &(Parameter->Delta_T)) ; 37 char* value8;
41 return(Parameter); 38 char* value9;
42 }*/ 39 char* value10;
43 40 char* value11;
41 char* value12;
42 char* value13;
43 char* value14;
44 char* value15;
45 char* value16;
46 char* value17;
47 char* value18;
48 char* value19;
49 } Command;
50
51 static int SendCmd(char *Buffer, DDSParameter *Param);
52 static int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param);
53 static void init_command(Command *cmd);
54 static int parse_command(Command *cmd1, char *buf);
55 static void convert_double_6char (double fraction, unsigned char* octets);
44 56
45 #define error(...) _error(__FILE__, __LINE__, __func__, __VA_ARGS__) 57 #define error(...) _error(__FILE__, __LINE__, __func__, __VA_ARGS__)
46 58
47 void _error(const char *file, int line, const char *func, const char *frmt, ...) 59 void _error(const char *file, int line, const char *func, const char *frmt, ...)
48 { 60 {
52 vfprintf(stderr, frmt, args); 64 vfprintf(stderr, frmt, args);
53 fprintf(stderr, "\n"); 65 fprintf(stderr, "\n");
54 va_end(args); 66 va_end(args);
55 } 67 }
56 68
57 69 void DDSFox_Initialize(DDSParameter *Param, char *ip, int port, double frequency)
58 void DDS_Initialize (DDSParameter *Param) { 70 {
59 char Request[255]; 71 strcpy(Param->ip, ip);
60 //send NOP command 72 Param->Port = 6665;
61 sprintf(Request,"set;%i;%i",1,CMD_NOP); 73 Param->Profil = 7; // configuration profile: single freq=0, sweep=7
62 SendCmd(Request, Param) ; 74 Param->Clock = 200000000;
63 //reset the DDS 75 Param->Delta_T = 0.01;
76 Param->SweepRate = 0.0;
77
78 // reset the DDS
79 //DDSFox_Nop(Param);
64 DDSFox_Reset(Param); 80 DDSFox_Reset(Param);
65 81 // set profile number 7 soft frequecy sweep
66 //Set Profile Number 0 (only one used for soft frequecy sweep) 82 DDSFox_SetProfile(Param);
67 // sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,0); 83 // set clock divider to 1
68 // SendCmd(Request, Param) ; 84 DDSFox_SetDiv(Param, 1);
69 85 // set frequency
70 //Set Profile Number 7 (only one used for soft frequecy sweep) 86 DDSFox_SetFreqInf(Param, frequency);
71 sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,7); 87 // set frequency upper bound to max value
72 SendCmd(Request, Param) ; 88 DDSFox_SetFreqMax(Param);
73 89 // set sweep rate to 0
74 90 DDSFox_SetDT(Param);
75 //Set Clock Divider to 00b (which means 1) 91 DDSFox_SetDf(Param);
76 DDSFox_SetDiv(Param); 92 DDSFox_SetSweepSign(Param);
77 //Set sweep rate at 0 93 // start sweep
78 DDSFox_SetFreqMax (Param); 94 DDSFox_StartSweep(Param);
79 DDSFox_SetFreq (Param) ; 95 // set frequency lower bound to min value
80 DDSFox_SetDT (Param) ; 96 DDSFox_SetFreqMin(Param);
81 DDSFox_SetDf (Param) ; 97
82 DDSFox_StartSweep (Param) ; 98 return;
83 return ; 99 }
84 } 100
85 101 void DDSFox_SetSweepRate(DDSParameter *Param, double sweepRate)
86 void DDS_ReInitialize (DDSParameter *Param) { 102 {
87 char Request[255]; 103 Param->SweepRate = sweepRate;
88 //send NOP command 104 DDSFox_SetDf(Param);
89 sprintf(Request,"set;%i;%i",1,CMD_NOP); 105 DDSFox_SetSweepSign(Param);
90 SendCmd(Request, Param) ; 106 }
91 //reset the DDS 107
92 //DDSFox_Reset(Param); 108 void DDSFox_Set(DDSParameter *Param, double frequency, double sweepRate)
93 109 {
94 //Set Profile Number 0 (only one used for soft frequecy sweep) 110 // set desired frequency
95 // sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,0); 111 DDSFox_SetFreqInf(Param, frequency);
96 // SendCmd(Request, Param) ; 112 // set frequency upper bound to max value
97 113 DDSFox_SetFreqMax(Param);
98 //Set Profile Number 7 (only one used for soft frequecy sweep) 114 // set sweep rate to 0
99 sprintf(Request,"set;%i;%i;%i",1,CMD_PROFILE,7); 115 Param->SweepRate = 0.0;
100 SendCmd(Request, Param) ; 116 DDSFox_SetDT(Param);
101 117 DDSFox_SetDf(Param);
102 118 DDSFox_SetSweepSign(Param);
103 //Set Clock Divider to 00b (which means 1) 119 // start sweep
104 DDSFox_SetDiv(Param); 120 DDSFox_StartSweep(Param);
105 //Set sweep rate at 0 121 // set frequency lower bound to min value
106 DDSFox_SetFreqMax (Param); 122 DDSFox_SetFreqMin(Param);
107 DDSFox_SetFreq (Param) ; 123 // set desired sweep rate
108 DDSFox_SetDT (Param) ; 124 Param->SweepRate = sweepRate;
109 DDSFox_SetDf (Param) ; 125 DDSFox_SetDf(Param);
110 DDSFox_StartSweep (Param) ; 126 DDSFox_SetSweepSign(Param);
111 return ; 127 }
112 } 128
113 129 void DDSFox_Nop (DDSParameter *Param)
114 130 {
115 void DDSFox_Reset(DDSParameter *Param) { 131 // send NOP command
116 char Request[255]; 132 char request[255];
117 sprintf(Request,"set;%i;%i",1,CMD_NOP); 133 sprintf(request, "set;%i;%i", 1, CMD_NOP);
118 SendCmd(Request, Param) ; 134 SendCmd(request, Param);
135 }
136
137 void DDSFox_SetProfile (DDSParameter *Param)
138 {
139 char request[255];
140 sprintf(request, "set;%i;%i;%i", 1, CMD_PROFILE, Param->Profil);
141 SendCmd(request, Param);
142 }
143
144 void DDSFox_Reset(DDSParameter *Param)
145 {
146 char Request[255];
147 //sprintf(Request,"set;%i;%i",1,CMD_NOP);
148 //SendCmd(Request, Param) ;
119 sprintf(Request,"set;%i;%i",1,CMD_RESET_DDS); 149 sprintf(Request,"set;%i;%i",1,CMD_RESET_DDS);
120 SendCmd(Request, Param) ; 150 SendCmd(Request, Param) ;
121 return ; 151 return ;
122 } 152 }
123 153
124 154 void DDSFox_StopSweep(DDSParameter *Param)
125 void DDSFox_StopSweep(DDSParameter *Param) { 155 {
126 char Request[255]; 156 char Request[255];
127 sprintf(Request,"set;%i;%i;%i",1,CMD_STOP_SWEEP,1); 157 sprintf(Request,"set;%i;%i;%i",1,CMD_STOP_SWEEP,1);
128 SendCmd(Request, Param); 158 SendCmd(Request, Param);
129 return ; 159 return ;
130 } 160 }
131 161
132 162 void DDSFox_StartSweep(DDSParameter *Param)
133 void DDSFox_StartSweep(DDSParameter *Param) { 163 {
134 char Request[255]; 164 char Request[255];
135 sprintf(Request,"set;%i;%i;%i",1,CMD_START_SWEEP,1); 165 sprintf(Request,"set;%i;%i;%i",1,CMD_START_SWEEP,1);
136 SendCmd(Request, Param); 166 SendCmd(Request, Param);
137 return ; 167 return ;
138 } 168 }
139 169
140 void DDSFox_SetFreqMax (DDSParameter *Param) { 170 void DDSFox_SetFreqMax (DDSParameter *Param)
171 {
141 unsigned char octets[6]; 172 unsigned char octets[6];
142 char Request[255]; 173 char Request[255];
143 convert_double_6char(pow(2,48)/2,octets); // borne sup : clock / 2 174 convert_double_6char(pow(2,48)/2,octets); // borne sup : clock / 2
144 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP, 175 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP,
145 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); 176 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets);
146 SendCmd(Request, Param); 177 SendCmd(Request, Param);
147 DDSFox_SetSweepSign (Param); 178 DDSFox_SetSweepSign (Param);
148 return ; 179 return ;
149 } 180 }
150 181
151 void DDSFox_SetFreqMin (DDSParameter *Param) { 182 void DDSFox_SetFreqMin (DDSParameter *Param)
183 {
152 unsigned char octets[6]; 184 unsigned char octets[6];
153 char Request[255]; 185 char Request[255];
154 convert_double_6char(1,octets); // borne inf : ~0 186 convert_double_6char(1,octets); // borne inf : ~0
155 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, 187 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF,
156 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); 188 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets);
157 SendCmd(Request, Param); 189 SendCmd(Request, Param);
158 DDSFox_SetSweepSign (Param); 190 DDSFox_SetSweepSign (Param);
159 return ; 191 return ;
160 } 192 }
161 193
162 void DDSFox_SetFreq (DDSParameter *Param) { 194 void DDSFox_SetFreqInf (DDSParameter *Param, double frequency)
195 {
163 double word ; 196 double word ;
164 unsigned char octets[6]; 197 unsigned char octets[6];
165 int SweepSign = -1 ; 198 char Request[255];
166 char Request[255]; 199 word = pow(2,48)*(frequency / Param->Clock);
167 if (Param->Profil == 0) { 200 convert_double_6char(word, octets);
168 word = pow(2,48)*(Param->Frequency/Param->Clock); 201 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF,
169 convert_double_6char(word, octets); // borne inf : la freq voulu 202 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets);
170 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, 203 SendCmd(Request, Param);
171 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); 204 }
172 SendCmd(Request, Param); 205
173 } 206 void DDSFox_SetFreqSup (DDSParameter *Param, double frequency)
174 else if(Param->Profil == 7) { 207 {
175 SweepSign = DDSFox_GetSweepSign(Param) ; 208 double word ;
176 if(SweepSign == 0){ // sweep positif 209 unsigned char octets[6];
177 word = pow(2,48)*(Param->Frequency/Param->Clock); 210 char Request[255];
178 convert_double_6char(word, octets); // borne inf : la freq voulu 211 word = pow(2,48)*(frequency / Param->Clock);
179 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_INF, 212 convert_double_6char(word, octets);
180 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); 213 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP,
181 SendCmd(Request, Param); 214 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets);
182 DDSFox_SetFreqMax (Param); 215 SendCmd(Request, Param);
183 } 216 }
184 else { // sweep neg 217
185 word = pow(2,48)*(Param->Frequency/Param->Clock); 218 static int GetSweepSign(DDSParameter *Param)
186 convert_double_6char(word, octets); // borne sup : la freq voulue 219 {
187 sprintf(Request,"set;%i;%i;%i;%i;%i;%i;%i;%i",1,CMD_FREQ_SUP, 220 if (Param->SweepRate >= 0.0)
188 *(octets+5),*(octets+4),*(octets+3),*(octets+2),*(octets+1),*octets); 221 return 0;
189 SendCmd(Request, Param); 222 return 1;
190 DDSFox_SetFreqMin (Param); 223 }
191 } 224
192 } 225 void DDSFox_SetSweepSign(DDSParameter *Param)
193 return ; 226 {
194 } 227 char Request[255] ;
195
196
197 // Change le signe de sweep en frequence. Argument: struct de param de la DDS
198 void DDSFox_SetSweepSign(DDSParameter *Param) {
199 char Request[255] ;
200 int SweepSign ; 228 int SweepSign ;
201 SweepSign = DDSFox_GetSweepSign(Param) ; 229 SweepSign = GetSweepSign(Param) ;
202 sprintf(Request,"set;%i;%i;%i",1,CMD_SWEEP_SIGN, SweepSign) ; 230 sprintf(Request, "set;%i;%i;%i", 1, CMD_SWEEP_SIGN, SweepSign);
203 SendCmd(Request, Param) ; 231 SendCmd(Request, Param) ;
204 return ; 232 return ;
205 } 233 }
206 234
207 /*void DDSFox_SetSweepSign(DDSParameter *Param) { 235 void DDSFox_SetDT(DDSParameter *Param)
208 char Request[255] ; 236 {
209 int SweepSign ;
210 if (Param->Profil == 7)
211 Param->Frequency = DDSFox_ReadFreq(Param) ;
212 DDSFox_SetFreq (Param) ;
213 SweepSign = DDSFox_GetSweepSign(Param) ;
214 sprintf(Request,"set;%i;%i;%i",1,CMD_SWEEP_SIGN, SweepSign) ;
215 SendCmd(Request, Param) ;
216 return ;
217 }*/
218
219
220 // Change la valeur de la base pour 'lincrement de frequence (sweep). Argument: delta T x 10ms, retourne rien.
221 void DDSFox_SetDT(DDSParameter *Param) {
222 char Request[255]; 237 char Request[255];
223 int dt= 0; 238 int dt= 0;
224 char FTW0[255]="0"; /**** sinon le fichier inf.xx est faux ***/ 239 char FTW0[255]="0"; /**** sinon le fichier inf.xx est faux ***/
225 dt = ((Param->Delta_T)/BASE_TIME) - 1 ; 240 dt = ((Param->Delta_T)/BASE_TIME) - 1 ;
226 sprintf(FTW0,"%ims",dt); 241 sprintf(FTW0,"%ims",dt);
227 sprintf(Request,"set;%i;%i;%i;%s",1,CMD_DT, dt ,FTW0); 242 sprintf(Request,"set;%i;%i;%i;%s",1,CMD_DT, dt ,FTW0);
228 SendCmd(Request, Param) ; 243 SendCmd(Request, Param) ;
229 return ; 244 return ;
230 } 245 }
231 246
232
233 // Change le facteur de division de la clock. Argument: facteur de div, retourne rien 247 // Change le facteur de division de la clock. Argument: facteur de div, retourne rien
234 void DDSFox_SetDiv(DDSParameter *Param) { 248 void DDSFox_SetDiv(DDSParameter *Param, int div)
235 char Request[255]; 249 {
236 int Div = DIV ; 250 char Request[255];
237 Command Rd; 251 Command Rd;
238 char temp; 252 char temp;
253
254 int Div = 0;
255 // Div = log2(div)
256 while ((div >>= 1) > 1)
257 Div++;
258
239 init_command(&Rd); 259 init_command(&Rd);
240 sprintf(Request,"get;%i;%i",1,CMD_CONFIG_REG2); 260 sprintf(Request,"get;%i;%i",1,CMD_CONFIG_REG2);
241 if (RecvCmd(&Rd, Request, Param) < 0) 261 if (RecvCmd(&Rd, Request, Param) < 0)
242 return; 262 return;
243 temp = atoi(Rd.value2) ; 263 temp = atoi(Rd.value2) ;
247 atoi(Rd.value1),temp,atoi(Rd.value3),atoi(Rd.value4)) ; 267 atoi(Rd.value1),temp,atoi(Rd.value3),atoi(Rd.value4)) ;
248 SendCmd(Request, Param) ; 268 SendCmd(Request, Param) ;
249 return ; 269 return ;
250 } 270 }
251 271
252 272 void DDSFox_SetDf(DDSParameter *Param)
253 273 {
254 void DDSFox_SetDf(DDSParameter *Param) {
255 char Request[255]; 274 char Request[255];
256 double word ; 275 double word ;
257 unsigned char *octets; 276 unsigned char *octets;
258 word = pow(2,48)*(fabs(Param->SweepRate)*Param->Delta_T/Param->Clock); 277 word = pow(2,48)*(fabs(Param->SweepRate)*Param->Delta_T/Param->Clock);
259 octets=malloc(6*sizeof(unsigned char)); 278 octets=malloc(6*sizeof(unsigned char));
263 SendCmd(Request, Param); 282 SendCmd(Request, Param);
264 free(octets); 283 free(octets);
265 return ; 284 return ;
266 } 285 }
267 286
268 int DDSFox_GetSweepSign(DDSParameter *Param) { 287 double DDSFox_ReadFreq(DDSParameter *Param)
269 if (Param->SweepRate >= 0) return(0); 288 {
270 else return(1);
271 }
272
273
274 double DDSFox_ReadFreq(DDSParameter *Param) {
275 char Request[255]; 289 char Request[255];
276 unsigned int OctetA, OctetB, OctetC, OctetD, OctetE, OctetF ; 290 unsigned int OctetA, OctetB, OctetC, OctetD, OctetE, OctetF ;
277 double FreqRead ; 291 double FreqRead ;
278 Command Rd; 292 Command Rd;
279 init_command(&Rd); 293 init_command(&Rd);
289 FreqRead = 1099511627776.0 * OctetA + 4294967296.0 * OctetB + 16777216.0 * OctetC + 65536.0 * OctetD + 256.0 * OctetE + 1.0 * OctetF; 303 FreqRead = 1099511627776.0 * OctetA + 4294967296.0 * OctetB + 16777216.0 * OctetC + 65536.0 * OctetD + 256.0 * OctetE + 1.0 * OctetF;
290 FreqRead = FreqRead * Param->Clock / pow(2,48); 304 FreqRead = FreqRead * Param->Clock / pow(2,48);
291 return(FreqRead); 305 return(FreqRead);
292 } 306 }
293 307
294 308 // TCP communication
295 /************************ Fonction de communications TCP ******************/ 309
296 static const double timeout = 0.5; 310 static unsigned int timeout = 0; // milliseconds
297 311
298 312 int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData)
299 int SendCmd(char *Buffer, DDSParameter *Param) { 313 {
314 return 0;
315 }
316
317 static int SendCmd(char *Buffer, DDSParameter *Param)
318 {
300 unsigned int hConv=0; 319 unsigned int hConv=0;
301 char Response[255]; 320 char Response[255];
302 if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, 0) < 0) { 321 #ifdef DEBUG
322 fprintf(stderr, "> %s\n", Buffer);
323 #endif
324 if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, timeout) < 0) {
303 error("Connect failed"); 325 error("Connect failed");
304 return -1; 326 return -1;
305 } 327 }
306 if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0) < 0) { 328 if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, timeout) < 0) {
307 error("Write failed"); 329 error("Write failed");
308 return -1; 330 return -1;
309 } 331 }
310 sprintf(Response," "); 332 sprintf(Response," ");
311 if (ClientTCPRead (hConv, Response, 255, 0) < 0) { 333 if (ClientTCPRead (hConv, Response, 255, timeout) < 0) {
312 error("Read failed"); 334 error("Read failed");
313 return -1; 335 return -1;
314 } 336 }
337 #ifdef DEBUG
338 fprintf(stderr, "< %s\n", Response);
339 #endif
315 if (strncmp("OK",Response,2)) { 340 if (strncmp("OK",Response,2)) {
316 error("Response failed %s %s", Buffer, Response); 341 error("Response failed %s %s", Buffer, Response);
317 return -1; 342 return -1;
318 } 343 }
319 return 0; 344 return 0;
320 } 345 }
321 346
322 347 static int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param)
323 int RecvCmd(Command *Rd, char * Buffer, DDSParameter *Param) { 348 {
324 unsigned int hConv=0; 349 unsigned int hConv=0;
325 char parsebuf[255]; 350 char parsebuf[255];
326 char Response[255]; 351 char Response[255];
327 if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, 0) < 0) { 352 #ifdef DEBUG
353 fprintf(stderr, "> %s\n", Buffer);
354 #endif
355 if (ConnectToTCPServer (&hConv, Param->Port, Param->ip, OnTCPEvent, Response, timeout) < 0) {
328 error("Connect failed"); 356 error("Connect failed");
329 return -1; 357 return -1;
330 } 358 }
331 if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, 0) < 0) { 359 if (ClientTCPWrite (hConv, Buffer, strlen(Buffer)+1, timeout) < 0) {
332 error("Write failed"); 360 error("Write failed");
333 return -1; 361 return -1;
334 } 362 }
335 sprintf(Response," "); 363 sprintf(Response," ");
336 if (ClientTCPRead (hConv, Response, 255, 0) < 0) { 364 if (ClientTCPRead (hConv, Response, 255, timeout) < 0) {
337 error("Read failed"); 365 error("Read failed");
338 return -1; 366 return -1;
339 } 367 }
340 368 #ifdef DEBUG
369 fprintf(stderr, "< %s\n", Response);
370 #endif
341 if (strncmp(Buffer,Response,strlen(Buffer))) { 371 if (strncmp(Buffer,Response,strlen(Buffer))) {
342 error("Response failed %s %s", Buffer, Response); 372 error("Response failed %s %s", Buffer, Response);
343 return -1; 373 return -1;
344 } 374 }
345 375
346 sprintf(parsebuf,"%s",Response) ; 376 sprintf(parsebuf,"%s",Response) ;
347 parse_command(Rd, parsebuf) ; 377 parse_command(Rd, parsebuf) ;
348 return 0; 378 return 0;
349 } 379 }
350 380
351 381 static void init_command(Command *cmd)
352 int OnTCPEvent(unsigned handle, int xType, int errCode, void *callbackData) { 382 {
353 //char* Response = (char*)(callbackData); 383 memset(cmd, 0, sizeof(Command));
354 //char Response[255]; 384 }
355 //switch(xType) { 385
356 // case TCP_DISCONNECT : 386 static int parse_command(Command *cmd1, char *buf)
357 //printf("Server disconnected\n"); 387 {
358 // break;
359 // case TCP_DATAREADY :
360 //printf("Server data ready\n");
361 //ClientTCPRead (handle, Response, 255, 5);
362 // break;
363 //}
364 return 0;
365 }
366
367 /************************ Fonction de mise en forme des données pour le transfert ******************/
368
369 void parse_command2(int* cmd, char* buf)
370 {
371 char* pointer;
372 int c = 0;
373
374 pointer = strtok( buf, CMD_SEP );
375 while( pointer ){
376 if ((c > 2) && ( c < 19 ))
377 {
378 cmd[c-3] = atoi(pointer);
379 printf("%i", cmd[c-3]);
380 }
381
382
383 pointer = strtok( NULL, CMD_SEP );
384 c++;
385 }
386 }
387
388
389 int parse_command(Command *cmd1, char *buf){
390 char* pointer; 388 char* pointer;
391 int c = 0; 389 int c = 0;
392 390
393 pointer = strtok( buf, CMD_SEP ); 391 pointer = strtok( buf, CMD_SEP );
394 while( pointer ){ 392 while( pointer ){
429 c++; 427 c++;
430 } 428 }
431 return 0; 429 return 0;
432 } 430 }
433 431
434 432 // convert double into 6 bytes sent back by an unsigned char pointer
435 int init_command(Command *cmd1){ 433 static void convert_double_6char (double fraction, unsigned char* octets)
436 cmd1->action = NULL; 434 {
437 cmd1->canal = NULL; 435 // 64 bits integer
438 cmd1->command = NULL; 436 unsigned long long x = fraction;
439 cmd1->value0 = NULL; 437 for (int i = 0; i < 6; i++)
440 cmd1->value1 = NULL; 438 octets[5 - i] = (unsigned char)((x >> (i * 8)) & 0xFF);
441 cmd1->value2 = NULL; 439 }
442 cmd1->value3 = NULL;
443 cmd1->value4 = NULL;
444 cmd1->value5 = NULL;
445 cmd1->value6 = NULL;
446 cmd1->value7 = NULL;
447 cmd1->value8 = NULL;
448 cmd1->value9 = NULL;
449 cmd1->value10 = NULL;
450 cmd1->value11 = NULL;
451 cmd1->value12 = NULL;
452 cmd1->value13 = NULL;
453 cmd1->value14 = NULL;
454 cmd1->value15 = NULL;
455 cmd1->value16 = NULL;
456 cmd1->value17 = NULL;
457 cmd1->value18 = NULL;
458 cmd1->value19 = NULL;
459 return 0;
460 }
461
462
463 //Convert double into 6 bytes sent back by an unsigned char pointer
464 int convert_double_6char (double fraction, unsigned char* octets) {
465 double bita, bitb, bitc, bitd, bite, bitf;
466 bitf= fraction/(1099511627776.0);
467 *(octets+5)=(unsigned char)bitf;
468 bite= (fraction-1099511627776.0*(*(octets+5)))/(4294967296.0);
469 *(octets+4)=(unsigned char)bite;
470 bitd=(fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4)))/( 16777216.0);
471 *(octets+3)=(unsigned char)bitd;
472 bitc=(fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3)))/( 65536.0);
473 *(octets+2)=(unsigned char)bitc;
474 bitb= (fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3))-65536*(*(octets+2)))/ 256.0;
475 *(octets+1)=(unsigned char)bitb;
476 bita= (fraction-1099511627776.0*(*(octets+5))- 4294967296.0*(*(octets+4))-16777216.0*(*(octets+3))-65536*(*(octets+2))-256.0*(*(octets+1)))/ 1.0;
477 *(octets)=(unsigned char)bita;
478 return 0;
479 }