# HG changeset patch # User Daniele Nicolodi # Date 1365430685 -7200 # Node ID 4f1f353e84f5858dd19613e3ebd23d4a5d64ca07 # Parent 8b90fbba59effcdcc0777c005c4290bdb95f47fd Add timeout to FX_Recv() function. Improve error handling in KKDataProvider diff -r 8b90fbba59ef -r 4f1f353e84f5 KKFX80E.c --- a/KKFX80E.c Mon Apr 08 16:15:44 2013 +0200 +++ b/KKFX80E.c Mon Apr 08 16:18:05 2013 +0200 @@ -1,5 +1,8 @@ #include #include +#include +#include + int (_stdcall *FX_OpenPort)(char **Port) = NULL; void (_stdcall *FX_ClosePort)(void) = NULL; @@ -12,11 +15,19 @@ // BOOL (_stdcall *EnumeratePorts)(char **PortName, char *PortNamePrefix) = NULL; static HMODULE module; +static char *errormsg; + + +char * FX_Error(void) +{ + return errormsg; +} int FX_Init(void) { - FARPROC funcp; + FARPROC funcp = NULL; + errormsg = ""; module = LoadLibrary("KK_FX80E.dll"); if (! module) @@ -64,10 +75,8 @@ int FX_Open(char *port) { int rv = FX_OpenPort(&port); -#ifdef DEBUG if (! rv) - fprintf(stderr, "%s\n", *port); -#endif + errormsg = port; return rv; } @@ -81,30 +90,33 @@ int FX_Send(char *cmd) { int rv = FX_SendCommand(&cmd); -#ifdef DEBUG if (! rv) - fprintf(stderr, "%s\n", cmd); -#endif + errormsg = cmd; return rv; } -char * FX_Recv(char **ret) +int FX_Recv(char **ret, int timeout) { int rv; char *data; + double mark = Timer(); do { data = "."; rv = FX_GetReport(&data); - } while (! data); -#ifdef DEBUG - if (! rv) - fprintf(stderr, "%s\n", data); -#endif - if (! rv) - return NULL; + if ((Timer() - mark) > timeout) { + rv = 0; + data = "Function FX_Recv: Timeout"; + } + } while ((! data) && (rv == 1)); + + if (! rv) { + errormsg = data; + data = NULL; + } + if (ret) *ret = data; - return data; + return rv; } diff -r 8b90fbba59ef -r 4f1f353e84f5 KKFX80E.h --- a/KKFX80E.h Mon Apr 08 16:15:44 2013 +0200 +++ b/KKFX80E.h Mon Apr 08 16:18:05 2013 +0200 @@ -16,7 +16,8 @@ int FX_Open(char *port); void FX_Close(void); int FX_Send(char *cmd); -char * FX_Recv(char **data); +int FX_Recv(char **data, int timeout); +char * FX_Error(void); #ifdef __cplusplus } diff -r 8b90fbba59ef -r 4f1f353e84f5 kk-data-provider.c --- 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 */