-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.cpp
376 lines (337 loc) · 7.92 KB
/
Server.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// File Example1Server.cpp
#include <litho.h>
#include <guimfc.h>
//#include <winsock2.h>
#include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <merlin_afm.h>
//#include <ctime>
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <sstream>
#include <string>
#include <vector>
#include <bitset>
#include <iostream>
#include <conio.h>
#include "my_globals.h"
using namespace std;
void DeleteContent(LPTSTR);
bool CheckAbort(LPTSTR);
bool RefreshDirectory(LPTSTR);
void RefreshTree(LPTSTR);
void WatchDirectory(LPTSTR);
void ParseFile(LPTSTR);
//Prototypen
int initWinsock(void);
int startWinsock(void);
int closeWinsock(void);
int connectWinsock(void);
int readWinsock(void);
int readAbort(void);
unsigned int __stdcall CheckKey( void *dummy );
//int writeWinsock(std::string);
template<typename C>
void split(string const&, char const*, C&);
bool b_shutdown = false; /* Global repeat flag */
bool b_abort = false;
volatile bool running = true;
long rc;
SOCKET ServerSocket;
SOCKET connectedSocket;
//SOCKADDR_IN addr;
/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
unsigned int __stdcall CheckKey( void *dummy )
{
int ch;
do
{
printf( "Type 'A' to abort or 'X' for shutdown\n" );
ch = _getch();
if (ch == 'A')
{
b_abort = true;
//Scriptabort
}
} while( (ch != 'X') && running);
b_shutdown = true; /* _endthread implied */
return 0;
}
template<typename C>
void split(string const& s, char const* d, C& ret)
{
C output;
bitset<255> delims;
while( *d )
{
unsigned char code = *d++;
delims[code] = true;
}
typedef string::const_iterator iter;
iter beg;
bool in_token = false;
for( string::const_iterator it = s.begin(), end = s.end();
it != end; ++it )
{
if( delims[*it] )
{
if( in_token )
{
output.push_back(typename C::value_type(beg, it));
in_token = false;
}
}
else if( !in_token )
{
beg = it;
in_token = true;
}
}
if( in_token )
output.push_back(typename C::value_type(beg, s.end()));
output.swap(ret);
}
// Server function.
//void InitStage(
extern "C" __declspec(dllexport) int macroMain()
{
b_shutdown = false;
b_abort = false;
initKeithley();
//HANDLE hThread = (HANDLE)_beginthread( &CheckKey, 0, 0 );
//HANDLE hThread = (HANDLE)_beginthreadex(0, 0, &CheckKey, 0, 0, 0);
LITHO_BEGIN;
//std::cout << "1 StageGetPosition X: " << StageGetPosition(0) <<std::endl; //X
//std::cout << "1 StageGetPosition Y: " << StageGetPosition(1) <<std::endl; //Y
//std::cout << "1 StageGetPosition Z: " << StageGetPosition(2) <<std::endl; //Z
//return 0;
if (1) {
LithoDisplayStatusBox();
bool stat = IsEngaged();
if (stat == 1)
{
LithoScan(FALSE);
}
LithoCenterXY();
//LithoPause(0.00000000001);
int ret = 0;
ret = initWinsock();
std::cout << "INIT: " << ret <<std::endl;
if (ret == 0)
{
//printf("16...\n");
//startWinsock();
ret = connectWinsock();
if (ret == 0)
{
printf("1...\n");
ret = 0;
while (running)
{
if (b_shutdown == true)
{
printf("22...\n");
break;
}
printf("2...\n");
if (ret == 1)
{
connectWinsock();
printf("3...\n");
//running = false;
//break;
}
printf("4...\n");
ret = readWinsock();
printf("5...\n");
if (ret == 2)
{
printf("6...\n");
running = false;
break;
}
if (ret == 1)
{
printf("14...\n");
//running = false;
//break;
}
printf("7...\n");
}
}
closeWinsock();
}
printf("8...\n");
}
printf("9...\n");
LITHO_END;
//CloseHandle(hThread);
printf("10...\n");
std::cout << std::endl << "END" << std::endl<< std::endl;
//KeithSetVoltage( float(0.0) );
KeithClose();
return 0;
}
void ParseScript()
{
printf("\nParsing Script...\n");
writeWinsock("\nParsing Script...\n");
printf("\n\n");
std::istringstream line(scriptbuf); //make a stream for the line itself
std::string str;
while (std::getline(line,str))
{
if (b_abort == true)
{
printf("21...\n");
b_abort = false;
writeWinsock("ABORT");
break;
return;
}
//std::vector<std::string> cmd = split(str, '\t');
char const* delims = " \t";
vector<std::string> cmd;
split(str, delims, cmd);
/*
std::cout << str <<":"<<std::endl;
std::cout << cmd.size() <<":"<<std::endl;*/
//std::string cmd;
writeWinsock(str);
if (cmd.size() == 0)
{
continue;
}
//std::cout << cmd[0] <<":"<<std::endl;
// LithoPause(0.00000000001);
if (cmd[0] == "xyAbs")
{
double x, y, r;
x = stod(cmd[1]);
y = stod(cmd[2]);
r = stod(cmd[3]);
//line >> x >> y >> rate; //now read the whitespace-separated floats
//std::cout << " moving to x: " << x <<" y: " <<y <<std::endl;
LithoTranslateAbsolute(x,y,r);
//std::cout << cmd << ":" << x << " " << y << " " << rate << std::endl;
}
else if (cmd[0] == "xy")
{
double x, y, rate;
x = stod(cmd[1]);
y = stod(cmd[2]);
rate = stod(cmd[3]);
//line >> x >> y >> rate; //now read the whitespace-separated floats
// std::cout << " moving to x: " << x <<" y: " <<y <<std::endl;
LithoTranslate(x,y,rate);
// std::cout << cmd << ":" << x << " " << y << " " << rate << std::endl;
}
//else if (cmd[0] == "vtip_swp")
//{
// double vtip;
// vtip = stod(cmd[1]);
// //line >> vtip; //now read the whitespace-separated floats
// KeithSweepVoltage(vtip,30);
// //LithoSet(lsAna2, vtip);
//}
else if (cmd[0] == "vtip")
{
float vtip;
vtip = stof(cmd[1]);
//line >> vtip; //now read the whitespace-separated floats
//KeithSetVoltage(vtip);
KeithSweepVoltage(vtip,30);
//LithoSet(lsAna2, vtip);
}
else if (cmd[0] == "trigger")
{
std::string chan;
chan = cmd[1];
//line >> chan; //now read the whitespace-separated floats
std::cout << cmd[0] << ":" << chan << std::endl;
}
else if (cmd[0] == "pulse")
{
std::string chan, value, time;
chan = cmd[1];
value = cmd[2];
time = cmd[3];
//line >> chan >> value >> time; //now read the whitespace-separated floats
std::cout << cmd[0] << ":" << chan << value << time << std::endl;
}
else if (cmd[0] == "signal")
{
std::string chan, value;
chan = cmd[1];
value = cmd[2];
//line >> chan >> value; //now read the whitespace-separated floats
std::cout << cmd[0] << ":" << chan << value << std::endl;
}
else if (cmd[0] == "setpoint")
{
double value;
value = stod(cmd[1]);
//line >> value; //now read the whitespace-separated floats
LithoSet(lsSetpoint, value);
std::cout << cmd[0] << ":" << "Setpoint " << value << std::endl;
}
else if (cmd[0] == "udp")
{
/*std::string send;
std::getline(line, send);*/
std::cout << cmd[0] << ":" << str << std::endl;
}
else if (cmd[0] == "getxy")
{
double posX = LithoGetXPosUM();
double posY = LithoGetYPosUM();
std::cout << cmd[0] << " x: " << posX << " y: " << posY << std::endl;
}
else if (cmd[0] == "pause")
{
float value;
value = stof(cmd[1]);
//line >> value;
std::cout << cmd[0] << " " << value << "s" << std::endl;
//Sleep(value);
LithoPause(value);
}
else if (cmd[0] == "wait")
{
// Here we need to wait for the scan to finish
}
else if (cmd[0] == "center")
{
LithoCenterXY();
double posX = LithoGetXPosUM();
double posY = LithoGetYPosUM();
std::cout << cmd[0] << " x: " << posX << " y: " << posY << std::endl;
}
else if (cmd[0] == "SketchScript")
{
//continue;
}
else if (cmd[0] == "#")
{
if (readAbort() != 0) {
writeWinsock("ABORT");
return;
}
}
else
{
if (str.empty()) {
// empty line
}
else {
std::cout << "unknown " << cmd[0] << ":" << str << std::endl;
}
}
}
writeWinsock("Ready");
}