1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 2 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //=================================================================
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
38 // Description: This class abstracts the serial port
42 //####DESCRIPTIONEND####
44 #ifndef _CECOSSERIAL_H
45 #define _CECOSSERIAL_H
47 #include "eCosSocket.h"
48 #include "Collections.h"
49 //=================================================================
50 // This class is a host-independent interface to a serial port
51 //=================================================================
53 friend CeCosSocket::SSReadResult CeCosSocket::SSRead (CeCosSerial &serial,CeCosSocket &socket,void *pBuf,unsigned int nSize,unsigned int &nRead,bool *pbStop);
56 enum StopBitsType { ONE_STOP_BIT, ONE_POINT_FIVE_STOP_BITS, TWO_STOP_BITS };
57 CeCosSerial(LPCTSTR pszPort,int nBaud); // ctor and open all in one go
58 CeCosSerial(); // Call Open() later
59 virtual ~CeCosSerial();
61 // Open the port with given baud rate. Result indicates how successful we've been
62 bool Open(LPCTSTR pszPort,int nBaud);
64 // Set various line characteristics. This can be done with the line open or closed.
65 // In each case the "bApplySettingsNow" argument indicates whether to perform the action now,
66 // or to hold off until a call of ApplySettings().
68 bool SetBaud(unsigned int nBaud,bool bApplySettingsNow=true);
69 bool SetParity(bool bParityOn,bool bApplySettingsNow=true);
70 bool SetDataBits(int n,bool bApplySettingsNow=true);
71 bool SetStopBits(StopBitsType n,bool bApplySettingsNow=true);
72 bool SetXONXOFFFlowControl(bool b,bool bApplySettingsNow=true);
73 bool SetRTSCTSFlowControl(bool b,bool bApplySettingsNow=true);
74 bool SetDSRDTRFlowControl(bool b,bool bApplySettingsNow=true);
75 bool SetReadTimeOuts(int nTotal,int nBetweenChars,bool bApplySettingsNow=true); // Times are in mSec
76 bool SetWriteTimeOuts(int nTotal,int nBetweenChars,bool bApplySettingsNow=true); // Times are in mSec
80 // Query the settings:
81 int GetParity() const { return m_bParity; }
82 int GetDataBits() const { return m_nDataBits; }
83 StopBitsType GetStopBits() const { return m_nStopBits; }
84 bool GetXONXOFFFlowControl() const { return m_bXONXOFFFlowControl; }
85 bool GetRTSCTSFlowControl() const { return m_bRTSCTSFlowControl; }
86 bool GetDSRDTRFlowControl() const { return m_bDSRDTRFlowControl; }
87 unsigned int GetBaud() const { return m_nBaud; }
88 bool GetReadTimeOuts(int &nTotal,int &nBetweenChars) const {nTotal=m_nTotalReadTimeout; nBetweenChars=m_nInterCharReadTimeout; return true; }// mSec
89 bool GetWriteTimeOuts(int &nTotal,int &nBetweenChars) const {nTotal=m_nTotalWriteTimeout; nBetweenChars=m_nInterCharWriteTimeout; return true; }// mSec
90 bool GetBlockingReads() const { return m_bBlockingReads; }
93 // Clear the serial buffer:
96 // Use to test success after opening with the ctor:
97 bool Ok() const { return 0!=m_pHandle; }
99 // Will read up to the length provided:
100 bool Read (void *pBuf,unsigned int nSize,unsigned int &nRead);
101 bool Write(void *pBuf,unsigned int nSize,unsigned int &nWritten);
103 // Use in the event of an error that needs to be cleared before the next operation:
106 // Set blocking/non-blocking
107 bool SetBlockingReads(bool b,bool bApplySettingsNow=true);
110 int Error() const { return m_nErr; }
112 // Return last error, translated to a string
113 String ErrString() const;
119 // Remember the error
122 m_nErr=WSAGetLastError();
128 // Line characteristics:
131 StopBitsType m_nStopBits;
132 bool m_bXONXOFFFlowControl;
133 bool m_bRTSCTSFlowControl;
134 bool m_bDSRDTRFlowControl;
136 unsigned int m_nBaud;
137 int m_nTotalReadTimeout,m_nTotalWriteTimeout;
138 int m_nInterCharReadTimeout,m_nInterCharWriteTimeout;
139 bool m_bBlockingReads;