-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMES.h
316 lines (268 loc) · 8.59 KB
/
MES.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef MES_H
#define MES_H
#include "OSMisc.h"
#include "RS232Port.h"
#ifndef DISABLE_MESTHREAD
#include "OSThread.h"
#endif // !DISABLE_MESTHREAD
#define TIMEOUT_MESSAGE_MES 4.0 // In s.
// Should be at least 2 * number of bytes to be sure to contain entirely the biggest desired message (or group of messages) + 1.
#define MAX_NB_BYTES_MES 24
#define MESSAGE_LEN_MES 10
struct MES
{
RS232PORT RS232Port;
FILE* pfSaveFile; // Used to save raw data, should be handled specifically...
double LastDistance;
char szCfgFilePath[256];
// Parameters.
char szDevPath[256];
int BaudRate;
int timeout;
int threadperiod;
BOOL bSaveRawData;
};
typedef struct MES MES;
// If this function succeeds, the beginning of buf contains a valid message
// but there might be other data at the end.
inline int AnalyzeMESMessage(char* str, int len)
{
// Check number of bytes.
if (len != MESSAGE_LEN_MES)
{
//printf("Invalid number of bytes.\n");
return EXIT_FAILURE;
}
// Check unit.
if (str[MESSAGE_LEN_MES-3] != 'm')
{
//printf("Invalid unit.\n");
return EXIT_FAILURE;
}
// Check CR.
if (str[MESSAGE_LEN_MES-2] != '\r')
{
//printf("Invalid CR.\n");
return EXIT_FAILURE;
}
// Check LF.
if (str[MESSAGE_LEN_MES-1] != '\n')
{
//printf("Invalid LF.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// If this function succeeds, the beginning of foundstr should contain a valid message
// but there might be other data at the end. Data in the beginning of str might have been discarded.
inline char* FindMESMessage(char* str)
{
char* foundstr = str;
int len = (int)strlen(str);
while (AnalyzeMESMessage(foundstr, len) != EXIT_SUCCESS)
{
foundstr++;
len--;
if (len < MESSAGE_LEN_MES)
{
// Could not find the message.
return NULL;
}
}
return foundstr;
}
inline char* FindLatestMESMessage(char* str)
{
char* ptr = NULL;
char* foundstr = NULL;
ptr = FindMESMessage(str);
while (ptr)
{
// Save the position of the beginning of the message.
foundstr = ptr;
// Search just after the beginning of the message.
ptr = FindMESMessage(foundstr+1);
}
return foundstr;
}
// We suppose that read operations return when a message has just been completely sent, and not randomly.
inline int GetLatestDataMES(MES* pMES, double* pDistance)
{
char recvbuf[2*MAX_NB_BYTES_MES];
char savebuf[MAX_NB_BYTES_MES];
int BytesReceived = 0, Bytes = 0, recvbuflen = 0;
char* ptr = NULL;
CHRONO chrono;
StartChrono(&chrono);
// Prepare the buffers.
memset(recvbuf, 0, sizeof(recvbuf));
memset(savebuf, 0, sizeof(savebuf));
recvbuflen = MAX_NB_BYTES_MES-1; // The last character must be a 0 to be a valid string for sscanf.
BytesReceived = 0;
if (ReadRS232Port(&pMES->RS232Port, (unsigned char*)recvbuf, recvbuflen, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a MES. \n");
return EXIT_FAILURE;
}
if ((pMES->bSaveRawData)&&(pMES->pfSaveFile))
{
fwrite(recvbuf, Bytes, 1, pMES->pfSaveFile);
fflush(pMES->pfSaveFile);
}
BytesReceived += Bytes;
if (BytesReceived >= recvbuflen)
{
// If the buffer is full and if the device always sends data, there might be old data to discard...
while (Bytes == recvbuflen)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_MES)
{
printf("Error reading data from a MES : Message timeout. \n");
return EXIT_TIMEOUT;
}
memcpy(savebuf, recvbuf, Bytes);
if (ReadRS232Port(&pMES->RS232Port, (unsigned char*)recvbuf, recvbuflen, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a MES. \n");
return EXIT_FAILURE;
}
if ((pMES->bSaveRawData)&&(pMES->pfSaveFile))
{
fwrite(recvbuf, Bytes, 1, pMES->pfSaveFile);
fflush(pMES->pfSaveFile);
}
BytesReceived += Bytes;
}
// The desired message should be among all the data gathered, unless there was
// so many other messages sent after that the desired message was in the
// discarded data, or we did not wait enough...
memmove(recvbuf+recvbuflen-Bytes, recvbuf, Bytes);
memcpy(recvbuf, savebuf+Bytes, recvbuflen-Bytes);
// Only the last recvbuflen bytes received should be taken into account in what follows.
BytesReceived = recvbuflen;
}
// The data need to be analyzed and we must check if we need to get more data from
// the device to get the desired message.
// But normally we should not have to get more data unless we did not wait enough
// for the desired message...
ptr = FindLatestMESMessage(recvbuf);
while (!ptr)
{
if (GetTimeElapsedChronoQuick(&chrono) > TIMEOUT_MESSAGE_MES)
{
printf("Error reading data from a MES : Message timeout. \n");
return EXIT_TIMEOUT;
}
// The last character must be a 0 to be a valid string for sscanf.
if (BytesReceived >= 2*MAX_NB_BYTES_MES-1)
{
printf("Error reading data from a MES : Invalid data. \n");
return EXIT_INVALID_DATA;
}
if (ReadRS232Port(&pMES->RS232Port, (unsigned char*)recvbuf+BytesReceived, 2*MAX_NB_BYTES_MES-1-BytesReceived, &Bytes) != EXIT_SUCCESS)
{
printf("Error reading data from a MES. \n");
return EXIT_FAILURE;
}
if ((pMES->bSaveRawData)&&(pMES->pfSaveFile))
{
fwrite((unsigned char*)recvbuf+BytesReceived, Bytes, 1, pMES->pfSaveFile);
fflush(pMES->pfSaveFile);
}
BytesReceived += Bytes;
ptr = FindLatestMESMessage(recvbuf);
}
// Analyze data.
if (sscanf(ptr, "%lfm\r\n", pDistance) != 1)
{
printf("Error reading data from a MES : Invalid data. \n");
return EXIT_FAILURE;
}
pMES->LastDistance = *pDistance;
return EXIT_SUCCESS;
}
// MES must be initialized to 0 before (e.g. MES mes; memset(&mes, 0, sizeof(MES));)!
inline int ConnectMES(MES* pMES, char* szCfgFilePath)
{
FILE* file = NULL;
char line[256];
memset(pMES->szCfgFilePath, 0, sizeof(pMES->szCfgFilePath));
sprintf(pMES->szCfgFilePath, "%.255s", szCfgFilePath);
// If szCfgFilePath starts with "hardcoded://", parameters are assumed to be already set in the structure,
// otherwise it should be loaded from a configuration file.
if (strncmp(szCfgFilePath, "hardcoded://", strlen("hardcoded://")) != 0)
{
memset(line, 0, sizeof(line));
// Default values.
memset(pMES->szDevPath, 0, sizeof(pMES->szDevPath));
sprintf(pMES->szDevPath, "COM1");
pMES->BaudRate = 115200;
pMES->timeout = 500;
pMES->threadperiod = 100;
pMES->bSaveRawData = 1;
// Load data from a file.
file = fopen(szCfgFilePath, "r");
if (file != NULL)
{
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%255s", pMES->szDevPath) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMES->BaudRate) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMES->timeout) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMES->threadperiod) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMES->bSaveRawData) != 1) printf("Invalid configuration file.\n");
if (fclose(file) != EXIT_SUCCESS) printf("fclose() failed.\n");
}
else
{
printf("Configuration file not found.\n");
}
}
if (pMES->threadperiod < 0)
{
printf("Invalid parameter : threadperiod.\n");
pMES->threadperiod = 100;
}
// Used to save raw data, should be handled specifically...
//pMES->pfSaveFile = NULL;
pMES->LastDistance = 0;
if (OpenRS232Port(&pMES->RS232Port, pMES->szDevPath) != EXIT_SUCCESS)
{
printf("Unable to connect to a MES.\n");
return EXIT_FAILURE;
}
if (SetOptionsRS232Port(&pMES->RS232Port, pMES->BaudRate, NOPARITY, FALSE, 8,
ONESTOPBIT, (UINT)pMES->timeout) != EXIT_SUCCESS)
{
printf("Unable to connect to a MES.\n");
CloseRS232Port(&pMES->RS232Port);
return EXIT_FAILURE;
}
printf("MES connected.\n");
return EXIT_SUCCESS;
}
inline int DisconnectMES(MES* pMES)
{
if (CloseRS232Port(&pMES->RS232Port) != EXIT_SUCCESS)
{
printf("MES disconnection failed.\n");
return EXIT_FAILURE;
}
printf("MES disconnected.\n");
return EXIT_SUCCESS;
}
#ifndef DISABLE_MESTHREAD
THREAD_PROC_RETURN_VALUE MESThread(void* pParam);
#endif // !DISABLE_MESTHREAD
#endif // !MES_H