-
Notifications
You must be signed in to change notification settings - Fork 3
/
SerialIO.cpp
603 lines (551 loc) · 16.9 KB
/
SerialIO.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
/****************************************************
*
* SerialIO.cpp
*
* --Educational Use Only
*
* This is a MEX-FILE for matlab.
* To compile type: "mex SerialIO.cpp" into prompt.
*
* After compiling, type:
* SerialIO('Help');
* for usage instructions.
*
* Credits:
* -Modified for by Lorgio Teodovich, november 2009
* Based on an example 'serial_write.c' from Kevin Barry
* Lorgiot@gmail.com
* -Modified for use with MATLAB by Jason Laska, April 2004
* http://www.uiuc.edu/~laska
* -Original code for SerialTerm (serial terminal program
* from which this was taken)
* by Albrecht Schmidt, Lancaster University - Oct 2001
* http://www.comp.lancs.ac.uk/~albrecht
* Albrecht@comp.lancs.ac.uk
* -SerialTerm was based on an example from Robert Mashlan
* see http://r2m.com/~rmashlan/
*
*****************************************************/
#include <windows.h>
#undef EXTERN_C
#include <process.h>
#include "mex.h"
#define Thread
#define WIN32_LEAN_AND_MEAN
//Global Declarations
#ifdef Thread
// Variables used to detect if the threads are detect RX
// (volatile: reload the variable instead of using the value available in a register)
static volatile int WaitForRX;
// Mutex used to lock WaitForRX variable, to allow only one thread to
// write it at the same time.
static HANDLE RX_Mutex;
static int EndWaitForRX; //exit of thread
static int QuitWaitForRX; //success exit of thread
HANDLE Thread1; // Handles to the worker threads
#endif
HANDLE hCom = NULL;
double *SerialData;
int NumPoints;
#ifdef Thread
// The function which is multi threaded.
void PolingWaitCommEvent(void *param)
{DWORD mask;
while( ! EndWaitForRX )
{
if(WaitForRX == 0){
WaitForSingleObject(RX_Mutex, INFINITE);
if(SetCommMask(hCom,EV_RXCHAR)){
if(WaitCommEvent(hCom,&mask, NULL)){
if( mask & EV_RXCHAR)
WaitForRX = 1 ;
}
}
ReleaseMutex(RX_Mutex);
}
Sleep(5);
}
QuitWaitForRX = 1 ;
// explicit end thread, helps to ensure proper recovery of resources allocated for the thread
_endthread();
}
#endif
void closeSerial()
{
if(!(hCom==NULL)){
#ifdef Thread
QuitWaitForRX = 0;
EndWaitForRX = 1;
while(!QuitWaitForRX)
Sleep(1);
#endif
CloseHandle(hCom);
#ifdef Thread
Sleep(1);
CloseHandle(RX_Mutex);
CloseHandle(Thread1);
#endif
hCom = NULL;
}
}
void help()
{
printf("Format1: \n");
printf(" s = SerialIO('open','port', baud);\n");
printf("Ports:\n");
printf(" 'com1', 'com2' \n");
printf("Bauds: \n");
printf(" 300, 4800, 9600, 19200, 38400, 57600, 115200, 230400\n");
printf("s: \n");
printf(" Is true if open success\n");
printf("**Note: 'port' must be entered in quotes. 8N1 \n");
printf("Default comunication in 8N1 \n");
printf("-----------------------------------------------------------\n\n");
printf("Format2: \n");
printf(" SerialIO('close');\n");
printf("-----------------------------------------------------------\n\n");
printf("Format3: \n");
printf(" y = SerialIO('read',Size);\n");
printf("Size:\n");
printf(" A Row Vector Size of your choice!\n");
printf("-----------------------------------------------------------\n\n");
printf("Format4: \n");
printf(" SerialIO('write',x,Size);\n");
printf("x:\n");
printf(" Vector to send\n");
printf("Size:\n");
printf(" A x Row Vector Size \n");
printf("-----------------------------------------------------------\n\n");
printf("Format5: \n");
printf(" SerialIO('clearRX');\n");
printf("Clear de input buffer\n");
printf("-----------------------------------------------------------\n\n");
printf("Format6: \n");
printf(" SerialIO('clearTX');\n");
printf("Clear de output buffer\n");
printf("-----------------------------------------------------------\n\n");
printf("Format7: \n");
printf(" SerialIO('setupcomm',InQueue,OutQueue);\n");
printf("Setup zise of input and output buffer\n");
#ifdef Thread
printf("-----------------------------------------------------------\n\n");
printf("Format8: \n");
printf(" s = SerialIO('stateRX');\n");
printf("s: \n");
printf(" Is true if char in RX buffer\n");
#endif
printf("-----------------------------------------------------------\n");
}
void PrintError(LPCSTR str)
{
LPVOID lpMessageBuffer;
int error = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
(LPTSTR) &lpMessageBuffer,
0,
NULL
);
printf("%s: (%d) %s\n\n",str,error,lpMessageBuffer);
help();
LocalFree( lpMessageBuffer );
}
void Receive(HANDLE h)
{
HANDLE hconn = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mask;
DWORD i;
OVERLAPPED ov;
int StopStream=0;
#ifdef Thread
WaitForSingleObject(RX_Mutex, INFINITE);
WaitForRX = 1 ;
ReleaseMutex(RX_Mutex);
#endif
//Initialization and Set-Up
ZeroMemory(&ov,sizeof(ov));
// create event for overlapped I/O
ov.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
if(ov.hEvent == INVALID_HANDLE_VALUE)
PrintError("E006_CreateEvent failed");
// wait for received characters
if(!SetCommMask(h,EV_RXCHAR))
PrintError("E007_SetCommMask failed");
//Get the Data
while(1)
{
// get the event mask
if( !WaitCommEvent(h,&mask,&ov) )
{
DWORD e = GetLastError();
if( e == ERROR_IO_PENDING )
{
DWORD r;
if( !GetOverlappedResult(h,&ov,&r,TRUE) )
{
PrintError("E008_GetOverlappedResult failed");
break;
}
}
else
{
PrintError("E009_WaitCommEvent failed");
break;
}
}
//There was an error getting the mask.
if(mask == 0)
{
break;
}
if( mask & EV_RXCHAR)
{
char buf[100];
DWORD read;
DWORD numread;
do
{
read = 0;
numread = NumPoints - StopStream;
if (numread>sizeof(buf))
numread=sizeof(buf);
if( !ReadFile(h,buf,numread,&read,&ov) )
{
if( GetLastError() == ERROR_IO_PENDING )
{
if( !GetOverlappedResult(h,&ov,&read,TRUE) )
{
PrintError("E010_GetOverlappedResult failed");
break;
}
}
else
{
PrintError("E011_ReadFile failed");
break;
}
}
//Read Data
for (i=0; i<read; i++)
{
//Write to matlab vector
if(StopStream < NumPoints)
{
SerialData[StopStream] = (unsigned char)buf[i];
StopStream++;
}
else
{
CloseHandle(ov.hEvent);
#ifdef Thread
WaitForSingleObject(RX_Mutex, INFINITE);
WaitForRX = 0 ;
ReleaseMutex(RX_Mutex);
#endif
return;
}
}
if(StopStream = NumPoints){
CloseHandle(ov.hEvent);
#ifdef Thread
WaitForSingleObject(RX_Mutex, INFINITE);
WaitForRX = 0 ;
ReleaseMutex(RX_Mutex);
#endif
return;
}
} while(StopStream < NumPoints);
}
//Clear Mask
mask = 0;
}
//Close the Event
CloseHandle(ov.hEvent);
#ifdef Thread
WaitForSingleObject(RX_Mutex, INFINITE);
WaitForRX = 0 ;
ReleaseMutex(RX_Mutex);
#endif
}
#ifdef Thread
int State_RXCHAR()
{int StateResult = 0;
WaitForSingleObject(RX_Mutex, INFINITE);
StateResult = WaitForRX;
ReleaseMutex(RX_Mutex);
return StateResult;
}
#endif
void openSerial(char *portname, int baud)
{
char portname_w32[255];
COMMCONFIG lpCC;
COMMTIMEOUTS lpTo;
int CBR_baud;
SerialData[0]=1;
// Win32 can't open >= COM10 with "COM10"
// Needs to be \\.\COM10
#ifndef _MSC_VER
sprintf(portname_w32, "\\\\.\\%s", portname);
#else
sprintf_s(portname_w32, "\\\\.\\%s", portname);
#endif
switch(baud) {
case 9600:
CBR_baud = CBR_9600;
break;
case 19200:
CBR_baud = CBR_19200;
break;
case 38400:
CBR_baud = CBR_38400;
break;
case 57600:
CBR_baud = CBR_57600;
break;
case 115200:
CBR_baud = CBR_115200;
break;
case 230400: // It seems Windows doesn't need CBR for these
case 460800:
default:
CBR_baud = baud;
break;
}
if (hCom != NULL) {
mexWarnMsgTxt("Already have an open port, closing first");
//closeSerial();
SerialData[0]=0;
}
hCom = CreateFile(portname_w32, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hCom == INVALID_HANDLE_VALUE) {
hCom = NULL;
SerialData[0]=0;
PrintError("E012_Failed to open port");
//mexErrMsgTxt("Could not open serial port");
}
mexAtExit(closeSerial);
GetCommState( hCom, &lpCC.dcb);
lpCC.dcb.BaudRate = CBR_baud;
lpCC.dcb.ByteSize = 8;
lpCC.dcb.StopBits = ONESTOPBIT;
lpCC.dcb.Parity = NOPARITY;
lpCC.dcb.fDtrControl = DTR_CONTROL_DISABLE;
lpCC.dcb.fRtsControl = RTS_CONTROL_DISABLE;
if(!SetCommState( hCom, &lpCC.dcb )){
SerialData[0]=0;
PrintError("E014_SetCommState failed");
}
GetCommTimeouts(hCom, &lpTo);
lpTo.ReadIntervalTimeout = 0;
lpTo.ReadTotalTimeoutMultiplier = 10;
lpTo.ReadTotalTimeoutConstant = 10;
lpTo.WriteTotalTimeoutMultiplier = 10;
lpTo.WriteTotalTimeoutConstant = 100;
if(!SetCommTimeouts(hCom, &lpTo))
PrintError("E013_SetCommTimeouts failed");
SetupComm(hCom, 2048, 2048);
#ifdef Thread
if(!(hCom==NULL)){
RX_Mutex = CreateMutex(NULL, FALSE, NULL);
EndWaitForRX = 0 ;
WaitForRX = 0 ;
Thread1 = (HANDLE)_beginthread( &PolingWaitCommEvent, 0, NULL );
}
#endif
}
int writeSerial(char *TXdata,int NumP) {
DWORD bytesWritten;
if (hCom == NULL)
mexErrMsgTxt("Cannot write. Open serial port first");
//printf("Writing Serial: [%s]\n", TXdata);
if ( WriteFile(hCom, TXdata, NumP, &bytesWritten, NULL) ) {
return bytesWritten;
} else {
char err_str[128];
#ifndef _MSC_VER
sprintf(err_str, "Could not write %d", GetLastError());
#else
sprintf_s(err_str, "Could not write %d", GetLastError());
#endif
mexErrMsgTxt(err_str);
}
return 0;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//Declarations
char *commandName;
int commandNum = 0;
char *In0;
int Length0;
mxArray *InArray;
double *xValues;
char *xCharData;
int Baud;
int ErrorParsec = 1;
DWORD InQueue, OutQueue;
//Convert the Inputs
if(nrhs >= 1){
commandName = (char *) mxArrayToString(prhs[0]);
if( (strcmp(commandName,"open")== 0)&&(nrhs == 3)&&(nlhs ==1))
{
//Get Port
if (mxIsChar(prhs[1]) != 1)
{
mexErrMsgTxt("Input 1 must be a string 'com1' or 'com2' ");
}
else
{
Length0 = mxGetN(prhs[1])+1;
In0 =(char *) mxCalloc(Length0, sizeof(char));
mxGetString(prhs[1],In0,Length0);
//Get Baud
Baud = (int)mxGetScalar(prhs[2]);
//Set up Output array
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
SerialData = mxGetPr(plhs[0]);
ErrorParsec = 0;
commandNum = 1;
}
}
if( (strcmp(commandName,"write")== 0)&&(nrhs = 3))
{
int i;
//Get Size
NumPoints = (int)mxGetScalar(prhs[2]);
InArray=(mxArray *)prhs[1];
xValues = mxGetPr(InArray);
xCharData= (char *) mxCalloc(NumPoints+1, sizeof(char));
for(i=0;i<NumPoints;i++)
{
xCharData[i]=(unsigned char)xValues[i];
}
if(nlhs==1){
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
SerialData = mxGetPr(plhs[0]);
}
ErrorParsec = 0;
commandNum = 2;
}
if( (strcmp(commandName,"read")== 0)&&(nrhs = 2)&&(nlhs ==1))
{
//Get Size
NumPoints = (int)mxGetScalar(prhs[1]);
//Set up Output array
plhs[0] = mxCreateDoubleMatrix(1,NumPoints,mxREAL);
SerialData = mxGetPr(plhs[0]);
ErrorParsec = 0;
commandNum = 3;
}
if( (strcmp(commandName,"close")== 0)&&(nrhs = 1))
{
ErrorParsec = 0;
commandNum = 4;
}
if( (strcmp(commandName,"clearRX")== 0)&&(nrhs = 1))
{
ErrorParsec = 0;
commandNum = 5;
}
if( (strcmp(commandName,"clearTX")== 0)&&(nrhs = 1))
{
ErrorParsec = 0;
commandNum = 6;
}
if( (strcmp(commandName,"setupcomm")== 0)&&(nrhs = 3))
{
InQueue = (DWORD)mxGetScalar(prhs[1]);
OutQueue = (DWORD)mxGetScalar(prhs[2]);
ErrorParsec = 0;
commandNum = 7;
}
#ifdef Thread
if( (strcmp(commandName,"stateRX")== 0)&&(nrhs == 1)&&(nlhs ==1))
{
//Set up Output array
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
SerialData = mxGetPr(plhs[0]);
ErrorParsec = 0;
commandNum = 8;
}
#endif
}
//I Dont know why, but the compiler needs this conditional here also.
//Set up Port and get Data
if((ErrorParsec==0)&&(commandNum>0))
{
if(commandNum==1){
openSerial(In0, Baud);
}
if(commandNum==2){
int BytesWritten;
BytesWritten = writeSerial(xCharData,NumPoints);
mxFree(xCharData);
if(nlhs==1){
SerialData[0] = BytesWritten;
}
}
if(commandNum==3){
if (hCom==NULL) {
mexErrMsgTxt("Cannot read. Open serial port first");
}
Receive(hCom);
}
if(commandNum==4){
if (hCom==NULL) {
mexErrMsgTxt("Failed to close port");
}
closeSerial();
}
if(commandNum==5){
if (hCom==NULL) {
mexErrMsgTxt("Failed to clear port");
}
#ifdef Thread
WaitForSingleObject(RX_Mutex, INFINITE);
#endif
PurgeComm(hCom, PURGE_RXABORT | PURGE_RXCLEAR);
#ifdef Thread
WaitForRX = 0 ;
ReleaseMutex(RX_Mutex);
#endif
}
if(commandNum==6){
if (hCom==NULL) {
mexErrMsgTxt("Failed to clear port");
}
PurgeComm(hCom, PURGE_TXABORT | PURGE_TXCLEAR);
}
if(commandNum==7){
if (hCom==NULL) {
mexErrMsgTxt("Failed to setup port");
}
SetupComm(hCom , InQueue , OutQueue);
}
#ifdef Thread
if(commandNum==8){
if (hCom==NULL) {
mexErrMsgTxt("Cannot read State. Open serial port first");
}
SerialData[0] = State_RXCHAR();
}
#endif
}
else
{
if(nlhs == 1)
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
printf(" \n");
printf("-------------- SerialIO -----------------\n");
printf(" Universidad Nacional de Tucuman \n");
printf(" \n");
help();
}
return;
}