1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/*
* Tclwinserial.c --
*
* This file implements the Windows-specific serial port functions,
* and the "serial" channel driver.
*
* Copyright (c) 1999 by Scriptics Corp.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
* Changes by Rolf.Schroedter@dlr.de June 25-27, 1999
*
* RCS: @(#) $Id: tclWinSerial.c,v 1.8 1999/10/05 22:47:05 hobbs Exp $
*/
#include "tclWinInt.h"
#include <dos.h>
#include <fcntl.h>
#include <io.h>
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/*
* Tclwinserial.c --
*
* This file implements the Windows-specific serial port functions,
* and the "serial" channel driver.
*
* Copyright (c) 1999 by Scriptics Corp.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
* Changes by Rolf.Schroedter@dlr.de June 25-27, 1999
*
* RCS: @(#) $Id: tclWinSerial.c,v 1.9 1999/11/24 20:55:17 hobbs Exp $
*/
#include "tclWinInt.h"
#include <dos.h>
#include <fcntl.h>
#include <io.h>
|
| ︙ | | | ︙ | |
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
|
* one time after write operation */
/*
* Default time to block between checking status on the serial port.
*/
#define SERIAL_DEFAULT_BLOCKTIME 10 /* 10 msec */
/*
* This structure describes per-instance data for a serial based channel.
*/
typedef struct SerialInfo {
HANDLE handle;
struct SerialInfo *nextPtr; /* Pointer to next registered serial. */
Tcl_Channel channel; /* Pointer to channel structure. */
int validMask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which operations are valid on the file. */
int watchMask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which events should be reported. */
int flags; /* State flags, see above for a list. */
int writable; /* flag that the channel is readable */
int readable; /* flag that the channel is readable */
int blockTime; /* max. blocktime in msec */
} SerialInfo;
typedef struct ThreadSpecificData {
/*
* The following pointer refers to the head of the list of serials
* that are being watched for file events.
*/
|
>
>
>
>
>
>
>
>
>
>
>
|
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
|
* one time after write operation */
/*
* Default time to block between checking status on the serial port.
*/
#define SERIAL_DEFAULT_BLOCKTIME 10 /* 10 msec */
/*
* Define Win32 read/write error masks returned by ClearCommError()
*/
#define SERIAL_READ_ERRORS ( CE_RXOVER | CE_OVERRUN | CE_RXPARITY \
| CE_FRAME | CE_BREAK )
#define SERIAL_WRITE_ERRORS ( CE_TXFULL )
/*
* This structure describes per-instance data for a serial based channel.
*/
typedef struct SerialInfo {
HANDLE handle;
struct SerialInfo *nextPtr; /* Pointer to next registered serial. */
Tcl_Channel channel; /* Pointer to channel structure. */
int validMask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which operations are valid on the file. */
int watchMask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
* which events should be reported. */
int flags; /* State flags, see above for a list. */
int writable; /* flag that the channel is readable */
int readable; /* flag that the channel is readable */
int blockTime; /* max. blocktime in msec */
DWORD error; /* pending error code returned by
* ClearCommError() */
DWORD lastError; /* last error code, can be fetched with
* fconfigure chan -lasterror */
} SerialInfo;
typedef struct ThreadSpecificData {
/*
* The following pointer refers to the head of the list of serials
* that are being watched for file events.
*/
|
| ︙ | | | ︙ | |
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
ClientData data, /* Not used. */
int flags) /* Event flags as passed to Tcl_DoOneEvent. */
{
SerialInfo *infoPtr;
SerialEvent *evPtr;
int needEvent;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
DWORD cError;
COMSTAT cStat;
if (!(flags & TCL_FILE_EVENTS)) {
return;
}
/*
|
<
|
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
ClientData data, /* Not used. */
int flags) /* Event flags as passed to Tcl_DoOneEvent. */
{
SerialInfo *infoPtr;
SerialEvent *evPtr;
int needEvent;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
COMSTAT cStat;
if (!(flags & TCL_FILE_EVENTS)) {
return;
}
/*
|
| ︙ | | | ︙ | |
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
|
/*
* If any READABLE or WRITABLE watch mask is set
* call ClearCommError to poll cbInQue,cbOutQue
* Window errors are ignored here
*/
if( infoPtr->watchMask & (TCL_WRITABLE | TCL_READABLE) ) {
if( ClearCommError( infoPtr->handle, &cError, &cStat ) ) {
/*
* Look for empty output buffer. If empty, poll.
*/
if( infoPtr->watchMask & TCL_WRITABLE ) {
if( ((infoPtr->flags & SERIAL_WRITE) != 0) && \
(cStat.cbOutQue == 0) ) {
/*
* allow only one fileevent after each callback
*/
infoPtr->flags &= ~SERIAL_WRITE;
infoPtr->writable = 1;
needEvent = 1;
}
}
/*
* Look for characters already pending in windows queue.
* If they are, poll.
*/
if( infoPtr->watchMask & TCL_READABLE ) {
if( cStat.cbInQue > 0 ) {
infoPtr->readable = 1;
needEvent = 1;
}
}
}
}
|
|
>
>
>
|
|
>
>
>
>
|
>
|
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
|
/*
* If any READABLE or WRITABLE watch mask is set
* call ClearCommError to poll cbInQue,cbOutQue
* Window errors are ignored here
*/
if( infoPtr->watchMask & (TCL_WRITABLE | TCL_READABLE) ) {
if( ClearCommError( infoPtr->handle, &infoPtr->error, &cStat ) ) {
/*
* Look for empty output buffer. If empty, poll.
*/
if( infoPtr->watchMask & TCL_WRITABLE ) {
/*
* force fileevent after serial write error
*/
if (((infoPtr->flags & SERIAL_WRITE) != 0) &&
((cStat.cbOutQue == 0) ||
(infoPtr->error & SERIAL_WRITE_ERRORS))) {
/*
* allow only one fileevent after each callback
*/
infoPtr->flags &= ~SERIAL_WRITE;
infoPtr->writable = 1;
needEvent = 1;
}
}
/*
* Look for characters already pending in windows queue.
* If they are, poll.
*/
if( infoPtr->watchMask & TCL_READABLE ) {
/*
* force fileevent after serial read error
*/
if( (cStat.cbInQue > 0) ||
(infoPtr->error & SERIAL_READ_ERRORS) ) {
infoPtr->readable = 1;
needEvent = 1;
}
}
}
}
|
| ︙ | | | ︙ | |
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
|
int bufSize, /* How much space is available
* in the buffer? */
int *errorCode) /* Where to store error code. */
{
SerialInfo *infoPtr = (SerialInfo *) instanceData;
DWORD bytesRead = 0;
DWORD err;
DWORD cError;
COMSTAT cStat;
*errorCode = 0;
/*
* Look for characters already pending in windows queue.
* This is the mainly restored good old code from Tcl8.0
*/
if( ClearCommError( infoPtr->handle, &cError, &cStat ) ) {
/*
* Check for errors here, but not in the evSetup/Check procedures
*/
if( cError != 0 ) {
*errorCode = EIO;
return -1;
}
if( infoPtr->flags & SERIAL_ASYNC ) {
/*
* NON_BLOCKING mode:
* Avoid blocking by reading more bytes than available
* in input buffer
*/
|
<
>
>
>
>
>
>
>
|
|
|
<
|
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
|
int bufSize, /* How much space is available
* in the buffer? */
int *errorCode) /* Where to store error code. */
{
SerialInfo *infoPtr = (SerialInfo *) instanceData;
DWORD bytesRead = 0;
DWORD err;
COMSTAT cStat;
*errorCode = 0;
/*
* Check if there is a CommError pending from SerialCheckProc
*/
if( infoPtr->error & SERIAL_READ_ERRORS ){
goto commError;
}
/*
* Look for characters already pending in windows queue.
* This is the mainly restored good old code from Tcl8.0
*/
if( ClearCommError( infoPtr->handle, &infoPtr->error, &cStat ) ) {
/*
* Check for errors here, but not in the evSetup/Check procedures
*/
if( infoPtr->error & SERIAL_READ_ERRORS ) {
goto commError;
}
if( infoPtr->flags & SERIAL_ASYNC ) {
/*
* NON_BLOCKING mode:
* Avoid blocking by reading more bytes than available
* in input buffer
*/
|
| ︙ | | | ︙ | |
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
err = GetLastError();
if (err != ERROR_IO_PENDING) {
goto error;
}
}
return bytesRead;
error:
TclWinConvertError(GetLastError());
*errorCode = errno;
return -1;
}
/*
*----------------------------------------------------------------------
*
* SerialOutputProc --
*
|
|
>
>
>
>
>
>
|
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
err = GetLastError();
if (err != ERROR_IO_PENDING) {
goto error;
}
}
return bytesRead;
error:
TclWinConvertError(GetLastError());
*errorCode = errno;
return -1;
commError:
infoPtr->lastError = infoPtr->error; /* save last error code */
infoPtr->error = 0; /* reset error code */
*errorCode = EIO; /* to return read-error only once */
return -1;
}
/*
*----------------------------------------------------------------------
*
* SerialOutputProc --
*
|
| ︙ | | | ︙ | |
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
int toWrite, /* How many bytes to write? */
int *errorCode) /* Where to store error code. */
{
SerialInfo *infoPtr = (SerialInfo *) instanceData;
DWORD bytesWritten, err;
*errorCode = 0;
/*
* Check for a background error on the last write.
* Allow one write-fileevent after each callback
*/
if( toWrite ) {
|
>
>
>
>
>
>
>
>
>
>
|
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
int toWrite, /* How many bytes to write? */
int *errorCode) /* Where to store error code. */
{
SerialInfo *infoPtr = (SerialInfo *) instanceData;
DWORD bytesWritten, err;
*errorCode = 0;
/*
* Check if there is a CommError pending from SerialCheckProc
*/
if( infoPtr->error & SERIAL_WRITE_ERRORS ){
infoPtr->lastError = infoPtr->error; /* save last error code */
infoPtr->error = 0; /* reset error code */
*errorCode = EIO; /* to return read-error only once */
return -1;
}
/*
* Check for a background error on the last write.
* Allow one write-fileevent after each callback
*/
if( toWrite ) {
|
| ︙ | | | ︙ | |
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
|
infoPtr->channel = Tcl_CreateChannel(&serialChannelType, channelName,
(ClientData) infoPtr, permissions);
infoPtr->readable = infoPtr->writable = 0;
infoPtr->blockTime = SERIAL_DEFAULT_BLOCKTIME;
/*
* Files have default translation of AUTO and ^Z eof char, which
* means that a ^Z will be accepted as EOF when reading.
*/
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto");
Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}");
return infoPtr->channel;
}
/*
*----------------------------------------------------------------------
*
* SerialSetOptionProc --
*
* Sets an option on a channel.
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
infoPtr->channel = Tcl_CreateChannel(&serialChannelType, channelName,
(ClientData) infoPtr, permissions);
infoPtr->readable = infoPtr->writable = 0;
infoPtr->blockTime = SERIAL_DEFAULT_BLOCKTIME;
infoPtr->lastError = infoPtr->error = 0;
/*
* Files have default translation of AUTO and ^Z eof char, which
* means that a ^Z will be accepted as EOF when reading.
*/
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto");
Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}");
return infoPtr->channel;
}
/*
*----------------------------------------------------------------------
*
* SerialErrorStr --
*
* Converts a Win32 serial error code to a list of readable errors
*
*----------------------------------------------------------------------
*/
static void
SerialErrorStr(error, dsPtr)
DWORD error; /* Win32 serial error code */
Tcl_DString *dsPtr; /* Where to store string */
{
if( (error & CE_RXOVER) != 0) {
Tcl_DStringAppendElement(dsPtr, "RXOVER");
}
if( (error & CE_OVERRUN) != 0) {
Tcl_DStringAppendElement(dsPtr, "OVERRUN");
}
if( (error & CE_RXPARITY) != 0) {
Tcl_DStringAppendElement(dsPtr, "RXPARITY");
}
if( (error & CE_FRAME) != 0) {
Tcl_DStringAppendElement(dsPtr, "FRAME");
}
if( (error & CE_BREAK) != 0) {
Tcl_DStringAppendElement(dsPtr, "BREAK");
}
if( (error & CE_TXFULL) != 0) {
Tcl_DStringAppendElement(dsPtr, "TXFULL");
}
if( (error & ~(SERIAL_READ_ERRORS | SERIAL_WRITE_ERRORS)) != 0) {
char buf[TCL_INTEGER_SPACE + 1];
wsprintfA(buf, "%d", error);
Tcl_DStringAppendElement(dsPtr, buf);
}
}
/*
*----------------------------------------------------------------------
*
* SerialSetOptionProc --
*
* Sets an option on a channel.
|
| ︙ | | | ︙ | |
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
|
((len > 1) && (strncmp(optionName, "-pollinterval", len) == 0))) {
char buf[TCL_INTEGER_SPACE + 1];
valid = 1;
wsprintfA(buf, "%d", infoPtr->blockTime);
Tcl_DStringAppendElement(dsPtr, buf);
}
if (valid) {
return TCL_OK;
} else {
return Tcl_BadChannelOption(interp, optionName, "mode pollinterval");
}
}
|
>
>
>
>
>
>
>
>
>
>
>
|
>
|
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
|
((len > 1) && (strncmp(optionName, "-pollinterval", len) == 0))) {
char buf[TCL_INTEGER_SPACE + 1];
valid = 1;
wsprintfA(buf, "%d", infoPtr->blockTime);
Tcl_DStringAppendElement(dsPtr, buf);
}
/*
* get option -lasterror
* option is readonly and returned by [fconfigure chan -lasterror]
* but not returned by unnamed [fconfigure chan]
*/
if ( (len > 1) && (strncmp(optionName, "-lasterror", len) == 0) ) {
valid = 1;
SerialErrorStr(infoPtr->lastError, dsPtr);
}
if (valid) {
return TCL_OK;
} else {
return Tcl_BadChannelOption(interp, optionName,
"mode pollinterval lasterror");
}
}
|