Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
674 | KeyOz | 1 | |
2 | #include "qextserialbase.h" |
||
3 | |||
4 | /*! |
||
5 | \class QextSerialBase |
||
6 | \version 1.0.0 |
||
7 | \author Stefan Sander |
||
8 | |||
9 | A common base class for Win_QextSerialBase, Posix_QextSerialBase and QextSerialPort. |
||
10 | */ |
||
11 | #ifdef QT_THREAD_SUPPORT |
||
12 | QMutex* QextSerialBase::mutex=NULL; |
||
13 | unsigned long QextSerialBase::refCount=0; |
||
14 | #endif |
||
15 | |||
16 | /*! |
||
17 | \fn QextSerialBase::QextSerialBase() |
||
18 | Default constructor. |
||
19 | */ |
||
20 | QextSerialBase::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) |
||
51 | Construct a port and assign it to the device specified by the name parameter. |
||
52 | */ |
||
53 | QextSerialBase::QextSerialBase(const QString & name) |
||
54 | : QIODevice() |
||
55 | { |
||
56 | setPortName(name); |
||
57 | construct(); |
||
58 | } |
||
59 | |||
60 | /*! |
||
61 | \fn QextSerialBase::~QextSerialBase() |
||
62 | Standard destructor. |
||
63 | */ |
||
64 | QextSerialBase::~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() |
||
79 | Common constructor function for setting up default port settings. |
||
80 | (9600 Baud, 8N1, no flow control where supported, otherwise no flow control, and 20 ms timeout). |
||
81 | */ |
||
82 | void QextSerialBase::construct() |
||
83 | { |
||
84 | Settings.BaudRate=BAUD9600; |
||
85 | Settings.DataBits=DATA_8; |
||
86 | Settings.Parity=PAR_NONE; |
||
87 | Settings.StopBits=STOP_1; |
||
88 | Settings.FlowControl=FLOW_OFF; |
||
89 | Settings.Timeout_Sec=0; |
||
90 | Settings.Timeout_Millisec=20; |
||
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) |
||
104 | Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0". |
||
105 | */ |
||
106 | void QextSerialBase::setPortName(const QString & name) |
||
107 | { |
||
108 | port = name; |
||
109 | } |
||
110 | |||
111 | /*! |
||
112 | \fn QString QextSerialBase::portName() const |
||
113 | Returns the name set by setPortName(). |
||
114 | */ |
||
115 | QString QextSerialBase::portName() const |
||
116 | { |
||
117 | return port; |
||
118 | } |
||
119 | |||
120 | /*! |
||
121 | \fn BaudRateType QextSerialBase::baudRate(void) const |
||
122 | Returns the baud rate of the serial port. For a list of possible return values see |
||
123 | the definition of the enum BaudRateType. |
||
124 | */ |
||
125 | BaudRateType QextSerialBase::baudRate(void) const |
||
126 | { |
||
127 | return Settings.BaudRate; |
||
128 | } |
||
129 | |||
130 | /*! |
||
131 | \fn DataBitsType QextSerialBase::dataBits() const |
||
132 | Returns the number of data bits used by the port. For a list of possible values returned by |
||
133 | this function, see the definition of the enum DataBitsType. |
||
134 | */ |
||
135 | DataBitsType QextSerialBase::dataBits() const |
||
136 | { |
||
137 | return Settings.DataBits; |
||
138 | } |
||
139 | |||
140 | /*! |
||
141 | \fn ParityType QextSerialBase::parity() const |
||
142 | Returns the type of parity used by the port. For a list of possible values returned by |
||
143 | this function, see the definition of the enum ParityType. |
||
144 | */ |
||
145 | ParityType QextSerialBase::parity() const |
||
146 | { |
||
147 | return Settings.Parity; |
||
148 | } |
||
149 | |||
150 | /*! |
||
151 | \fn StopBitsType QextSerialBase::stopBits() const |
||
152 | Returns the number of stop bits used by the port. For a list of possible return values, see |
||
153 | the definition of the enum StopBitsType. |
||
154 | */ |
||
155 | StopBitsType QextSerialBase::stopBits() const |
||
156 | { |
||
157 | return Settings.StopBits; |
||
158 | } |
||
159 | |||
160 | /*! |
||
161 | \fn FlowType QextSerialBase::flowControl() const |
||
162 | Returns the type of flow control used by the port. For a list of possible values returned |
||
163 | by this function, see the definition of the enum FlowType. |
||
164 | */ |
||
165 | FlowType QextSerialBase::flowControl() const |
||
166 | { |
||
167 | return Settings.FlowControl; |
||
168 | } |
||
169 | |||
170 | /*! |
||
171 | \fn bool QextSerialBase::isSequential() const |
||
172 | Returns true if device is sequential, otherwise returns false. Serial port is sequential device |
||
173 | so this function always returns true. Check QIODevice::isSequential() documentation for more |
||
174 | information. |
||
175 | */ |
||
176 | bool QextSerialBase::isSequential() const |
||
177 | { |
||
178 | return true; |
||
179 | } |
||
180 | |||
181 | /*! |
||
182 | \fn bool QextSerialBase::atEnd() const |
||
183 | This function will return true if the input buffer is empty (or on error), and false otherwise. |
||
184 | Call QextSerialBase::lastError() for error information. |
||
185 | */ |
||
186 | bool 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) |
||
196 | This function will read a line of buffered input from the port, stopping when either maxSize bytes |
||
197 | have been read, the port has no more data available, or a newline is encountered. |
||
198 | The value returned is the length of the string that was read. |
||
199 | */ |
||
200 | qint64 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 |
||
223 | Returns the code for the last error encountered by the port, or E_NO_ERROR if the last port |
||
224 | operation was successful. Possible error codes are: |
||
225 | |||
226 | \verbatim |
||
227 | Error Explanation |
||
228 | --------------------------- ------------------------------------------------------------- |
||
229 | E_NO_ERROR No Error has occured |
||
230 | E_INVALID_FD Invalid file descriptor (port was not opened correctly) |
||
231 | E_NO_MEMORY Unable to allocate memory tables (POSIX) |
||
232 | E_CAUGHT_NON_BLOCKED_SIGNAL Caught a non-blocked signal (POSIX) |
||
233 | E_PORT_TIMEOUT Operation timed out (POSIX) |
||
234 | E_INVALID_DEVICE The file opened by the port is not a character device (POSIX) |
||
235 | E_BREAK_CONDITION The port detected a break condition |
||
236 | E_FRAMING_ERROR The port detected a framing error |
||
237 | (usually caused by incorrect baud rate settings) |
||
238 | E_IO_ERROR There was an I/O error while communicating with the port |
||
239 | E_BUFFER_OVERRUN Character buffer overrun |
||
240 | E_RECEIVE_OVERFLOW Receive buffer overflow |
||
241 | E_RECEIVE_PARITY_ERROR The port detected a parity error in the received data |
||
242 | E_TRANSMIT_OVERFLOW Transmit buffer overflow |
||
243 | E_READ_FAILED General read operation failure |
||
244 | E_WRITE_FAILED General write operation failure |
||
245 | \endverbatim |
||
246 | */ |
||
247 | ulong QextSerialBase::lastError() const |
||
248 | { |
||
249 | return lastErr; |
||
250 | } |