Mercurial > hg > fxanalyse
diff 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 |
line wrap: on
line diff
--- a/kk-data-provider.c Mon Apr 08 16:15:44 2013 +0200 +++ b/kk-data-provider.c Mon Apr 08 16:18:05 2013 +0200 @@ -11,6 +11,7 @@ #define CONFIGFILE "FXAnalise.ini" #define DEFAULTPORT "COM4:115200" +#define TIMEOUT 1 /* seconds */ #define strneq(a, b, len) (strncmp((a), (b), (len)) == 0) @@ -37,7 +38,7 @@ /* get serial port name configuration */ rv = Ini_GetStringIntoBuffer(configuration, "KK", "port", port, sizeof(port)); - if (rv == 0) + if (! rv) strncpy(port, DEFAULTPORT, sizeof(port)); /* free */ @@ -45,30 +46,83 @@ /* initialize library */ FX_Init(); + /* connect to KK FX80E counter */ rv = FX_Open(port); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } /* clear transmit buffer */ - FX_Send("\x80"); - FX_Recv(&resp); + rv = FX_Send("\x80"); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + rv = FX_Recv(&resp, TIMEOUT); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + /* set report interval 1sec */ - FX_Send("\x29"); - FX_Recv(&resp); + rv = FX_Send("\x29"); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + rv = FX_Recv(&resp, TIMEOUT); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + /* read 4 channels */ - FX_Send("\x34"); - FX_Recv(&resp); + rv = FX_Send("\x34"); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + rv = FX_Recv(&resp, TIMEOUT); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + /* set mode to instantaneous frequency measurement */ - FX_Send("\x42"); - FX_Recv(&resp); + rv = FX_Send("\x42"); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + rv = FX_Recv(&resp, TIMEOUT); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + /* switch scrambler off */ - FX_Send("\x50"); - FX_Recv(&resp); + rv = FX_Send("\x50"); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } + rv = FX_Recv(&resp, TIMEOUT); + if (! rv) { + SendMessage(mainThreadId, FX_Error()); + goto error; + } while (acquiring) { /* receive data from counter */ - FX_Recv(&resp); + FX_Recv(&resp, TIMEOUT); + if (! resp) { + SendMessage(mainThreadId, FX_Error()); + break; + } - if (strneq(resp, "2900;", 5)) { + if (strneq(resp, "2900", 4)) { /* timestamp */ gettimeofday(&event.time, NULL); @@ -86,18 +140,25 @@ /* push data into the data queue */ CmtWriteTSQData(dataQueue, &event, 1, TSQ_INFINITE_TIMEOUT, 0); - } else if (strneq(resp, "7000;", 5)) { + } else if (strneq(resp, "7000", 4)) { /* ignore heart beat packet */ - } else if (strneq(resp, "7015;", 5)) { + } else if (strneq(resp, "7015", 4)) { /* ignore undocumented packet. it is sent by newer kk counters - for each data packet. it contains a sample count along with - other information */ + for each data packet. it probably contains a sample count along + with some other information */ + } else if (strneq(resp, "7020", 4)) { + /* undocumented packet. it probably reports the header for + subsequent data packets. match it against expected value */ + if (strcmp(resp + 6, "$2900")) { + SendMessage(mainThreadId, "KK Counter: %s", resp); + } } else { /* send message to the main thread */ - SendMessage(mainThreadId, resp); + SendMessage(mainThreadId, "KK Counter: %s", resp); } } +error: /* close serial port */ FX_Close(); /* free allocated resources */