|
qCNC 0.82-alpha
|
00001 00002 00003 #include <stdio.h> 00004 #include "qextserialport.h" 00005 00029 QextSerialPort::QextSerialPort(QextSerialPort::QueryMode mode) 00030 : QIODevice() 00031 { 00032 00033 #ifdef Q_OS_WIN 00034 setPortName("COM1"); 00035 00036 #elif defined(_TTY_IRIX_) 00037 setPortName("/dev/ttyf1"); 00038 00039 #elif defined(_TTY_HPUX_) 00040 setPortName("/dev/tty1p0"); 00041 00042 #elif defined(_TTY_SUN_) 00043 setPortName("/dev/ttya"); 00044 00045 #elif defined(_TTY_DIGITAL_) 00046 setPortName("/dev/tty01"); 00047 00048 #elif defined(_TTY_FREEBSD_) 00049 setPortName("/dev/ttyd1"); 00050 00051 #elif defined(_TTY_OPENBSD_) 00052 setPortName("/dev/tty00"); 00053 00054 #else 00055 setPortName("/dev/ttyS0"); 00056 #endif 00057 00058 construct(); 00059 setQueryMode(mode); 00060 platformSpecificInit(); 00061 } 00062 00068 QextSerialPort::QextSerialPort(const QString & name, QextSerialPort::QueryMode mode) 00069 : QIODevice() 00070 { 00071 construct(); 00072 setQueryMode(mode); 00073 setPortName(name); 00074 platformSpecificInit(); 00075 } 00076 00080 QextSerialPort::QextSerialPort(const PortSettings& settings, QextSerialPort::QueryMode mode) 00081 : QIODevice() 00082 { 00083 construct(); 00084 setBaudRate(settings.BaudRate); 00085 setDataBits(settings.DataBits); 00086 setParity(settings.Parity); 00087 setStopBits(settings.StopBits); 00088 setFlowControl(settings.FlowControl); 00089 setTimeout(settings.Timeout_Millisec); 00090 setQueryMode(mode); 00091 platformSpecificInit(); 00092 } 00093 00097 QextSerialPort::QextSerialPort(const QString & name, const PortSettings& settings, QextSerialPort::QueryMode mode) 00098 : QIODevice() 00099 { 00100 construct(); 00101 setPortName(name); 00102 setBaudRate(settings.BaudRate); 00103 setDataBits(settings.DataBits); 00104 setParity(settings.Parity); 00105 setStopBits(settings.StopBits); 00106 setFlowControl(settings.FlowControl); 00107 setTimeout(settings.Timeout_Millisec); 00108 setQueryMode(mode); 00109 platformSpecificInit(); 00110 } 00111 00116 void QextSerialPort::construct() 00117 { 00118 lastErr = E_NO_ERROR; 00119 Settings.BaudRate=BAUD115200; 00120 Settings.DataBits=DATA_8; 00121 Settings.Parity=PAR_NONE; 00122 Settings.StopBits=STOP_1; 00123 Settings.FlowControl=FLOW_HARDWARE; 00124 Settings.Timeout_Millisec=500; 00125 mutex = new QMutex( QMutex::Recursive ); 00126 setOpenMode(QIODevice::NotOpen); 00127 } 00128 00129 void QextSerialPort::setQueryMode(QueryMode mechanism) 00130 { 00131 _queryMode = mechanism; 00132 } 00133 00137 void QextSerialPort::setPortName(const QString & name) 00138 { 00139 #ifdef Q_OS_WIN 00140 port = fullPortNameWin( name ); 00141 #else 00142 port = name; 00143 #endif 00144 } 00145 00149 QString QextSerialPort::portName() const 00150 { 00151 return port; 00152 } 00153 00159 QByteArray QextSerialPort::readAll() 00160 { 00161 int avail = this->bytesAvailable(); 00162 return (avail > 0) ? this->read(avail) : QByteArray(); 00163 } 00164 00169 BaudRateType QextSerialPort::baudRate(void) const 00170 { 00171 return Settings.BaudRate; 00172 } 00173 00178 DataBitsType QextSerialPort::dataBits() const 00179 { 00180 return Settings.DataBits; 00181 } 00182 00187 ParityType QextSerialPort::parity() const 00188 { 00189 return Settings.Parity; 00190 } 00191 00196 StopBitsType QextSerialPort::stopBits() const 00197 { 00198 return Settings.StopBits; 00199 } 00200 00205 FlowType QextSerialPort::flowControl() const 00206 { 00207 return Settings.FlowControl; 00208 } 00209 00215 bool QextSerialPort::isSequential() const 00216 { 00217 return true; 00218 } 00219 00220 QString QextSerialPort::errorString() 00221 { 00222 switch(lastErr) 00223 { 00224 case E_NO_ERROR: return "No Error has occurred"; 00225 case E_INVALID_FD: return "Invalid file descriptor (port was not opened correctly)"; 00226 case E_NO_MEMORY: return "Unable to allocate memory tables (POSIX)"; 00227 case E_CAUGHT_NON_BLOCKED_SIGNAL: return "Caught a non-blocked signal (POSIX)"; 00228 case E_PORT_TIMEOUT: return "Operation timed out (POSIX)"; 00229 case E_INVALID_DEVICE: return "The file opened by the port is not a valid device"; 00230 case E_BREAK_CONDITION: return "The port detected a break condition"; 00231 case E_FRAMING_ERROR: return "The port detected a framing error (usually caused by incorrect baud rate settings)"; 00232 case E_IO_ERROR: return "There was an I/O error while communicating with the port"; 00233 case E_BUFFER_OVERRUN: return "Character buffer overrun"; 00234 case E_RECEIVE_OVERFLOW: return "Receive buffer overflow"; 00235 case E_RECEIVE_PARITY_ERROR: return "The port detected a parity error in the received data"; 00236 case E_TRANSMIT_OVERFLOW: return "Transmit buffer overflow"; 00237 case E_READ_FAILED: return "General read operation failure"; 00238 case E_WRITE_FAILED: return "General write operation failure"; 00239 case E_FILE_NOT_FOUND: return "The "+this->portName()+" file doesn't exists"; 00240 default: return QString("Unknown error: %1").arg(lastErr); 00241 } 00242 } 00243 00247 QextSerialPort::~QextSerialPort() 00248 { 00249 if (isOpen()) { 00250 close(); 00251 } 00252 platformSpecificDestruct(); 00253 delete mutex; 00254 }