1 |
786 |
skrzyp |
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####
|
2 |
|
|
// -------------------------------------------
|
3 |
|
|
// This file is part of the eCos host tools.
|
4 |
|
|
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
5 |
|
|
//
|
6 |
|
|
// This program is free software; you can redistribute it and/or modify
|
7 |
|
|
// it under the terms of the GNU General Public License as published by
|
8 |
|
|
// the Free Software Foundation; either version 2 or (at your option) any
|
9 |
|
|
// later version.
|
10 |
|
|
//
|
11 |
|
|
// This program is distributed in the hope that it will be useful, but
|
12 |
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
// General Public License for more details.
|
15 |
|
|
//
|
16 |
|
|
// You should have received a copy of the GNU General Public License
|
17 |
|
|
// along with this program; if not, write to the
|
18 |
|
|
// Free Software Foundation, Inc., 51 Franklin Street,
|
19 |
|
|
// Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
|
|
// -------------------------------------------
|
21 |
|
|
// ####ECOSHOSTGPLCOPYRIGHTEND####
|
22 |
|
|
//=================================================================
|
23 |
|
|
//
|
24 |
|
|
// eCosSerial.h
|
25 |
|
|
//
|
26 |
|
|
// Serial test class
|
27 |
|
|
//
|
28 |
|
|
//=================================================================
|
29 |
|
|
//=================================================================
|
30 |
|
|
//#####DESCRIPTIONBEGIN####
|
31 |
|
|
//
|
32 |
|
|
// Author(s): sdf
|
33 |
|
|
// Contributors: sdf
|
34 |
|
|
// Date: 1999-04-01
|
35 |
|
|
// Description: This class abstracts the serial port
|
36 |
|
|
// Usage:
|
37 |
|
|
//
|
38 |
|
|
//
|
39 |
|
|
//####DESCRIPTIONEND####
|
40 |
|
|
|
41 |
|
|
#ifndef _CECOSSERIAL_H
|
42 |
|
|
#define _CECOSSERIAL_H
|
43 |
|
|
#include "eCosStd.h"
|
44 |
|
|
#include "eCosSocket.h"
|
45 |
|
|
#include "Collections.h"
|
46 |
|
|
//=================================================================
|
47 |
|
|
// This class is a host-independent interface to a serial port
|
48 |
|
|
//=================================================================
|
49 |
|
|
class CeCosSerial {
|
50 |
|
|
friend CeCosSocket::SSReadResult CeCosSocket::SSRead (CeCosSerial &serial,CeCosSocket &socket,void *pBuf,unsigned int nSize,unsigned int &nRead,bool *pbStop);
|
51 |
|
|
|
52 |
|
|
public:
|
53 |
|
|
enum StopBitsType { ONE_STOP_BIT, ONE_POINT_FIVE_STOP_BITS, TWO_STOP_BITS };
|
54 |
|
|
CeCosSerial(LPCTSTR pszPort,int nBaud); // ctor and open all in one go
|
55 |
|
|
CeCosSerial(); // Call Open() later
|
56 |
|
|
virtual ~CeCosSerial();
|
57 |
|
|
|
58 |
|
|
// Open the port with given baud rate. Result indicates how successful we've been
|
59 |
|
|
bool Open(LPCTSTR pszPort,int nBaud);
|
60 |
|
|
|
61 |
|
|
// Set various line characteristics. This can be done with the line open or closed.
|
62 |
|
|
// In each case the "bApplySettingsNow" argument indicates whether to perform the action now,
|
63 |
|
|
// or to hold off until a call of ApplySettings().
|
64 |
|
|
|
65 |
|
|
bool SetBaud(unsigned int nBaud,bool bApplySettingsNow=true);
|
66 |
|
|
bool SetParity(bool bParityOn,bool bApplySettingsNow=true);
|
67 |
|
|
bool SetDataBits(int n,bool bApplySettingsNow=true);
|
68 |
|
|
bool SetStopBits(StopBitsType n,bool bApplySettingsNow=true);
|
69 |
|
|
bool SetXONXOFFFlowControl(bool b,bool bApplySettingsNow=true);
|
70 |
|
|
bool SetRTSCTSFlowControl(bool b,bool bApplySettingsNow=true);
|
71 |
|
|
bool SetDSRDTRFlowControl(bool b,bool bApplySettingsNow=true);
|
72 |
|
|
bool SetReadTimeOuts(int nTotal,int nBetweenChars,bool bApplySettingsNow=true); // Times are in mSec
|
73 |
|
|
bool SetWriteTimeOuts(int nTotal,int nBetweenChars,bool bApplySettingsNow=true); // Times are in mSec
|
74 |
|
|
|
75 |
|
|
bool ApplySettings();
|
76 |
|
|
|
77 |
|
|
// Query the settings:
|
78 |
|
|
int GetParity() const { return m_bParity; }
|
79 |
|
|
int GetDataBits() const { return m_nDataBits; }
|
80 |
|
|
StopBitsType GetStopBits() const { return m_nStopBits; }
|
81 |
|
|
bool GetXONXOFFFlowControl() const { return m_bXONXOFFFlowControl; }
|
82 |
|
|
bool GetRTSCTSFlowControl() const { return m_bRTSCTSFlowControl; }
|
83 |
|
|
bool GetDSRDTRFlowControl() const { return m_bDSRDTRFlowControl; }
|
84 |
|
|
unsigned int GetBaud() const { return m_nBaud; }
|
85 |
|
|
bool GetReadTimeOuts(int &nTotal,int &nBetweenChars) const {nTotal=m_nTotalReadTimeout; nBetweenChars=m_nInterCharReadTimeout; return true; }// mSec
|
86 |
|
|
bool GetWriteTimeOuts(int &nTotal,int &nBetweenChars) const {nTotal=m_nTotalWriteTimeout; nBetweenChars=m_nInterCharWriteTimeout; return true; }// mSec
|
87 |
|
|
bool GetBlockingReads() const { return m_bBlockingReads; }
|
88 |
|
|
bool Close();
|
89 |
|
|
|
90 |
|
|
// Clear the serial buffer:
|
91 |
|
|
bool Flush (void);
|
92 |
|
|
|
93 |
|
|
// Use to test success after opening with the ctor:
|
94 |
|
|
bool Ok() const { return 0!=m_pHandle; }
|
95 |
|
|
|
96 |
|
|
// Will read up to the length provided:
|
97 |
|
|
bool Read (void *pBuf,unsigned int nSize,unsigned int &nRead);
|
98 |
|
|
bool Write(void *pBuf,unsigned int nSize,unsigned int &nWritten);
|
99 |
|
|
|
100 |
|
|
// Use in the event of an error that needs to be cleared before the next operation:
|
101 |
|
|
bool ClearError();
|
102 |
|
|
|
103 |
|
|
// Set blocking/non-blocking
|
104 |
|
|
bool SetBlockingReads(bool b,bool bApplySettingsNow=true);
|
105 |
|
|
|
106 |
|
|
// Return last error
|
107 |
|
|
int Error() const { return m_nErr; }
|
108 |
|
|
|
109 |
|
|
// Return last error, translated to a string
|
110 |
|
|
String ErrString() const;
|
111 |
|
|
|
112 |
|
|
protected:
|
113 |
|
|
// The last error:
|
114 |
|
|
int m_nErr
|
115 |
|
|
;
|
116 |
|
|
// Remember the error
|
117 |
|
|
void SaveError() {
|
118 |
|
|
#ifdef _WIN32
|
119 |
|
|
m_nErr=WSAGetLastError();
|
120 |
|
|
#else // UNIX
|
121 |
|
|
m_nErr=errno;
|
122 |
|
|
#endif
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
// Line characteristics:
|
126 |
|
|
void *m_pHandle;
|
127 |
|
|
int m_nDataBits;
|
128 |
|
|
StopBitsType m_nStopBits;
|
129 |
|
|
bool m_bXONXOFFFlowControl;
|
130 |
|
|
bool m_bRTSCTSFlowControl;
|
131 |
|
|
bool m_bDSRDTRFlowControl;
|
132 |
|
|
bool m_bParity;
|
133 |
|
|
unsigned int m_nBaud;
|
134 |
|
|
int m_nTotalReadTimeout,m_nTotalWriteTimeout;
|
135 |
|
|
int m_nInterCharReadTimeout,m_nInterCharWriteTimeout;
|
136 |
|
|
bool m_bBlockingReads;
|
137 |
|
|
String m_strPort;
|
138 |
|
|
};
|
139 |
|
|
#endif
|