source: ntrip/branches/BNC_2.12/src/serial/qextserialbase.cpp@ 8051

Last change on this file since 8051 was 1317, checked in by mervart, 15 years ago

* empty log message *

File size: 6.5 KB
Line 
1
2#include "qextserialbase.h"
3
4/*!
5\class QextSerialBase
6\version 1.0.0
7\author Stefan Sander
8
9A common base class for Win_QextSerialBase, Posix_QextSerialBase and QextSerialPort.
10*/
11#ifdef QT_THREAD_SUPPORT
12QMutex* QextSerialBase::mutex=NULL;
13unsigned long QextSerialBase::refCount=0;
14#endif
15
16/*!
17\fn QextSerialBase::QextSerialBase()
18Default constructor.
19*/
20QextSerialBase::QextSerialBase()
21 : QIODevice()
22{
23
24#ifdef _TTY_WIN_
25 setPortName("COM1");
26
27#elif defined(_TTY_IRIX_)
28 setPortName("/dev/ttyf1");
29
30#elif defined(_TTY_HPUX_)
31 setPortName("/dev/tty1p0");
32
33#elif defined(_TTY_SUN_)
34 setPortName("/dev/ttya");
35
36#elif defined(_TTY_DIGITAL_)
37 setPortName("/dev/tty01");
38
39#elif defined(_TTY_FREEBSD_)
40 setPortName("/dev/ttyd1");
41
42#else
43 setPortName("/dev/ttyS0");
44#endif
45
46 construct();
47}
48
49/*!
50\fn QextSerialBase::QextSerialBase(const QString & name)
51Construct a port and assign it to the device specified by the name parameter.
52*/
53QextSerialBase::QextSerialBase(const QString & name)
54 : QIODevice()
55{
56 setPortName(name);
57 construct();
58}
59
60/*!
61\fn QextSerialBase::~QextSerialBase()
62Standard destructor.
63*/
64QextSerialBase::~QextSerialBase()
65{
66
67#ifdef QT_THREAD_SUPPORT
68 refCount--;
69 if (mutex && refCount==0) {
70 delete mutex;
71 mutex=NULL;
72 }
73#endif
74
75}
76
77/*!
78\fn void QextSerialBase::construct()
79Common constructor function for setting up default port settings.
80(115200 Baud, 8N1, Hardware flow control where supported, otherwise no flow control, and 500 ms timeout).
81*/
82void QextSerialBase::construct()
83{
84 Settings.BaudRate=BAUD115200;
85 Settings.DataBits=DATA_8;
86 Settings.Parity=PAR_NONE;
87 Settings.StopBits=STOP_1;
88 Settings.FlowControl=FLOW_HARDWARE;
89 Settings.Timeout_Sec=0;
90 Settings.Timeout_Millisec=500;
91
92#ifdef QT_THREAD_SUPPORT
93 if (!mutex) {
94 mutex=new QMutex( QMutex::Recursive );
95 }
96 refCount++;
97#endif
98
99 setOpenMode(QIODevice::NotOpen);
100}
101
102/*!
103\fn void QextSerialBase::setPortName(const QString & name)
104Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0".
105*/
106void QextSerialBase::setPortName(const QString & name)
107{
108 port = name;
109}
110
111/*!
112\fn QString QextSerialBase::portName() const
113Returns the name set by setPortName().
114*/
115QString QextSerialBase::portName() const
116{
117 return port;
118}
119
120/*!
121\fn BaudRateType QextSerialBase::baudRate(void) const
122Returns the baud rate of the serial port. For a list of possible return values see
123the definition of the enum BaudRateType.
124*/
125BaudRateType QextSerialBase::baudRate(void) const
126{
127 return Settings.BaudRate;
128}
129
130/*!
131\fn DataBitsType QextSerialBase::dataBits() const
132Returns the number of data bits used by the port. For a list of possible values returned by
133this function, see the definition of the enum DataBitsType.
134*/
135DataBitsType QextSerialBase::dataBits() const
136{
137 return Settings.DataBits;
138}
139
140/*!
141\fn ParityType QextSerialBase::parity() const
142Returns the type of parity used by the port. For a list of possible values returned by
143this function, see the definition of the enum ParityType.
144*/
145ParityType QextSerialBase::parity() const
146{
147 return Settings.Parity;
148}
149
150/*!
151\fn StopBitsType QextSerialBase::stopBits() const
152Returns the number of stop bits used by the port. For a list of possible return values, see
153the definition of the enum StopBitsType.
154*/
155StopBitsType QextSerialBase::stopBits() const
156{
157 return Settings.StopBits;
158}
159
160/*!
161\fn FlowType QextSerialBase::flowControl() const
162Returns the type of flow control used by the port. For a list of possible values returned
163by this function, see the definition of the enum FlowType.
164*/
165FlowType QextSerialBase::flowControl() const
166{
167 return Settings.FlowControl;
168}
169
170/*!
171\fn bool QextSerialBase::isSequential() const
172Returns true if device is sequential, otherwise returns false. Serial port is sequential device
173so this function always returns true. Check QIODevice::isSequential() documentation for more
174information.
175*/
176bool QextSerialBase::isSequential() const
177{
178 return true;
179}
180
181/*!
182\fn bool QextSerialBase::atEnd() const
183This function will return true if the input buffer is empty (or on error), and false otherwise.
184Call QextSerialBase::lastError() for error information.
185*/
186bool QextSerialBase::atEnd() const
187{
188 if (size()) {
189 return true;
190 }
191 return false;
192}
193
194/*!
195\fn qint64 QextSerialBase::readLine(char * data, qint64 maxSize)
196This function will read a line of buffered input from the port, stopping when either maxSize bytes
197have been read, the port has no more data available, or a newline is encountered.
198The value returned is the length of the string that was read.
199*/
200qint64 QextSerialBase::readLine(char * data, qint64 maxSize)
201{
202 qint64 numBytes = bytesAvailable();
203 char* pData = data;
204
205 if (maxSize < 2) //maxSize must be larger than 1
206 return -1;
207
208 /*read a byte at a time for MIN(bytesAvail, maxSize - 1) iterations, or until a newline*/
209 while (pData<(data+numBytes) && --maxSize) {
210 readData(pData, 1);
211 if (*pData++ == '\n') {
212 break;
213 }
214 }
215 *pData='\0';
216
217 /*return size of data read*/
218 return (pData-data);
219}
220
221/*!
222\fn ulong QextSerialBase::lastError() const
223Returns the code for the last error encountered by the port, or E_NO_ERROR if the last port
224operation was successful. Possible error codes are:
225
226\verbatim
227Error Explanation
228--------------------------- -------------------------------------------------------------
229E_NO_ERROR No Error has occured
230E_INVALID_FD Invalid file descriptor (port was not opened correctly)
231E_NO_MEMORY Unable to allocate memory tables (POSIX)
232E_CAUGHT_NON_BLOCKED_SIGNAL Caught a non-blocked signal (POSIX)
233E_PORT_TIMEOUT Operation timed out (POSIX)
234E_INVALID_DEVICE The file opened by the port is not a character device (POSIX)
235E_BREAK_CONDITION The port detected a break condition
236E_FRAMING_ERROR The port detected a framing error
237 (usually caused by incorrect baud rate settings)
238E_IO_ERROR There was an I/O error while communicating with the port
239E_BUFFER_OVERRUN Character buffer overrun
240E_RECEIVE_OVERFLOW Receive buffer overflow
241E_RECEIVE_PARITY_ERROR The port detected a parity error in the received data
242E_TRANSMIT_OVERFLOW Transmit buffer overflow
243E_READ_FAILED General read operation failure
244E_WRITE_FAILED General write operation failure
245\endverbatim
246*/
247ulong QextSerialBase::lastError() const
248{
249 return lastErr;
250}
Note: See TracBrowser for help on using the repository browser.