]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/Utils/common/eCosSerial.h
Initial revision
[karo-tx-redboot.git] / tools / src / tools / Utils / common / eCosSerial.h
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
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) 
11 // any later version.
12 // 
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 
16 // more details.
17 // 
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.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //=================================================================
26 //
27 //        eCosSerial.h
28 //
29 //        Serial test class
30 //
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
34 //
35 // Author(s):     sdf
36 // Contributors:  sdf
37 // Date:          1999-04-01
38 // Description:   This class abstracts the serial port 
39 // Usage:
40 //
41 //
42 //####DESCRIPTIONEND####
43
44 #ifndef _CECOSSERIAL_H
45 #define _CECOSSERIAL_H
46 #include "eCosStd.h"
47 #include "eCosSocket.h"
48 #include "Collections.h"
49 //=================================================================
50 // This class is a host-independent interface to a serial port
51 //=================================================================
52 class CeCosSerial {
53     friend CeCosSocket::SSReadResult CeCosSocket::SSRead (CeCosSerial &serial,CeCosSocket &socket,void *pBuf,unsigned int nSize,unsigned int &nRead,bool *pbStop);
54
55 public:
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();
60
61     // Open the port with given baud rate.  Result indicates how successful we've been
62     bool Open(LPCTSTR pszPort,int nBaud);
63
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().
67
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
77
78     bool ApplySettings();
79     
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; }
91     bool Close();
92
93     // Clear the serial buffer:
94     bool Flush (void);
95
96     // Use to test success after opening with the ctor:
97     bool Ok() const { return 0!=m_pHandle; }
98
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);
102
103     // Use in the event of an error that needs to be cleared before the next operation:
104     bool ClearError();
105
106     // Set blocking/non-blocking
107     bool SetBlockingReads(bool b,bool bApplySettingsNow=true);
108
109     // Return last error
110     int Error() const { return m_nErr; }
111
112     // Return last error, translated to a string
113     String ErrString() const;
114
115 protected:
116     // The last error:
117     int m_nErr
118       ;
119     // Remember the error
120     void SaveError() { 
121         #ifdef _WIN32
122         m_nErr=WSAGetLastError();
123         #else // UNIX
124         m_nErr=errno;
125         #endif
126     }
127
128     // Line characteristics:
129     void *m_pHandle;
130     int m_nDataBits;
131     StopBitsType m_nStopBits;
132     bool m_bXONXOFFFlowControl;
133     bool m_bRTSCTSFlowControl;
134     bool m_bDSRDTRFlowControl;
135     bool m_bParity;
136     unsigned int m_nBaud;
137     int m_nTotalReadTimeout,m_nTotalWriteTimeout;
138     int m_nInterCharReadTimeout,m_nInterCharWriteTimeout; 
139     bool m_bBlockingReads;
140     String m_strPort;
141 };
142 #endif