-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_commands.cpp
755 lines (643 loc) · 21.7 KB
/
server_commands.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include <sstream>
#include "server_functions.h"
#include "command_def.h"
#define BUFFERSIZE 1024
#define WP2_CONTROLLER_POLLUX 128
#define WMU_ASYNCMSG WM_USER+10
DWORD dwSuccess;
DWORD dwAxes;
DWORD dwControllerMode;
HINSTANCE hWp2CommDll;
char rotflag[4];
char initflag[10];
static HANDLE hcomm;
using namespace std;
typedef DWORD (__stdcall *pInitController)(DWORD CMode,DWORD Axes,DWORD Port, DWORD Baudrate,HWND UserWin,UINT UserMsg,DWORD Mode);
typedef DWORD (__stdcall *pOpenController)();
typedef DWORD (__stdcall *pCloseController)();
typedef DWORD (__stdcall *pExecuteCommand)(LPSTR pCommand, DWORD Lines, LPSTR pData);
int CMD_GETVERSION(Command cmd, SOCKET s, int NumParams, int NumOptions)
{
string msg;
msg = VERSION;
printf("Sending server version...\n");
SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return 0;
}
int CMD_PING(Command cmd, SOCKET s, int NumParams, int NumOptions)
{
string msg;
msg = PING_ANSWER;
printf("Responding to ping...\n");
SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return 0;
}
int CMD_PINGCOM(Command cmd, SOCKET s, int NumParams, int NumOptions)
{
string msg;
msg = PING_ANSWER;
printf("Responding to ping...\n");
SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return 0;
}
int CMD_OPEN(Command cmd, SOCKET s, int NumParams, int NumOptions)
{
string msg;
DWORD baud_rate=0;
DCB dcb;
COMMTIMEOUTS timeouts = {0};
DWORD err;
TCHAR lpBuffer[BUFFERSIZE];
pInitController Wp2CommInitController;
pOpenController Wp2CommOpenController;
hWp2CommDll=0;
dwAxes=1;
dwControllerMode=WP2_CONTROLLER_POLLUX;
baud_rate = (DWORD)atoi(cmd.params[1]);
// cmd.params: COM# Baud_rate flow_control
if(NumParams < 3)
{
printf("Error (OPEN): Missing parameters\n");
msg = "COM opening error: Missing parameters";
}
else
if(strcmp(cmd.params[3],"ROT") == 0)
{
strcpy(rotflag, "ROT");
hWp2CommDll=LoadLibrary("WP2COMM.DLL");
if (hWp2CommDll==NULL)
{
printf("Unable to load WP2COMM.DLL\n");
}
if (strcmp(initflag,"INITIATED") != 0)
{
Wp2CommInitController=(pInitController)GetProcAddress(hWp2CommDll,"InitController");
if (Wp2CommInitController==0)
{
printf("GetProcAddress-Error (InitController)\n");
msg = "RS-40 controller initialization error";
}
else
{
printf("Successfully initialized\n");
msg = "RS-40 controller successfully initialized";
}
(void) SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
char port[] = " ";
char *temp;
int portnum;
strncpy(port, cmd.params[0], 3);
temp = strtok(cmd.params[0],port);
portnum = atoi(temp);
dwSuccess=(*Wp2CommInitController)(dwControllerMode,dwAxes,portnum,baud_rate,0,0,1050884);
strcpy(initflag,"INITIATED");
}
Wp2CommOpenController=(pOpenController)GetProcAddress(hWp2CommDll,"OpenController");
dwSuccess=(*Wp2CommOpenController)();
if (dwSuccess)
{
msg = "RS-40 COM port opening error";
}
else
{
msg = "RS-40 COM port successfully opened";
err = 0;
}
}
else
{
// Fix for accessing COM port number higher than 9
char port[] = "\\\\.\\";
strcat(port, cmd.params[0]);
printf("COM port:\t %s\n", port);
// Get user-supplied Baud rate
baud_rate = (DWORD)atoi(cmd.params[1]);
printf("Baud rate:\t %lu\n", baud_rate);
// Check validy of supplied flow control setting
if((strcmp(cmd.params[2],"NONE") != 0) && (strcmp(cmd.params[2],"XON/XOFF") != 0) && (strcmp(cmd.params[2],"HARDWARE") != 0))
{
printf("Error (OPEN): Invalid flow control parameter\n");
msg = "COM opening error: Invalid flow control parameter";
}
else
{
// Create COM port handle
hcomm = CreateFile(port,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
err = GetLastError();
// Get COM port properties
COMMPROP commProp;
if(GetCommProperties(hcomm, &commProp) == FALSE)
{
err = GetLastError();
printf("GetCommProperties error code: %lu\n", err);
}
//printf("commprop.dwMaxBaud = %x\n", (int)commProp.dwMaxBaud);
//printf("commprop.dwSettableBaud = %x\n", (int)commProp.dwSettableBaud);
// Check validity of supplied Baud rate
if((commProp.dwSettableBaud & baud_rate) != baud_rate)
{
printf("Warning (OPEN): Invalid Baud rate parameter\n");
msg = "COM opening warning: Invalid Baud rate parameter";
}
// Check handle status
if(hcomm == INVALID_HANDLE_VALUE)
{
hcomm = NULL;
printf("Handle creation error code: %lu\n", err);
msg = "COM opening error";
}
else
{
// Set timeout settings
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if(SetCommTimeouts(hcomm,&timeouts) == FALSE)
{
err = GetLastError();
printf("Timeout setting error code: %lu\n", err);
msg = "COM opening error";
}
else
{
// Initialize DCB
FillMemory(&dcb,sizeof(dcb),0);
if(GetCommState(hcomm,&dcb) == FALSE)
{
err = GetLastError(); // Error in GetCommState
printf("DCB setting error code: %lu\n", err);
msg = "COM opening error";
}
// Setup DCB setting
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = baud_rate;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.fParity = FALSE;
// DCB settings for flow control
printf("Flow control:\t %s\n",cmd.params[2]);
if(strcmp(cmd.params[2],"XON/XOFF") == 0)
{
dcb.fOutX = TRUE; // Indicates whether XON/XOFF flow control is used during transmission
dcb.fInX = TRUE; // Indicates whether XON/XOFF flow control is used during reception
dcb.fOutxCtsFlow = FALSE; // If TRUE, the CTS (clear-to-send) signal is monitored for output flow control
dcb.fOutxDsrFlow = FALSE; // If TRUE, the DSR (data-set-ready) signal is monitored for output flow control
dcb.fDsrSensitivity = FALSE; // If TRUE, the communications driver is sensitive to the state of the DSR signal
dcb.fRtsControl = RTS_CONTROL_DISABLE; // RTS (request-to-send) flow control
dcb.fDtrControl = DTR_CONTROL_DISABLE; // DTR (data-terminal-ready) flow control
}
else if(strcmp(cmd.params[2],"HARDWARE") == 0)
{
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = TRUE;
dcb.fDsrSensitivity = TRUE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
}
else if(strcmp(cmd.params[2],"NONE") == 0)
{
}
// Set device control settings
if(SetCommState(hcomm, &dcb) == FALSE)
{
err = GetLastError();
printf("DCB setting error code: %lu\n", err);
msg = "COM opening error";
}
else
{
msg = "COM successfully opened";
}
}
}
}
}
// Get standard error message from system
if(err != 0)
{
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (OPEN): " << lpBuffer << endl;
}
else
{
cout << msg << endl;
}
// Send result back to client
//int i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
(void) SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return 0;
}
int CMD_CLOSE(Command cmd,SOCKET s,int NumParams,int NumOptions)
{
string msg;
BOOL status_closeCOM = FALSE;
DWORD err;
TCHAR lpBuffer[BUFFERSIZE];
pCloseController Wp2CommCloseController;
if(strcmp(rotflag,"ROT")==0)
{
Wp2CommCloseController=(pCloseController)GetProcAddress(hWp2CommDll,"CloseController");
dwSuccess=(*Wp2CommCloseController)();
if (dwSuccess)
{
msg = "RS-40 COM port closing error";
}
else
{
msg = "RS-40 COM port successfully closed";
}
}
else
{
status_closeCOM = CloseHandle(hcomm);
err = GetLastError();
// Check status
if(status_closeCOM == FALSE)
{
msg = "COM closing error";
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
// cout << "Error (CloseHandle): " << lpBuffer << endl;
}
else
{
msg = "COM successfully closed";
}
}
cout << msg << endl;
SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
rotflag[0]='\0';
return 0;
}
int CMD_CHECKCOM(Command cmd,SOCKET s,int NumParams,int NumOptions)
{
DCB dcb;
string msg;
DWORD err;
TCHAR lpBuffer[BUFFERSIZE];
BOOL status_checkCOM = FALSE;
status_checkCOM = GetCommState(hcomm,&dcb);
err = GetLastError();
if(status_checkCOM == FALSE)
{
msg = "COM handle error";
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (GetCommState): " << lpBuffer << endl;
}
else
{
msg = "COM handle OK";
}
cout << "COM checking status: " << msg << endl;
//int i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
(void) SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return 0;
}
int CMD_SENDRCV(Command cmd, SOCKET s, int NumParams, int NumOptions)
{
int i = 0;
string msg;
char WriteBuffer[BUFFERSIZE];
char ReadBuffer[BUFFERSIZE] = {0}; // BUFFERSIZE = 1024
BOOL status_writeCOM = FALSE;
BOOL status_readCOM = FALSE;
DWORD dwBytesToWrite = 0;
DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
DWORD err;
TCHAR lpBuffer[BUFFERSIZE];
pExecuteCommand Wp2CommExecuteCommand;
// Construct command message for device
strcpy(WriteBuffer, cmd.params[1]);
if(NumParams > 2)
{
for(i = 2; i < NumParams; i++)
{
strcat(WriteBuffer,cmd.params[i]);
}
}
// Get termination character(s) to be appended
char term_char[BUFFERSIZE];
strcpy(term_char, cmd.params[0]);
// Chop off EOL
char *cp;
if ((cp = strchr(term_char, '\r')) != 0)
{
*cp = 0;
}
if ((cp = strchr(term_char, '\n')) != 0)
{
*cp = 0;
}
if ((cp = strchr(term_char, ' ')) != 0)
{
*cp = 0;
}
// Put termination character(s) at end of message
if(strcmp(term_char, "CRLF") == 0)
{
strcat(WriteBuffer, "\r\n");
}
else if(strcmp(term_char, "LFCR") == 0)
{
strcat(WriteBuffer, "\n\r");
}
else if(strcmp(term_char, "LF") == 0)
{
strcat(WriteBuffer, "\n");
}
else if(strcmp(term_char, "CR") == 0)
{
strcat(WriteBuffer, "\r");
}
else if(strcmp(term_char, "NONE") == 0)
{
// Do nothing
}
else
{
printf("Error (SENDRCV): Invalid termination character(s)\n");
msg = "SENDRCV error: Invalid termination character(s)";
(void) SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
dwBytesToWrite = (DWORD) strlen(WriteBuffer);
printf("Command (size: %lu bytes): %s\n", dwBytesToWrite, WriteBuffer);
if(strcmp(rotflag,"ROT")==0)
{
Wp2CommExecuteCommand=(pExecuteCommand)GetProcAddress(hWp2CommDll,"ExecuteCommand");
dwSuccess=(*Wp2CommExecuteCommand)(strlwr(WriteBuffer),1,ReadBuffer);
msg = ReadBuffer;
dwBytesRead = (DWORD) strlen(ReadBuffer);
}
else
{
// Send command to COM port
status_writeCOM = WriteFile(hcomm, // open file handle
WriteBuffer, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL); // no overlapped structure
// Error check
err = GetLastError();
if(status_writeCOM == FALSE)
{
msg = "COM WriteFile error";
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (WriteFile): " << lpBuffer << endl;
// Send device message to client
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
else if(dwBytesWritten != dwBytesToWrite)
{
// This is an error because a synchronous write that results in
// success (WriteFile returns TRUE) should write all data as
// requested. This would not necessarily be the case for
// asynchronous writes.
printf("Error: dwBytesWritten != dwBytesToWrite\n");
// Send device message to client
msg = "COM WriteFile error";
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
else
{
printf("Wrote %lu bytes successfully\n", dwBytesWritten);
}
// Wait for device processing
Sleep(350);
// Receive message from COM port
status_readCOM = ReadFile(hcomm, // open file handle
ReadBuffer, // start of data to be read
BUFFERSIZE, // number of bytes to read
&dwBytesRead, // number of bytes that were read
NULL); // no overlapped structure
// Error check
err = GetLastError();
if(status_readCOM == FALSE)
{
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
// cout << "Error (ReadFile): " << lpBuffer << endl;
msg = "COM ReadFile error";
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
}
// Assuming the read data is ANSI text
if((dwBytesRead > 0) && (dwBytesRead <= BUFFERSIZE))
{
ReadBuffer[dwBytesRead+1]='\0'; // NULL character
printf("Data read (%lu bytes): %s\n",dwBytesRead,ReadBuffer);
// Send device message to client
ReadBuffer[dwBytesRead+1] = '\0';
msg = ReadBuffer;
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
}
else if(dwBytesRead == 0)
{
printf("No data read from COM port\n");
msg = "No data read from COM port";
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
}
else
{
msg = "COM ReadFile error";
printf("Error (ReadFile): Unexpected value for dwBytesRead\n");
}
return 0;
}
int CMD_SENDRCV_HEX(Command cmd, SOCKET s, int NumParams, int NumOptions)
{
int i = 0;
string msg;
unsigned char WriteBuffer[BUFFERSIZE] = {0};
unsigned char ReadBuffer[BUFFERSIZE] = {0}; // BUFFERSIZE = 256
BOOL status_writeCOM = FALSE;
BOOL status_readCOM = FALSE;
DWORD dwBytesToWrite = 0;
DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
DWORD err;
TCHAR lpBuffer[BUFFERSIZE];
// Remove trailing whitespaces
char *cp;
for(i = 0; i < NumParams; i++)
{
if ((cp = strchr(cmd.params[i], '\r')) != 0)
{
*cp = 0;
}
if ((cp = strchr(cmd.params[i], '\n')) != 0)
{
*cp = 0;
}
if ((cp = strchr(cmd.params[i], ' ')) != 0)
{
*cp = 0;
}
}
// Construct command message for device
if(NumParams > 2)
{
for(i = 0; i < NumParams; i++)
{
WriteBuffer[i] = strtoul(cmd.params[i], NULL, 16);
}
}
// DEBUG
printf("NumParams = %d\n", NumParams);
for(i = 0; i < NumParams ; i++)
{
printf("WriteBuffer %d = %s \n", i, cmd.params[i]);
}
dwBytesToWrite = (DWORD) (NumParams);
printf("Command (size: %lu bytes): ", dwBytesToWrite);
for(i = 0; i < (NumParams); i++)
{
printf("%X (%s)", WriteBuffer[i], cmd.params[i]);
}
printf("\n");
// Send command to COM port
status_writeCOM = WriteFile(hcomm, // open file handle
WriteBuffer, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL); // no overlapped structure
// Error check
err = GetLastError();
if(status_writeCOM == FALSE)
{
msg = "COM WriteFile error";
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (WriteFile): " << lpBuffer << endl;
// Send device message to client
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
else if(dwBytesWritten != dwBytesToWrite)
{
// This is an error because a synchronous write that results in
// success (WriteFile returns TRUE) should write all data as
// requested. This would not necessarily be the case for
// asynchronous writes.
printf("Error: dwBytesWritten != dwBytesToWrite\n");
// Send device message to client
msg = "COM WriteFile error";
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
else
{
printf("Wrote %lu bytes successfully\n", dwBytesWritten);
}
// Wait for device processing
Sleep(350);
// Receive message from COM port
status_readCOM = ReadFile(hcomm, // open file handle
ReadBuffer, // start of data to be read
BUFFERSIZE, // number of bytes to read
&dwBytesRead, // number of bytes that were read
NULL); // no overlapped structure
// Error check
err = GetLastError();
if(status_readCOM == FALSE)
{
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
BUFFERSIZE-1, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
// cout << "Error (ReadFile): " << lpBuffer << endl;
msg = "COM ReadFile error";
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
return -1;
}
// Assuming the read data is ANSI text
if((dwBytesRead > 0) && (dwBytesRead <= BUFFERSIZE))
{
unsigned int m;
char answer[40]={'\0'};
char temp[3];
temp[0] = '\0';
ReadBuffer[dwBytesRead+1]='\0'; // NULL character
printf("Data read (%lu bytes)\n",dwBytesRead);
for(m = 0; m < dwBytesRead; m++)
{
printf("ReadBuffer %02X\n", ReadBuffer[m]);
sprintf(temp, "%02X", ReadBuffer[m]);
strcat(answer, temp);
}
// Send device message to client
ReadBuffer[dwBytesRead+1] = '\0';
printf("Answer: %s\n", answer);
msg = answer;
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
}
else if(dwBytesRead == 0)
{
printf("No data read from COM port\n");
msg = "No data read from COM port";
i = SndLine(s,msg.data(),msg.length(),DEFAULT_SEND_METHOD);
}
else
{
msg = "COM ReadFile error";
printf("Error (ReadFile): Unexpected value for dwBytesRead\n");
}
return 0;
}