Diff
Not logged in

Differences From Artifact [2da853a516]:

To Artifact [dbb05990f1]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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.
 *
 * Serial functionality implemented by Rolf.Schroedter@dlr.de
 *
 * RCS: @(#) $Id: tclWinSerial.c,v 1.25.2.2 2005/01/27 22:53:38 andreas_kupries Exp $
 */

#include "tclWinInt.h"

#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>













|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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.
 *
 * Serial functionality implemented by Rolf.Schroedter@dlr.de
 *
 * RCS: @(#) $Id: tclWinSerial.c,v 1.25.2.3 2005/10/05 06:33:52 hobbs Exp $
 */

#include "tclWinInt.h"

#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721

1722
1723
1724
1725
1726
1727

1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744

1745
1746
1747
1748
1749
1750
1751
1752
1753
1754

1755
1756
1757
1758
1759

1760
1761
1762
1763
1764
1765
1766
1767
1768

1769
1770
1771
1772
1773
1774
1775
1776
1777

1778
1779
1780
1781
1782
1783
1784
1785
1786

1787
1788
1789
1790
1791
1792
1793
1794

1795
1796
1797
1798


1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822

1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
 * Side effects:
 *  May modify an option on a device.
 *
 *----------------------------------------------------------------------
 */
static int
SerialSetOptionProc(instanceData, interp, optionName, value)
    ClientData instanceData;    /* File state. */
    Tcl_Interp *interp;         /* For error reporting - can be NULL. */
    CONST char *optionName;     /* Which option to set? */
    CONST char *value;          /* New value for option. */
{
    SerialInfo *infoPtr;
    DCB dcb;
    BOOL result, flag;
    size_t len, vlen;
    Tcl_DString ds;
    CONST TCHAR *native;
    int argc;
    CONST char **argv;
    
    infoPtr = (SerialInfo *) instanceData;
    
    /* 
    * Parse options
    */
    len = strlen(optionName);
    vlen = strlen(value);

    /* 
    * Option -mode baud,parity,databits,stopbits
    */
    if ((len > 2) && (strncmp(optionName, "-mode", len) == 0)) {
        
        if (! GetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        native = Tcl_WinUtfToTChar(value, -1, &ds);
        result = (*tclWinProcs->buildCommDCBProc)(native, &dcb);
        Tcl_DStringFree(&ds);
        
        if (result == FALSE) {
            if (interp) {
                Tcl_AppendResult(interp,
                    "bad value for -mode: should be baud,parity,data,stop",
                    (char *) NULL);
            }
            return TCL_ERROR;
        }

        /* Default settings for serial communications */ 
        dcb.fBinary = TRUE;
        dcb.fErrorChar = FALSE;
        dcb.fNull = FALSE;
        dcb.fAbortOnError = FALSE;

        if (! SetCommState(infoPtr->handle, &dcb) ) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't set comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        return TCL_OK;
    }
    
    /* 
    * Option -handshake none|xonxoff|rtscts|dtrdsr
    */
    if ((len > 1) && (strncmp(optionName, "-handshake", len) == 0)) {
        
        if (! GetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        /* 
        * Reset all handshake options
        * DTR and RTS are ON by default
        */
        dcb.fOutX = dcb.fInX = FALSE;
        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = dcb.fDsrSensitivity = FALSE;
        dcb.fDtrControl = DTR_CONTROL_ENABLE;
        dcb.fRtsControl = RTS_CONTROL_ENABLE;
        dcb.fTXContinueOnXoff = FALSE;

        /* 
        * Adjust the handshake limits.
        * Yes, the XonXoff limits seem to influence even hardware handshake
        */
        dcb.XonLim = (WORD) (infoPtr->sysBufRead*1/2);
        dcb.XoffLim = (WORD) (infoPtr->sysBufRead*1/4);
        
        if (strnicmp(value, "NONE", vlen) == 0) {
            /* leave all handshake options disabled */
        } else if (strnicmp(value, "XONXOFF", vlen) == 0) {
            dcb.fOutX = dcb.fInX = TRUE;
        } else if (strnicmp(value, "RTSCTS", vlen) == 0) {
            dcb.fOutxCtsFlow = TRUE;
            dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
        } else if (strnicmp(value, "DTRDSR", vlen) == 0) {
            dcb.fOutxDsrFlow = TRUE;
            dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
        } else {
            if (interp) {
                Tcl_AppendResult(interp, "bad value for -handshake: ",
                    "must be one of xonxoff, rtscts, dtrdsr or none",
                    (char *) NULL);
                return TCL_ERROR;
            }
        }
        
        if (! SetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't set comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        return TCL_OK;
    }
    
    /* 
    * Option -xchar {\x11 \x13}
    */
    if ((len > 1) && (strncmp(optionName, "-xchar", len) == 0)) {
        
        if (! GetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        
        if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
            return TCL_ERROR;
        }
        if (argc == 2) {
            dcb.XonChar  = argv[0][0];
            dcb.XoffChar = argv[1][0];

        } else {
            if (interp) {
                Tcl_AppendResult(interp,
                    "bad value for -xchar: should be a list of two elements",
                    (char *) NULL);
            }

            return TCL_ERROR;
        }
        
        if (! SetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't set comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        return TCL_OK;
    }
    
    /* 
    * Option -ttycontrol {DTR 1 RTS 0 BREAK 0}
    */
    if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) {

        
        if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
            return TCL_ERROR;
        }
        if ((argc % 2) == 1) {
            if (interp) {
                Tcl_AppendResult(interp,
                    "bad value for -ttycontrol: should be a list of signal,value pairs",
                    (char *) NULL);
            }

            return TCL_ERROR;
        }
        while (argc > 1) {
            if (Tcl_GetBoolean(interp, argv[1], &flag) == TCL_ERROR) {
                return TCL_ERROR;

            }
            if (strnicmp(argv[0], "DTR", strlen(argv[0])) == 0) {
                if (! EscapeCommFunction(infoPtr->handle, flag ?
                        (DWORD) SETDTR : (DWORD) CLRDTR)) {
                    if (interp) {
                        Tcl_AppendResult(interp, 
                            "can't set DTR signal", (char *) NULL);
                    }
                    return TCL_ERROR;

                }
            } else if (strnicmp(argv[0], "RTS", strlen(argv[0])) == 0) {
                if (! EscapeCommFunction(infoPtr->handle, flag ?
                        (DWORD) SETRTS : (DWORD) CLRRTS)) {
                    if (interp) {
                        Tcl_AppendResult(interp, 
                            "can't set RTS signal", (char *) NULL);
                    }
                    return TCL_ERROR;

                }
            } else if (strnicmp(argv[0], "BREAK", strlen(argv[0])) == 0) {
                if (! EscapeCommFunction(infoPtr->handle, flag ?
                        (DWORD) SETBREAK : (DWORD) CLRBREAK)) {
                    if (interp) {
                        Tcl_AppendResult(interp, 
                            "can't set BREAK signal", (char *) NULL);
                    }
                    return TCL_ERROR;

                }
            } else {
                if (interp) {
                    Tcl_AppendResult(interp, 
                        "bad signal for -ttycontrol: must be DTR, RTS or BREAK", 
                        (char *) NULL);
                }
                return TCL_ERROR;

            }
            argc -= 2, argv += 2;
        } /* while (argc > 1) */
        


        return TCL_OK;
    }
    
    /* 
    * Option -sysbuffer {read_size write_size}
    * Option -sysbuffer read_size 
    */
    if ((len > 1) && (strncmp(optionName, "-sysbuffer", len) == 0)) {
        
        /*
        * -sysbuffer 4096 or -sysbuffer {64536 4096}
        */
        size_t inSize = (size_t) -1, outSize = (size_t) -1;
        
        if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
            return TCL_ERROR;
        }
        if (argc == 1) {
            inSize = atoi(argv[0]);
            outSize = infoPtr->sysBufWrite;
        } else if (argc == 2) {
            inSize  = atoi(argv[0]);
            outSize = atoi(argv[1]);
        }

        if ( (inSize <= 0) || (outSize <= 0) ) {
            if (interp) {
                Tcl_AppendResult(interp,
                    "bad value for -sysbuffer: should be a list of one or two integers > 0",
                    (char *) NULL);
            }
            return TCL_ERROR;
        }
        if (! SetupComm(infoPtr->handle, inSize, outSize)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't setup comm buffers", (char *) NULL);
            }
            return TCL_ERROR;
        }
        infoPtr->sysBufRead  = inSize;
        infoPtr->sysBufWrite = outSize;
        
         /* 
        * Adjust the handshake limits.
        * Yes, the XonXoff limits seem to influence even hardware handshake
        */
        if (! GetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        dcb.XonLim = (WORD) (infoPtr->sysBufRead*1/2);
        dcb.XoffLim = (WORD) (infoPtr->sysBufRead*1/4);
        if (! SetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't set comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        return TCL_OK;
    }

    /* 
    * Option -pollinterval msec
    */
    if ((len > 1) && (strncmp(optionName, "-pollinterval", len) == 0)) {
        
        if ( Tcl_GetInt(interp, value, &(infoPtr->blockTime)) != TCL_OK ) {
            return TCL_ERROR;
        }
        return TCL_OK;
    }
    
    /* 
    * Option -timeout msec
    */
    if ((len > 2) && (strncmp(optionName, "-timeout", len) == 0)) {
        int msec;
        COMMTIMEOUTS tout = {0,0,0,0,0};
  
        if ( Tcl_GetInt(interp, value, &msec) != TCL_OK ) {
            return TCL_ERROR;
        }
        tout.ReadTotalTimeoutConstant = msec;
        if (! SetCommTimeouts(infoPtr->handle, &tout)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't set comm timeouts", (char *) NULL);
            }
            return TCL_ERROR;
        }

        return TCL_OK;
    }
    
    return Tcl_BadChannelOption(interp, optionName,
        "mode handshake pollinterval sysbuffer timeout ttycontrol xchar");
}

/*
 *----------------------------------------------------------------------
 *
 * SerialGetOptionProc --
 *







|
|
|
|









|

|
|
|
|



|
|
|

<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|

<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|

<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|

>
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
<
|
|
>
|
<
<
|
>
>
|

|
|
|
|
|

<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|


|
|
|

|
|
|
|
|

|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|

|

|

|







1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607

1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647

1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704

1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796

1797
1798
1799
1800


1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811

1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
 * Side effects:
 *  May modify an option on a device.
 *
 *----------------------------------------------------------------------
 */
static int
SerialSetOptionProc(instanceData, interp, optionName, value)
    ClientData instanceData;	/* File state. */
    Tcl_Interp *interp;		/* For error reporting - can be NULL. */
    CONST char *optionName;	/* Which option to set? */
    CONST char *value;		/* New value for option. */
{
    SerialInfo *infoPtr;
    DCB dcb;
    BOOL result, flag;
    size_t len, vlen;
    Tcl_DString ds;
    CONST TCHAR *native;
    int argc;
    CONST char **argv;

    infoPtr = (SerialInfo *) instanceData;

    /*
     * Parse options
     */
    len = strlen(optionName);
    vlen = strlen(value);

    /*
     * Option -mode baud,parity,databits,stopbits
     */
    if ((len > 2) && (strncmp(optionName, "-mode", len) == 0)) {

	if (! GetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	native = Tcl_WinUtfToTChar(value, -1, &ds);
	result = (*tclWinProcs->buildCommDCBProc)(native, &dcb);
	Tcl_DStringFree(&ds);

	if (result == FALSE) {
	    if (interp) {
		Tcl_AppendResult(interp,
			"bad value for -mode: should be baud,parity,data,stop",
			(char *) NULL);
	    }
	    return TCL_ERROR;
	}

	/* Default settings for serial communications */ 
	dcb.fBinary = TRUE;
	dcb.fErrorChar = FALSE;
	dcb.fNull = FALSE;
	dcb.fAbortOnError = FALSE;

	if (! SetCommState(infoPtr->handle, &dcb) ) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't set comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    /*
     * Option -handshake none|xonxoff|rtscts|dtrdsr
     */
    if ((len > 1) && (strncmp(optionName, "-handshake", len) == 0)) {

	if (! GetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	/*
	 * Reset all handshake options
	 * DTR and RTS are ON by default
	 */
	dcb.fOutX = dcb.fInX = FALSE;
	dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = dcb.fDsrSensitivity = FALSE;
	dcb.fDtrControl = DTR_CONTROL_ENABLE;
	dcb.fRtsControl = RTS_CONTROL_ENABLE;
	dcb.fTXContinueOnXoff = FALSE;

	/*
	 * Adjust the handshake limits.
	 * Yes, the XonXoff limits seem to influence even hardware handshake
	 */
	dcb.XonLim = (WORD) (infoPtr->sysBufRead*1/2);
	dcb.XoffLim = (WORD) (infoPtr->sysBufRead*1/4);

	if (strnicmp(value, "NONE", vlen) == 0) {
	    /* leave all handshake options disabled */
	} else if (strnicmp(value, "XONXOFF", vlen) == 0) {
	    dcb.fOutX = dcb.fInX = TRUE;
	} else if (strnicmp(value, "RTSCTS", vlen) == 0) {
	    dcb.fOutxCtsFlow = TRUE;
	    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
	} else if (strnicmp(value, "DTRDSR", vlen) == 0) {
	    dcb.fOutxDsrFlow = TRUE;
	    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
	} else {
	    if (interp) {
		Tcl_AppendResult(interp, "bad value for -handshake: ",
			"must be one of xonxoff, rtscts, dtrdsr or none",
			(char *) NULL);
		return TCL_ERROR;
	    }
	}

	if (! SetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't set comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    /*
     * Option -xchar {\x11 \x13}
     */
    if ((len > 1) && (strncmp(optionName, "-xchar", len) == 0)) {

	if (! GetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}

	if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
	    return TCL_ERROR;
	}
	if (argc == 2) {
	    dcb.XonChar	 = argv[0][0];
	    dcb.XoffChar = argv[1][0];
	    ckfree((char *) argv);
	} else {
	    if (interp) {
		Tcl_AppendResult(interp,
			"bad value for -xchar: should be a list of two elements",
			(char *) NULL);
	    }
	    ckfree((char *) argv);
	    return TCL_ERROR;
	}

	if (! SetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp,
			"can't set comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    /*
     * Option -ttycontrol {DTR 1 RTS 0 BREAK 0}
     */
    if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) {
	int i, result = TCL_OK;

	if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
	    return TCL_ERROR;
	}
	if ((argc % 2) == 1) {
	    if (interp) {
		Tcl_AppendResult(interp,
			"bad value for -ttycontrol: should be a list of signal,value pairs",
			(char *) NULL);
	    }
	    ckfree((char *) argv);
	    return TCL_ERROR;
	}
	for (i = 0; i < argc - 1; i += 2) {
	    if (Tcl_GetBoolean(interp, argv[i+1], &flag) == TCL_ERROR) {
		result = TCL_ERROR;
		break;
	    }
	    if (strnicmp(argv[i], "DTR", strlen(argv[i])) == 0) {
		if (!EscapeCommFunction(infoPtr->handle, flag ?
			    (DWORD) SETDTR : (DWORD) CLRDTR)) {
		    if (interp) {
			Tcl_AppendResult(interp,
				"can't set DTR signal", (char *) NULL);
		    }
		    result = TCL_ERROR;
		    break;
		}
	    } else if (strnicmp(argv[i], "RTS", strlen(argv[i])) == 0) {
		if (!EscapeCommFunction(infoPtr->handle, flag ?
			    (DWORD) SETRTS : (DWORD) CLRRTS)) {
		    if (interp) {
			Tcl_AppendResult(interp,
				"can't set RTS signal", (char *) NULL);
		    }
		    result = TCL_ERROR;
		    break;
		}
	    } else if (strnicmp(argv[i], "BREAK", strlen(argv[i])) == 0) {
		if (!EscapeCommFunction(infoPtr->handle, flag ?
			    (DWORD) SETBREAK : (DWORD) CLRBREAK)) {
		    if (interp) {
			Tcl_AppendResult(interp,
				"can't set BREAK signal", (char *) NULL);
		    }
		    result = TCL_ERROR;
		    break;
		}
	    } else {
		if (interp) {
		    Tcl_AppendResult(interp, "bad signal for -ttycontrol: ",
			    "must be DTR, RTS or BREAK", (char *) NULL);

		}
		result = TCL_ERROR;
		break;
	    }


	}

	ckfree((char *) argv);
	return result;
    }

    /*
     * Option -sysbuffer {read_size write_size}
     * Option -sysbuffer read_size 
     */
    if ((len > 1) && (strncmp(optionName, "-sysbuffer", len) == 0)) {

	/*
	 * -sysbuffer 4096 or -sysbuffer {64536 4096}
	 */
	size_t inSize = (size_t) -1, outSize = (size_t) -1;

	if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
	    return TCL_ERROR;
	}
	if (argc == 1) {
	    inSize = atoi(argv[0]);
	    outSize = infoPtr->sysBufWrite;
	} else if (argc == 2) {
	    inSize  = atoi(argv[0]);
	    outSize = atoi(argv[1]);
	}
	ckfree((char *) argv);
	if ((inSize <= 0) || (outSize <= 0)) {
	    if (interp) {
		Tcl_AppendResult(interp,
			"bad value for -sysbuffer: should be a list of one or two integers > 0",
			(char *) NULL);
	    }
	    return TCL_ERROR;
	}
	if (! SetupComm(infoPtr->handle, inSize, outSize)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't setup comm buffers", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	infoPtr->sysBufRead  = inSize;
	infoPtr->sysBufWrite = outSize;

	/*
	 * Adjust the handshake limits.
	 * Yes, the XonXoff limits seem to influence even hardware handshake
	 */
	if (! GetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	dcb.XonLim = (WORD) (infoPtr->sysBufRead*1/2);
	dcb.XoffLim = (WORD) (infoPtr->sysBufRead*1/4);
	if (! SetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't set comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    /*
     * Option -pollinterval msec
     */
    if ((len > 1) && (strncmp(optionName, "-pollinterval", len) == 0)) {

	if ( Tcl_GetInt(interp, value, &(infoPtr->blockTime)) != TCL_OK ) {
	    return TCL_ERROR;
	}
	return TCL_OK;
    }

    /*
     * Option -timeout msec
     */
    if ((len > 2) && (strncmp(optionName, "-timeout", len) == 0)) {
	int msec;
	COMMTIMEOUTS tout = {0,0,0,0,0};

	if ( Tcl_GetInt(interp, value, &msec) != TCL_OK ) {
	    return TCL_ERROR;
	}
	tout.ReadTotalTimeoutConstant = msec;
	if (! SetCommTimeouts(infoPtr->handle, &tout)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't set comm timeouts", (char *) NULL);
	    }
	    return TCL_ERROR;
	}

	return TCL_OK;
    }

    return Tcl_BadChannelOption(interp, optionName,
	    "mode handshake pollinterval sysbuffer timeout ttycontrol xchar");
}

/*
 *----------------------------------------------------------------------
 *
 * SerialGetOptionProc --
 *
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
 *  The string returned by this function is in static storage and
 *  may be reused at any time subsequent to the call.
 *
 *----------------------------------------------------------------------
 */
static int
SerialGetOptionProc(instanceData, interp, optionName, dsPtr)
    ClientData instanceData;    /* File state. */
    Tcl_Interp *interp;         /* For error reporting - can be NULL. */
    CONST char *optionName;     /* Option to get. */
    Tcl_DString *dsPtr;         /* Where to store value(s). */
{
    SerialInfo *infoPtr;
    DCB dcb;
    size_t len;
    int valid = 0;  /* flag if valid option parsed */
    
    infoPtr = (SerialInfo *) instanceData;
    
    if (optionName == NULL) {
        len = 0;
    } else {
        len = strlen(optionName);
    }
    
    /*
    * get option -mode
    */
    
    if (len == 0) {
        Tcl_DStringAppendElement(dsPtr, "-mode");
    }
    if ((len == 0) ||
        ((len > 2) && (strncmp(optionName, "-mode", len) == 0))) {
        
        char parity;
        char *stop;
        char buf[2 * TCL_INTEGER_SPACE + 16];
        
        if (! GetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        
        valid = 1;
        parity = 'n';
        if (dcb.Parity <= 4) {
            parity = "noems"[dcb.Parity];
        }
        stop = (dcb.StopBits == ONESTOPBIT) ? "1" :
        (dcb.StopBits == ONE5STOPBITS) ? "1.5" : "2";
        
        wsprintfA(buf, "%d,%c,%d,%s", dcb.BaudRate, parity,
            dcb.ByteSize, stop);
        Tcl_DStringAppendElement(dsPtr, buf);
    }
    
    /*
    * get option -pollinterval
    */
    
    if (len == 0) {
        Tcl_DStringAppendElement(dsPtr, "-pollinterval");
    }
    if ((len == 0) ||
        ((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 -sysbuffer
    */
    
    if (len == 0) {
        Tcl_DStringAppendElement(dsPtr, "-sysbuffer");
        Tcl_DStringStartSublist(dsPtr);
    }
    if ((len == 0) ||
        ((len > 1) && (strncmp(optionName, "-sysbuffer", len) == 0))) {

        char buf[TCL_INTEGER_SPACE + 1];
        valid = 1;

        wsprintfA(buf, "%d", infoPtr->sysBufRead);
        Tcl_DStringAppendElement(dsPtr, buf);
        wsprintfA(buf, "%d", infoPtr->sysBufWrite);
        Tcl_DStringAppendElement(dsPtr, buf);
    }
    if (len == 0) {
        Tcl_DStringEndSublist(dsPtr);
    }
    
    /*
    * get option -xchar
    */
    
    if (len == 0) {
        Tcl_DStringAppendElement(dsPtr, "-xchar");
        Tcl_DStringStartSublist(dsPtr);
    }
    if ((len == 0) ||
        ((len > 1) && (strncmp(optionName, "-xchar", len) == 0))) {

        char buf[4];
        valid = 1;

        if (! GetCommState(infoPtr->handle, &dcb)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get comm state", (char *) NULL);
            }
            return TCL_ERROR;
        }
        sprintf(buf, "%c", dcb.XonChar);
        Tcl_DStringAppendElement(dsPtr, buf);
        sprintf(buf, "%c", dcb.XoffChar);
        Tcl_DStringAppendElement(dsPtr, buf);
    }
    if (len == 0) {
        Tcl_DStringEndSublist(dsPtr);
    }

    /*
    * 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);
    }
    
    /*
    * get option -queue
    * option is readonly and returned by [fconfigure chan -queue]
    */
    
    if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) {
        char buf[TCL_INTEGER_SPACE + 1];
        COMSTAT cStat;
        DWORD error;
	int inBuffered, outBuffered, count;

        valid = 1;

        /* 
        * Query the pending data in Tcl's internal queues
        */
        inBuffered  = Tcl_InputBuffered(infoPtr->channel);
	outBuffered = Tcl_OutputBuffered(infoPtr->channel);

        /*
        * Query the number of bytes in our output queue:
        *     1. The bytes pending in the output thread
        *     2. The bytes in the system drivers buffer
        * The writer thread should not interfere this action.
        */
        EnterCriticalSection(&infoPtr->csWrite);
        ClearCommError( infoPtr->handle, &error, &cStat );
        count = (int)cStat.cbOutQue + infoPtr->writeQueue;
        LeaveCriticalSection(&infoPtr->csWrite);

        wsprintfA(buf, "%d", inBuffered + cStat.cbInQue); 
        Tcl_DStringAppendElement(dsPtr, buf);
        wsprintfA(buf, "%d", outBuffered + count); 
        Tcl_DStringAppendElement(dsPtr, buf);
    }

    /*
    * get option -ttystatus
    * option is readonly and returned by [fconfigure chan -ttystatus]
    * but not returned by unnamed [fconfigure chan]
    */
    if ( (len > 4) && (strncmp(optionName, "-ttystatus", len) == 0) ) {
        
        DWORD status;
        
        if (! GetCommModemStatus(infoPtr->handle, &status)) {
            if (interp) {
                Tcl_AppendResult(interp, 
                    "can't get tty status", (char *) NULL);
            }
            return TCL_ERROR;
        }
        valid = 1;
        SerialModemStatusStr(status, dsPtr);
    }
    
    if (valid) {
        return TCL_OK;
    } else {
        return Tcl_BadChannelOption(interp, optionName,
            "mode pollinterval lasterror queue sysbuffer ttystatus xchar");
    }
}

/*
 *----------------------------------------------------------------------
 *
 * SerialThreadActionProc --







|
|
|
|





|

|

|

|

|

|
|
|

|


|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|

|
|
|

|


|
|
|
|
|
|

|

|
|
|

|
|


|

|
|

|
|
|
|


|

|

|
|
|

|
|


|

|
|

|
|
|
|
|
|
|
|
|
|
|


|



|
|
|
|
|

|
|

|

|
|
|
|

|
|
|


|

|
|
|
|


|
|
|
|
|
|
|
|
|
|

|
|
|
|



|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|

|

|
|







1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
 *  The string returned by this function is in static storage and
 *  may be reused at any time subsequent to the call.
 *
 *----------------------------------------------------------------------
 */
static int
SerialGetOptionProc(instanceData, interp, optionName, dsPtr)
    ClientData instanceData;	/* File state. */
    Tcl_Interp *interp;		/* For error reporting - can be NULL. */
    CONST char *optionName;	/* Option to get. */
    Tcl_DString *dsPtr;		/* Where to store value(s). */
{
    SerialInfo *infoPtr;
    DCB dcb;
    size_t len;
    int valid = 0;  /* flag if valid option parsed */

    infoPtr = (SerialInfo *) instanceData;

    if (optionName == NULL) {
	len = 0;
    } else {
	len = strlen(optionName);
    }

    /*
     * get option -mode
     */

    if (len == 0) {
	Tcl_DStringAppendElement(dsPtr, "-mode");
    }
    if ((len == 0) ||
	    ((len > 2) && (strncmp(optionName, "-mode", len) == 0))) {

	char parity;
	char *stop;
	char buf[2 * TCL_INTEGER_SPACE + 16];

	if (! GetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}

	valid = 1;
	parity = 'n';
	if (dcb.Parity <= 4) {
	    parity = "noems"[dcb.Parity];
	}
	stop = (dcb.StopBits == ONESTOPBIT) ? "1" :
	    (dcb.StopBits == ONE5STOPBITS) ? "1.5" : "2";

	wsprintfA(buf, "%d,%c,%d,%s", dcb.BaudRate, parity,
		dcb.ByteSize, stop);
	Tcl_DStringAppendElement(dsPtr, buf);
    }

    /*
     * get option -pollinterval
     */

    if (len == 0) {
	Tcl_DStringAppendElement(dsPtr, "-pollinterval");
    }
    if ((len == 0) ||
	    ((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 -sysbuffer
     */

    if (len == 0) {
	Tcl_DStringAppendElement(dsPtr, "-sysbuffer");
	Tcl_DStringStartSublist(dsPtr);
    }
    if ((len == 0) ||
	    ((len > 1) && (strncmp(optionName, "-sysbuffer", len) == 0))) {

	char buf[TCL_INTEGER_SPACE + 1];
	valid = 1;

	wsprintfA(buf, "%d", infoPtr->sysBufRead);
	Tcl_DStringAppendElement(dsPtr, buf);
	wsprintfA(buf, "%d", infoPtr->sysBufWrite);
	Tcl_DStringAppendElement(dsPtr, buf);
    }
    if (len == 0) {
	Tcl_DStringEndSublist(dsPtr);
    }

    /*
     * get option -xchar
     */

    if (len == 0) {
	Tcl_DStringAppendElement(dsPtr, "-xchar");
	Tcl_DStringStartSublist(dsPtr);
    }
    if ((len == 0) ||
	    ((len > 1) && (strncmp(optionName, "-xchar", len) == 0))) {

	char buf[4];
	valid = 1;

	if (! GetCommState(infoPtr->handle, &dcb)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get comm state", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	sprintf(buf, "%c", dcb.XonChar);
	Tcl_DStringAppendElement(dsPtr, buf);
	sprintf(buf, "%c", dcb.XoffChar);
	Tcl_DStringAppendElement(dsPtr, buf);
    }
    if (len == 0) {
	Tcl_DStringEndSublist(dsPtr);
    }

    /*
     * 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);
    }

    /*
     * get option -queue
     * option is readonly and returned by [fconfigure chan -queue]
     */

    if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) {
	char buf[TCL_INTEGER_SPACE + 1];
	COMSTAT cStat;
	DWORD error;
	int inBuffered, outBuffered, count;

	valid = 1;

	/* 
	 * Query the pending data in Tcl's internal queues
	 */
	inBuffered  = Tcl_InputBuffered(infoPtr->channel);
	outBuffered = Tcl_OutputBuffered(infoPtr->channel);

	/*
	 * Query the number of bytes in our output queue:
	 *     1. The bytes pending in the output thread
	 *     2. The bytes in the system drivers buffer
	 * The writer thread should not interfere this action.
	 */
	EnterCriticalSection(&infoPtr->csWrite);
	ClearCommError( infoPtr->handle, &error, &cStat );
	count = (int)cStat.cbOutQue + infoPtr->writeQueue;
	LeaveCriticalSection(&infoPtr->csWrite);

	wsprintfA(buf, "%d", inBuffered + cStat.cbInQue); 
	Tcl_DStringAppendElement(dsPtr, buf);
	wsprintfA(buf, "%d", outBuffered + count); 
	Tcl_DStringAppendElement(dsPtr, buf);
    }

    /*
     * get option -ttystatus
     * option is readonly and returned by [fconfigure chan -ttystatus]
     * but not returned by unnamed [fconfigure chan]
     */
    if ((len > 4) && (strncmp(optionName, "-ttystatus", len) == 0)) {

	DWORD status;

	if (! GetCommModemStatus(infoPtr->handle, &status)) {
	    if (interp) {
		Tcl_AppendResult(interp, 
			"can't get tty status", (char *) NULL);
	    }
	    return TCL_ERROR;
	}
	valid = 1;
	SerialModemStatusStr(status, dsPtr);
    }

    if (valid) {
	return TCL_OK;
    } else {
	return Tcl_BadChannelOption(interp, optionName,
		"mode pollinterval lasterror queue sysbuffer ttystatus xchar");
    }
}

/*
 *----------------------------------------------------------------------
 *
 * SerialThreadActionProc --