comparison KKFX80E.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 c9e4f63c2033
children 3427013e4f70
comparison
equal deleted inserted replaced
101:8b90fbba59ef 102:4f1f353e84f5
1 #include <ctype.h> 1 #include <ctype.h>
2 #include <windows.h> 2 #include <windows.h>
3 #include <utility.h>
4 #include <ansi_c.h>
5
3 6
4 int (_stdcall *FX_OpenPort)(char **Port) = NULL; 7 int (_stdcall *FX_OpenPort)(char **Port) = NULL;
5 void (_stdcall *FX_ClosePort)(void) = NULL; 8 void (_stdcall *FX_ClosePort)(void) = NULL;
6 int (_stdcall *FX_SendCommand)(char **Command) = NULL; 9 int (_stdcall *FX_SendCommand)(char **Command) = NULL;
7 int (_stdcall *FX_GetReport)(char **Data) = NULL; 10 int (_stdcall *FX_GetReport)(char **Data) = NULL;
10 // void (_stdcall *Log)(int DbgNum) = NULL; 13 // void (_stdcall *Log)(int DbgNum) = NULL;
11 // int (_stdcall *ReadMonitor)(char **Data) = NULL; 14 // int (_stdcall *ReadMonitor)(char **Data) = NULL;
12 // BOOL (_stdcall *EnumeratePorts)(char **PortName, char *PortNamePrefix) = NULL; 15 // BOOL (_stdcall *EnumeratePorts)(char **PortName, char *PortNamePrefix) = NULL;
13 16
14 static HMODULE module; 17 static HMODULE module;
18 static char *errormsg;
19
20
21 char * FX_Error(void)
22 {
23 return errormsg;
24 }
15 25
16 26
17 int FX_Init(void) 27 int FX_Init(void)
18 { 28 {
19 FARPROC funcp; 29 FARPROC funcp = NULL;
30 errormsg = "";
20 31
21 module = LoadLibrary("KK_FX80E.dll"); 32 module = LoadLibrary("KK_FX80E.dll");
22 if (! module) 33 if (! module)
23 return 0; 34 return 0;
24 35
62 73
63 74
64 int FX_Open(char *port) 75 int FX_Open(char *port)
65 { 76 {
66 int rv = FX_OpenPort(&port); 77 int rv = FX_OpenPort(&port);
67 #ifdef DEBUG
68 if (! rv) 78 if (! rv)
69 fprintf(stderr, "%s\n", *port); 79 errormsg = port;
70 #endif
71 return rv; 80 return rv;
72 } 81 }
73 82
74 83
75 void FX_Close(void) 84 void FX_Close(void)
79 88
80 89
81 int FX_Send(char *cmd) 90 int FX_Send(char *cmd)
82 { 91 {
83 int rv = FX_SendCommand(&cmd); 92 int rv = FX_SendCommand(&cmd);
84 #ifdef DEBUG
85 if (! rv) 93 if (! rv)
86 fprintf(stderr, "%s\n", cmd); 94 errormsg = cmd;
87 #endif
88 return rv; 95 return rv;
89 } 96 }
90 97
91 98
92 char * FX_Recv(char **ret) 99 int FX_Recv(char **ret, int timeout)
93 { 100 {
94 int rv; 101 int rv;
95 char *data; 102 char *data;
103 double mark = Timer();
96 do { 104 do {
97 data = "."; 105 data = ".";
98 rv = FX_GetReport(&data); 106 rv = FX_GetReport(&data);
99 } while (! data); 107 if ((Timer() - mark) > timeout) {
100 #ifdef DEBUG 108 rv = 0;
101 if (! rv) 109 data = "Function FX_Recv: Timeout";
102 fprintf(stderr, "%s\n", data); 110 }
103 #endif 111 } while ((! data) && (rv == 1));
104 if (! rv) 112
105 return NULL; 113 if (! rv) {
114 errormsg = data;
115 data = NULL;
116 }
117
106 if (ret) 118 if (ret)
107 *ret = data; 119 *ret = data;
108 120
109 return data; 121 return rv;
110 } 122 }