#include <posix_qextserialport.h>
Inheritance diagram for Posix_QextSerialPort:

Public Member Functions | |
| Posix_QextSerialPort () | |
| Posix_QextSerialPort (const Posix_QextSerialPort &s) | |
| Posix_QextSerialPort (const QString &name) | |
| Posix_QextSerialPort (const PortSettings &settings) | |
| Posix_QextSerialPort (const QString &name, const PortSettings &settings) | |
| Posix_QextSerialPort & | operator= (const Posix_QextSerialPort &s) |
| virtual | ~Posix_QextSerialPort () |
| virtual void | setBaudRate (BaudRateType) |
| virtual void | setDataBits (DataBitsType) |
| virtual void | setParity (ParityType) |
| virtual void | setStopBits (StopBitsType) |
| virtual void | setFlowControl (FlowType) |
| virtual void | setTimeout (ulong, ulong) |
| virtual bool | open (OpenMode mode=0) |
| virtual void | close () |
| virtual void | flush () |
| virtual qint64 | size () const |
| virtual qint64 | bytesAvailable () |
| virtual bool | getChar (char *c) |
| virtual bool | putChar (char c) |
| virtual void | ungetChar (char c) |
| virtual void | translateError (ulong error) |
| virtual void | setDtr (bool set=true) |
| virtual void | setRts (bool set=true) |
| virtual ulong | lineStatus () |
| virtual qint64 | readData (char *data, qint64 maxSize) |
| virtual qint64 | writeData (const char *data, qint64 maxSize) |
| virtual void | construct () |
| virtual void | setPortName (const QString &name) |
| virtual QString | portName () const |
| virtual BaudRateType | baudRate () const |
| virtual DataBitsType | dataBits () const |
| virtual ParityType | parity () const |
| virtual StopBitsType | stopBits () const |
| virtual FlowType | flowControl () const |
| virtual bool | isOpen () const |
| virtual bool | atEnd () const |
| virtual qint64 | readLine (char *data, qint64 maxSize) |
| virtual ulong | lastError () const |
Protected Attributes | |
| QFile * | Posix_File |
| termios | Posix_CommConfig |
| timeval | Posix_Timeout |
| timeval | Posix_Copy_Timeout |
| QString | port |
| bool | portOpen |
| PortSettings | Settings |
| ulong | lastErr |
Static Protected Attributes | |
| static QMutex * | mutex = NULL |
| static ulong | refCount = 0 |
|
|
Default constructor. Note that the name of the device used by a QextSerialPort constructed with this constructor will be determined by #defined constants, or lack thereof - the default behavior is the same as _TTY_LINUX_. Possible naming conventions and their associated constants are:
Constant Used By Naming Convention ---------- ------------- ------------------------ _TTY_WIN_ Windows COM1, COM2 _TTY_IRIX_ SGI/IRIX /dev/ttyf1, /dev/ttyf2 _TTY_HPUX_ HP-UX /dev/tty1p0, /dev/tty2p0 _TTY_SUN_ SunOS/Solaris /dev/ttya, /dev/ttyb _TTY_DIGITAL_ Digital UNIX /dev/tty01, /dev/tty02 _TTY_FREEBSD_ FreeBSD /dev/ttyd0, /dev/ttyd1 _TTY_LINUX_ Linux /dev/ttyS0, /dev/ttyS1 <none> Linux /dev/ttyS0, /dev/ttyS1 This constructor assigns the device name to the name of the first port on the specified system. See the other constructors if you need to open a different port. 00041 : QextSerialBase() 00042 { 00043 Posix_File=new QFile(); 00044 }
|
|
|
Copy constructor. 00051 : QextSerialBase(s.port) 00052 { 00053 port = s.port; 00054 portOpen=s.portOpen; 00055 Settings.BaudRate=s.Settings.BaudRate; 00056 Settings.DataBits=s.Settings.DataBits; 00057 Settings.Parity=s.Settings.Parity; 00058 Settings.StopBits=s.Settings.StopBits; 00059 Settings.FlowControl=s.Settings.FlowControl; 00060 lastErr=s.lastErr; 00061 00062 Posix_File=new QFile(); 00063 Posix_File=s.Posix_File; 00064 memcpy(&Posix_Timeout, &s.Posix_Timeout, sizeof(struct timeval)); 00065 memcpy(&Posix_Copy_Timeout, &s.Posix_Copy_Timeout, sizeof(struct timeval)); 00066 memcpy(&Posix_CommConfig, &s.Posix_CommConfig, sizeof(struct termios)); 00067 }
|
|
|
Constructs a serial port attached to the port specified by name. name is the name of the device, which is windowsystem-specific, e.g."COM1" or "/dev/ttyS0". 00076 : QextSerialBase(name) 00077 { 00078 Posix_File=new QFile(); 00079 }
|
|
|
Constructs a port with default name and specified settings. 00086 : QextSerialBase() 00087 { 00088 setBaudRate(settings.BaudRate); 00089 setDataBits(settings.DataBits); 00090 setParity(settings.Parity); 00091 setStopBits(settings.StopBits); 00092 setFlowControl(settings.FlowControl); 00093 00094 Posix_File=new QFile(); 00095 setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec); 00096 }
|
|
||||||||||||
|
Constructs a port with specified name and settings. 00103 : QextSerialBase(name) 00104 { 00105 setBaudRate(settings.BaudRate); 00106 setDataBits(settings.DataBits); 00107 setParity(settings.Parity); 00108 setStopBits(settings.StopBits); 00109 setFlowControl(settings.FlowControl); 00110 00111 Posix_File=new QFile(); 00112 setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec); 00113 }
|
|
|
Standard destructor. 00142 {
00143 if (portOpen) {
00144 close();
00145 }
00146 Posix_File->close();
00147 delete Posix_File;
00148 }
|
|
|
Override the = operator. 00120 {
00121 port = s.port;
00122 portOpen=s.portOpen;
00123 Settings.BaudRate=s.Settings.BaudRate;
00124 Settings.DataBits=s.Settings.DataBits;
00125 Settings.Parity=s.Settings.Parity;
00126 Settings.StopBits=s.Settings.StopBits;
00127 Settings.FlowControl=s.Settings.FlowControl;
00128 lastErr=s.lastErr;
00129
00130 Posix_File=s.Posix_File;
00131 memcpy(&Posix_Timeout, &(s.Posix_Timeout), sizeof(struct timeval));
00132 memcpy(&Posix_Copy_Timeout, &(s.Posix_Copy_Timeout), sizeof(struct timeval));
00133 memcpy(&Posix_CommConfig, &(s.Posix_CommConfig), sizeof(struct termios));
00134 return *this;
00135 }
|
|
|
Sets the baud rate of the serial port. Note that not all rates are applicable on all platforms. The following table shows translations of the various baud rate constants on Windows(including NT/2000) and POSIX platforms. Speeds marked with an * are speeds that are usable on both Windows and POSIX.
RATE Windows Speed POSIX Speed ----------- ------------- ----------- BAUD50 110 50 BAUD75 110 75 *BAUD110 110 110 BAUD134 110 134.5 BAUD150 110 150 BAUD200 110 200 *BAUD300 300 300 *BAUD600 600 600 *BAUD1200 1200 1200 BAUD1800 1200 1800 *BAUD2400 2400 2400 *BAUD4800 4800 4800 *BAUD9600 9600 9600 BAUD14400 14400 9600 *BAUD19200 19200 19200 *BAUD38400 38400 38400 BAUD56000 56000 38400 *BAUD57600 57600 57600 BAUD76800 57600 76800 *BAUD115200 115200 115200 BAUD128000 128000 115200 BAUD256000 256000 115200 Implements QextSerialBase. 00190 {
00191 LOCK_MUTEX();
00192 if (Settings.BaudRate!=baudRate) {
00193 switch (baudRate) {
00194 case BAUD14400:
00195 Settings.BaudRate=BAUD9600;
00196 break;
00197
00198 case BAUD56000:
00199 Settings.BaudRate=BAUD38400;
00200 break;
00201
00202 case BAUD76800:
00203
00204 #ifndef B76800
00205 Settings.BaudRate=BAUD57600;
00206 #else
00207 Settings.BaudRate=baudRate;
00208 #endif
00209 break;
00210
00211 case BAUD128000:
00212 case BAUD256000:
00213 Settings.BaudRate=BAUD115200;
00214 break;
00215
00216 default:
00217 Settings.BaudRate=baudRate;
00218 break;
00219 }
00220 }
00221 if (portOpen) {
00222 switch (baudRate) {
00223
00224 /*50 baud*/
00225 case BAUD50:
00226 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 50 baud operation.");
00227 #ifdef CBAUD
00228 Posix_CommConfig.c_cflag&=(~CBAUD);
00229 Posix_CommConfig.c_cflag|=B50;
00230 #else
00231 cfsetispeed(&Posix_CommConfig, B50);
00232 cfsetospeed(&Posix_CommConfig, B50);
00233 #endif
00234 break;
00235
00236 /*75 baud*/
00237 case BAUD75:
00238 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 75 baud operation.");
00239 #ifdef CBAUD
00240 Posix_CommConfig.c_cflag&=(~CBAUD);
00241 Posix_CommConfig.c_cflag|=B75;
00242 #else
00243 cfsetispeed(&Posix_CommConfig, B75);
00244 cfsetospeed(&Posix_CommConfig, B75);
00245 #endif
00246 break;
00247
00248 /*110 baud*/
00249 case BAUD110:
00250 #ifdef CBAUD
00251 Posix_CommConfig.c_cflag&=(~CBAUD);
00252 Posix_CommConfig.c_cflag|=B110;
00253 #else
00254 cfsetispeed(&Posix_CommConfig, B110);
00255 cfsetospeed(&Posix_CommConfig, B110);
00256 #endif
00257 break;
00258
00259 /*134.5 baud*/
00260 case BAUD134:
00261 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 134.5 baud operation.");
00262 #ifdef CBAUD
00263 Posix_CommConfig.c_cflag&=(~CBAUD);
00264 Posix_CommConfig.c_cflag|=B134;
00265 #else
00266 cfsetispeed(&Posix_CommConfig, B134);
00267 cfsetospeed(&Posix_CommConfig, B134);
00268 #endif
00269 break;
00270
00271 /*150 baud*/
00272 case BAUD150:
00273 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 150 baud operation.");
00274 #ifdef CBAUD
00275 Posix_CommConfig.c_cflag&=(~CBAUD);
00276 Posix_CommConfig.c_cflag|=B150;
00277 #else
00278 cfsetispeed(&Posix_CommConfig, B150);
00279 cfsetospeed(&Posix_CommConfig, B150);
00280 #endif
00281 break;
00282
00283 /*200 baud*/
00284 case BAUD200:
00285 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows does not support 200 baud operation.");
00286 #ifdef CBAUD
00287 Posix_CommConfig.c_cflag&=(~CBAUD);
00288 Posix_CommConfig.c_cflag|=B200;
00289 #else
00290 cfsetispeed(&Posix_CommConfig, B200);
00291 cfsetospeed(&Posix_CommConfig, B200);
00292 #endif
00293 break;
00294
00295 /*300 baud*/
00296 case BAUD300:
00297 #ifdef CBAUD
00298 Posix_CommConfig.c_cflag&=(~CBAUD);
00299 Posix_CommConfig.c_cflag|=B300;
00300 #else
00301 cfsetispeed(&Posix_CommConfig, B300);
00302 cfsetospeed(&Posix_CommConfig, B300);
00303 #endif
00304 break;
00305
00306 /*600 baud*/
00307 case BAUD600:
00308 #ifdef CBAUD
00309 Posix_CommConfig.c_cflag&=(~CBAUD);
00310 Posix_CommConfig.c_cflag|=B600;
00311 #else
00312 cfsetispeed(&Posix_CommConfig, B600);
00313 cfsetospeed(&Posix_CommConfig, B600);
00314 #endif
00315 break;
00316
00317 /*1200 baud*/
00318 case BAUD1200:
00319 #ifdef CBAUD
00320 Posix_CommConfig.c_cflag&=(~CBAUD);
00321 Posix_CommConfig.c_cflag|=B1200;
00322 #else
00323 cfsetispeed(&Posix_CommConfig, B1200);
00324 cfsetospeed(&Posix_CommConfig, B1200);
00325 #endif
00326 break;
00327
00328 /*1800 baud*/
00329 case BAUD1800:
00330 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows and IRIX do not support 1800 baud operation.");
00331 #ifdef CBAUD
00332 Posix_CommConfig.c_cflag&=(~CBAUD);
00333 Posix_CommConfig.c_cflag|=B1800;
00334 #else
00335 cfsetispeed(&Posix_CommConfig, B1800);
00336 cfsetospeed(&Posix_CommConfig, B1800);
00337 #endif
00338 break;
00339
00340 /*2400 baud*/
00341 case BAUD2400:
00342 #ifdef CBAUD
00343 Posix_CommConfig.c_cflag&=(~CBAUD);
00344 Posix_CommConfig.c_cflag|=B2400;
00345 #else
00346 cfsetispeed(&Posix_CommConfig, B2400);
00347 cfsetospeed(&Posix_CommConfig, B2400);
00348 #endif
00349 break;
00350
00351 /*4800 baud*/
00352 case BAUD4800:
00353 #ifdef CBAUD
00354 Posix_CommConfig.c_cflag&=(~CBAUD);
00355 Posix_CommConfig.c_cflag|=B4800;
00356 #else
00357 cfsetispeed(&Posix_CommConfig, B4800);
00358 cfsetospeed(&Posix_CommConfig, B4800);
00359 #endif
00360 break;
00361
00362 /*9600 baud*/
00363 case BAUD9600:
00364 #ifdef CBAUD
00365 Posix_CommConfig.c_cflag&=(~CBAUD);
00366 Posix_CommConfig.c_cflag|=B9600;
00367 #else
00368 cfsetispeed(&Posix_CommConfig, B9600);
00369 cfsetospeed(&Posix_CommConfig, B9600);
00370 #endif
00371 break;
00372
00373 /*14400 baud*/
00374 case BAUD14400:
00375 TTY_WARNING("Posix_QextSerialPort: POSIX does not support 14400 baud operation. Switching to 9600 baud.");
00376 #ifdef CBAUD
00377 Posix_CommConfig.c_cflag&=(~CBAUD);
00378 Posix_CommConfig.c_cflag|=B9600;
00379 #else
00380 cfsetispeed(&Posix_CommConfig, B9600);
00381 cfsetospeed(&Posix_CommConfig, B9600);
00382 #endif
00383 break;
00384
00385 /*19200 baud*/
00386 case BAUD19200:
00387 #ifdef CBAUD
00388 Posix_CommConfig.c_cflag&=(~CBAUD);
00389 Posix_CommConfig.c_cflag|=B19200;
00390 #else
00391 cfsetispeed(&Posix_CommConfig, B19200);
00392 cfsetospeed(&Posix_CommConfig, B19200);
00393 #endif
00394 break;
00395
00396 /*38400 baud*/
00397 case BAUD38400:
00398 #ifdef CBAUD
00399 Posix_CommConfig.c_cflag&=(~CBAUD);
00400 Posix_CommConfig.c_cflag|=B38400;
00401 #else
00402 cfsetispeed(&Posix_CommConfig, B38400);
00403 cfsetospeed(&Posix_CommConfig, B38400);
00404 #endif
00405 break;
00406
00407 /*56000 baud*/
00408 case BAUD56000:
00409 TTY_WARNING("Posix_QextSerialPort: POSIX does not support 56000 baud operation. Switching to 38400 baud.");
00410 #ifdef CBAUD
00411 Posix_CommConfig.c_cflag&=(~CBAUD);
00412 Posix_CommConfig.c_cflag|=B38400;
00413 #else
00414 cfsetispeed(&Posix_CommConfig, B38400);
00415 cfsetospeed(&Posix_CommConfig, B38400);
00416 #endif
00417 break;
00418
00419 /*57600 baud*/
00420 case BAUD57600:
00421 #ifdef CBAUD
00422 Posix_CommConfig.c_cflag&=(~CBAUD);
00423 Posix_CommConfig.c_cflag|=B57600;
00424 #else
00425 cfsetispeed(&Posix_CommConfig, B57600);
00426 cfsetospeed(&Posix_CommConfig, B57600);
00427 #endif
00428 break;
00429
00430 /*76800 baud*/
00431 case BAUD76800:
00432 TTY_PORTABILITY_WARNING("Posix_QextSerialPort Portability Warning: Windows and some POSIX systems do not support 76800 baud operation.");
00433 #ifdef CBAUD
00434 Posix_CommConfig.c_cflag&=(~CBAUD);
00435
00436 #ifdef B76800
00437 Posix_CommConfig.c_cflag|=B76800;
00438 #else
00439 TTY_WARNING("Posix_QextSerialPort: Posix_QextSerialPort was compiled without 76800 baud support. Switching to 57600 baud.");
00440 Posix_CommConfig.c_cflag|=B57600;
00441 #endif //B76800
00442 #else //CBAUD
00443 #ifdef B76800
00444 cfsetispeed(&Posix_CommConfig, B76800);
00445 cfsetospeed(&Posix_CommConfig, B76800);
00446 #else
00447 TTY_WARNING("Posix_QextSerialPort: Posix_QextSerialPort was compiled without 76800 baud support. Switching to 57600 baud.");
00448 cfsetispeed(&Posix_CommConfig, B57600);
00449 cfsetospeed(&Posix_CommConfig, B57600);
00450 #endif //B76800
00451 #endif //CBAUD
00452 break;
00453
00454 /*115200 baud*/
00455 case BAUD115200:
00456 #ifdef CBAUD
00457 Posix_CommConfig.c_cflag&=(~CBAUD);
00458 Posix_CommConfig.c_cflag|=B115200;
00459 #else
00460 cfsetispeed(&Posix_CommConfig, B115200);
00461 cfsetospeed(&Posix_CommConfig, B115200);
00462 #endif
00463 break;
00464
00465 /*128000 baud*/
00466 case BAUD128000:
00467 TTY_WARNING("Posix_QextSerialPort: POSIX does not support 128000 baud operation. Switching to 115200 baud.");
00468 #ifdef CBAUD
00469 Posix_CommConfig.c_cflag&=(~CBAUD);
00470 Posix_CommConfig.c_cflag|=B115200;
00471 #else
00472 cfsetispeed(&Posix_CommConfig, B115200);
00473 cfsetospeed(&Posix_CommConfig, B115200);
00474 #endif
00475 break;
00476
00477 /*256000 baud*/
00478 case BAUD256000:
00479 TTY_WARNING("Posix_QextSerialPort: POSIX does not support 256000 baud operation. Switching to 115200 baud.");
00480 #ifdef CBAUD
00481 Posix_CommConfig.c_cflag&=(~CBAUD);
00482 Posix_CommConfig.c_cflag|=B115200;
00483 #else
00484 cfsetispeed(&Posix_CommConfig, B115200);
00485 cfsetospeed(&Posix_CommConfig, B115200);
00486 #endif
00487 break;
00488 }
00489 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00490 }
00491 UNLOCK_MUTEX();
00492 }
|
|
|
Sets the number of data bits used by the serial port. Possible values of dataBits are:
DATA_5 5 data bits
DATA_6 6 data bits
DATA_7 7 data bits
DATA_8 8 data bits
Implements QextSerialBase. 00513 {
00514 LOCK_MUTEX();
00515 if (Settings.DataBits!=dataBits) {
00516 if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) ||
00517 (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5) ||
00518 (Settings.Parity==PAR_SPACE && dataBits==DATA_8)) {
00519 }
00520 else {
00521 Settings.DataBits=dataBits;
00522 }
00523 }
00524 if (portOpen) {
00525 switch(dataBits) {
00526
00527 /*5 data bits*/
00528 case DATA_5:
00529 if (Settings.StopBits==STOP_2) {
00530 TTY_WARNING("Posix_QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
00531 }
00532 else {
00533 Settings.DataBits=dataBits;
00534 Posix_CommConfig.c_cflag&=(~CSIZE);
00535 Posix_CommConfig.c_cflag|=CS5;
00536 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00537 }
00538 break;
00539
00540 /*6 data bits*/
00541 case DATA_6:
00542 if (Settings.StopBits==STOP_1_5) {
00543 TTY_WARNING("Posix_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
00544 }
00545 else {
00546 Settings.DataBits=dataBits;
00547 Posix_CommConfig.c_cflag&=(~CSIZE);
00548 Posix_CommConfig.c_cflag|=CS6;
00549 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00550 }
00551 break;
00552
00553 /*7 data bits*/
00554 case DATA_7:
00555 if (Settings.StopBits==STOP_1_5) {
00556 TTY_WARNING("Posix_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
00557 }
00558 else {
00559 Settings.DataBits=dataBits;
00560 Posix_CommConfig.c_cflag&=(~CSIZE);
00561 Posix_CommConfig.c_cflag|=CS7;
00562 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00563 }
00564 break;
00565
00566 /*8 data bits*/
00567 case DATA_8:
00568 if (Settings.StopBits==STOP_1_5) {
00569 TTY_WARNING("Posix_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
00570 }
00571 else {
00572 Settings.DataBits=dataBits;
00573 Posix_CommConfig.c_cflag&=(~CSIZE);
00574 Posix_CommConfig.c_cflag|=CS8;
00575 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00576 }
00577 break;
00578 }
00579 }
00580 UNLOCK_MUTEX();
00581 }
|
|
|
Sets the parity associated with the serial port. The possible values of parity are:
PAR_SPACE Space Parity
PAR_MARK Mark Parity
PAR_NONE No Parity
PAR_EVEN Even Parity
PAR_ODD Odd Parity
Implements QextSerialBase. 00604 {
00605 LOCK_MUTEX();
00606 if (Settings.Parity!=parity) {
00607 if (parity==PAR_MARK || (parity==PAR_SPACE && Settings.DataBits==DATA_8)) {
00608 }
00609 else {
00610 Settings.Parity=parity;
00611 }
00612 }
00613 if (portOpen) {
00614 switch (parity) {
00615
00616 /*space parity*/
00617 case PAR_SPACE:
00618 if (Settings.DataBits==DATA_8) {
00619 TTY_PORTABILITY_WARNING("Posix_QextSerialPort: Space parity is only supported in POSIX with 7 or fewer data bits");
00620 }
00621 else {
00622
00623 /*space parity not directly supported - add an extra data bit to simulate it*/
00624 Posix_CommConfig.c_cflag&=~(PARENB|CSIZE);
00625 switch(Settings.DataBits) {
00626 case DATA_5:
00627 Settings.DataBits=DATA_6;
00628 Posix_CommConfig.c_cflag|=CS6;
00629 break;
00630
00631 case DATA_6:
00632 Settings.DataBits=DATA_7;
00633 Posix_CommConfig.c_cflag|=CS7;
00634 break;
00635
00636 case DATA_7:
00637 Settings.DataBits=DATA_8;
00638 Posix_CommConfig.c_cflag|=CS8;
00639 break;
00640
00641 case DATA_8:
00642 break;
00643 }
00644 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00645 }
00646 break;
00647
00648 /*mark parity - WINDOWS ONLY*/
00649 case PAR_MARK:
00650 TTY_WARNING("Posix_QextSerialPort: Mark parity is not supported by POSIX.");
00651 break;
00652
00653 /*no parity*/
00654 case PAR_NONE:
00655 Posix_CommConfig.c_cflag&=(~PARENB);
00656 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00657 break;
00658
00659 /*even parity*/
00660 case PAR_EVEN:
00661 Posix_CommConfig.c_cflag&=(~PARODD);
00662 Posix_CommConfig.c_cflag|=PARENB;
00663 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00664 break;
00665
00666 /*odd parity*/
00667 case PAR_ODD:
00668 Posix_CommConfig.c_cflag|=(PARENB|PARODD);
00669 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00670 break;
00671 }
00672 }
00673 UNLOCK_MUTEX();
00674 }
|
|
|
Sets the number of stop bits used by the serial port. Possible values of stopBits are:
STOP_1 1 stop bit
STOP_1_5 1.5 stop bits
STOP_2 2 stop bits
Implements QextSerialBase. 00693 {
00694 LOCK_MUTEX();
00695 if (Settings.StopBits!=stopBits) {
00696 if ((Settings.DataBits==DATA_5 && stopBits==STOP_2) || stopBits==STOP_1_5) {}
00697 else {
00698 Settings.StopBits=stopBits;
00699 }
00700 }
00701 if (portOpen) {
00702 switch (stopBits) {
00703
00704 /*one stop bit*/
00705 case STOP_1:
00706 Settings.StopBits=stopBits;
00707 Posix_CommConfig.c_cflag&=(~CSTOPB);
00708 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00709 break;
00710
00711 /*1.5 stop bits*/
00712 case STOP_1_5:
00713 TTY_WARNING("Posix_QextSerialPort: 1.5 stop bit operation is not supported by POSIX.");
00714 break;
00715
00716 /*two stop bits*/
00717 case STOP_2:
00718 if (Settings.DataBits==DATA_5) {
00719 TTY_WARNING("Posix_QextSerialPort: 2 stop bits cannot be used with 5 data bits");
00720 }
00721 else {
00722 Settings.StopBits=stopBits;
00723 Posix_CommConfig.c_cflag|=CSTOPB;
00724 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00725 }
00726 break;
00727 }
00728 }
00729 UNLOCK_MUTEX();
00730 }
|
|
|
Sets the flow control used by the port. Possible values of flow are:
FLOW_OFF No flow control
FLOW_HARDWARE Hardware (RTS/CTS) flow control
FLOW_XONXOFF Software (XON/XOFF) flow control
Implements QextSerialBase. 00746 {
00747 LOCK_MUTEX();
00748 if (Settings.FlowControl!=flow) {
00749 Settings.FlowControl=flow;
00750 }
00751 if (portOpen) {
00752 switch(flow) {
00753
00754 /*no flow control*/
00755 case FLOW_OFF:
00756 Posix_CommConfig.c_cflag&=(~CRTSCTS);
00757 Posix_CommConfig.c_iflag&=(~(IXON|IXOFF|IXANY));
00758 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00759 break;
00760
00761 /*software (XON/XOFF) flow control*/
00762 case FLOW_XONXOFF:
00763 Posix_CommConfig.c_cflag&=(~CRTSCTS);
00764 Posix_CommConfig.c_iflag|=(IXON|IXOFF|IXANY);
00765 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00766 break;
00767
00768 case FLOW_HARDWARE:
00769 Posix_CommConfig.c_cflag|=CRTSCTS;
00770 Posix_CommConfig.c_iflag&=(~(IXON|IXOFF|IXANY));
00771 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00772 break;
00773 }
00774 }
00775 UNLOCK_MUTEX();
00776 }
|
|
||||||||||||
|
Sets the read and write timeouts for the port to sec seconds and millisec milliseconds. Note that this is a per-character timeout, i.e. the port will wait this long for each individual character, not for the whole read operation. This timeout also applies to the bytesWaiting() function.
Implements QextSerialBase. 00796 {
00797 LOCK_MUTEX();
00798 Settings.Timeout_Sec=sec;
00799 Settings.Timeout_Millisec=millisec;
00800 Posix_Copy_Timeout.tv_sec=sec;
00801 Posix_Copy_Timeout.tv_usec=millisec;
00802 if (portOpen) {
00803 tcgetattr(Posix_File->handle(), &Posix_CommConfig);
00804 Posix_CommConfig.c_cc[VTIME]=sec*10+millisec/100;
00805 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00806 }
00807 UNLOCK_MUTEX();
00808 }
|
|
|
Opens the serial port associated to this class. This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. Implements QextSerialBase. 00817 {
00818 LOCK_MUTEX();
00819 if (!portOpen) {
00820
00821 /*open the port*/
00822 Posix_File->setFileName(port);
00823 qDebug("Trying to open File");
00824 if (Posix_File->open(QIODevice::ReadWrite|QIODevice::Unbuffered)) {
00825 qDebug("Opened File succesfully");
00826 portOpen=true;
00827
00828 /*configure port settings*/
00829 tcgetattr(Posix_File->handle(), &Posix_CommConfig);
00830
00831 /*set up other port settings*/
00832 Posix_CommConfig.c_cflag|=CREAD|CLOCAL;
00833 Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG));
00834 Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY));
00835 Posix_CommConfig.c_oflag&=(~OPOST);
00836 Posix_CommConfig.c_cc[VMIN]=0;
00837 Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE;
00838 Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE;
00839 Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE;
00840 Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE;
00841 Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE;
00842 setBaudRate(Settings.BaudRate);
00843 setDataBits(Settings.DataBits);
00844 setParity(Settings.Parity);
00845 setStopBits(Settings.StopBits);
00846 setFlowControl(Settings.FlowControl);
00847 setTimeout(Settings.Timeout_Sec, Settings.Timeout_Millisec);
00848 tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig);
00849 } else {
00850 qDebug("Could not open File! Error code : %d", Posix_File->error());
00851 }
00852 }
00853 UNLOCK_MUTEX();
00854 return portOpen;
00855 }
|
|
|
Closes a serial port. This function has no effect if the serial port associated with the class is not currently open. Implements QextSerialBase. 00863 {
00864 LOCK_MUTEX();
00865 Posix_File->close();
00866 portOpen=false;
00867 UNLOCK_MUTEX();
00868 }
|
|
|
Flushes all pending I/O to the serial port. This function has no effect if the serial port associated with the class is not currently open. Implements QextSerialBase. 00876 {
00877 LOCK_MUTEX();
00878 if (portOpen) {
00879 Posix_File->flush();
00880 }
00881 UNLOCK_MUTEX();
00882 }
|
|
|
This function will return the number of bytes waiting in the receive queue of the serial port. It is included primarily to provide a complete QIODevice interface, and will not record errors in the lastErr member (because it is const). This function is also not thread-safe - in multithreading situations, use Posix_QextSerialPort::bytesWaiting() instead. Implements QextSerialBase. 00892 {
00893 int numBytes;
00894 if (ioctl(Posix_File->handle(), FIONREAD, &numBytes)<0) {
00895 numBytes=0;
00896 }
00897 return (qint64)numBytes;
00898 }
|
|
|
Returns the number of bytes waiting in the port's receive queue. This function will return 0 if the port is not currently open, or -1 on error. Error information can be retrieved by calling Posix_QextSerialPort::getLastError(). Implements QextSerialBase. 00907 {
00908 LOCK_MUTEX();
00909 if (portOpen) {
00910 int bytesQueued;
00911 fd_set fileSet;
00912 FD_ZERO(&fileSet);
00913 FD_SET(Posix_File->handle(), &fileSet);
00914
00915 /*on Linux systems the Posix_Timeout structure will be altered by the select() call.
00916 Make sure we use the right timeout values*/
00917 //memcpy(&Posix_Timeout, &Posix_Copy_Timeout, sizeof(struct timeval));
00918 Posix_Timeout = Posix_Copy_Timeout;
00919 int n=select(Posix_File->handle()+1, &fileSet, NULL, &fileSet, &Posix_Timeout);
00920 if (!n) {
00921 lastErr=E_PORT_TIMEOUT;
00922 UNLOCK_MUTEX();
00923 return -1;
00924 }
00925 if (n==-1 || ioctl(Posix_File->handle(), FIONREAD, &bytesQueued)==-1) {
00926 translateError(errno);
00927 UNLOCK_MUTEX();
00928 return -1;
00929 }
00930 lastErr=E_NO_ERROR;
00931 UNLOCK_MUTEX();
00932 return bytesQueued;
00933 }
00934 UNLOCK_MUTEX();
00935 return 0;
00936 }
|
|
|
Reads one character from the device and stores it in c. Returns true on success; otherwise returns false. This function has no effect if the serial port associated with the class is not currently open. Implements QextSerialBase. 00945 {
00946 LOCK_MUTEX();
00947 bool retVal=false;
00948 if (portOpen) {
00949 char readChar=0;
00950 retVal = Posix_File->getChar(&readChar);
00951 if(retVal == false) {
00952 lastErr=E_READ_FAILED;
00953 } else {
00954 *c = readChar;
00955 }
00956 }
00957 UNLOCK_MUTEX();
00958 return retVal;
00959 }
|
|
|
Writes the character c to the device. Returns true on success; otherwise returns false. This function has no effect if the serial port associated with the class is not currently open. Implements QextSerialBase. 00968 {
00969 LOCK_MUTEX();
00970 bool retVal=false;
00971 if (portOpen) {
00972 retVal=Posix_File->putChar(c);
00973 if (retVal == false) {
00974 lastErr=E_WRITE_FAILED;
00975 }
00976 }
00977 UNLOCK_MUTEX();
00978 flush();
00979 return retVal;
00980 }
|
|
|
This function is included to implement the full QIODevice interface, and currently has no purpose within this class. This function is meaningless on an unbuffered device and currently only prints a warning message to that effect. Implements QextSerialBase. 00989 {
00990 /*meaningless on unbuffered sequential device - return error and print a warning*/
00991 TTY_WARNING("Posix_QextSerialPort: ungetChar() called on an unbuffered sequential device - operation is meaningless");
00992 }
|
|
|
Translates a system-specific error code to a QextSerialPort error code. Used internally. Implements QextSerialBase. 00999 {
01000 switch (error) {
01001 case EBADF:
01002 case ENOTTY:
01003 lastErr=E_INVALID_FD;
01004 break;
01005
01006 case EINTR:
01007 lastErr=E_CAUGHT_NON_BLOCKED_SIGNAL;
01008 break;
01009
01010 case ENOMEM:
01011 lastErr=E_NO_MEMORY;
01012 break;
01013 }
01014 }
|
|
|
Sets DTR line to the requested state (high by default). This function will have no effect if the port associated with the class is not currently open. Implements QextSerialBase. 01022 {
01023 LOCK_MUTEX();
01024 if (portOpen) {
01025 int status;
01026 ioctl(Posix_File->handle(), TIOCMGET, &status);
01027 if (set) {
01028 status|=TIOCM_DTR;
01029 }
01030 else {
01031 status&=~TIOCM_DTR;
01032 }
01033 ioctl(Posix_File->handle(), TIOCMSET, &status);
01034 }
01035 UNLOCK_MUTEX();
01036 }
|
|
|
Sets RTS line to the requested state (high by default). This function will have no effect if the port associated with the class is not currently open. Implements QextSerialBase. 01044 {
01045 LOCK_MUTEX();
01046 if (portOpen) {
01047 int status;
01048 ioctl(Posix_File->handle(), TIOCMGET, &status);
01049 if (set) {
01050 status|=TIOCM_RTS;
01051 }
01052 else {
01053 status&=~TIOCM_RTS;
01054 }
01055 ioctl(Posix_File->handle(), TIOCMSET, &status);
01056 }
01057 UNLOCK_MUTEX();
01058 }
|
|
|
returns the line status as stored by the port function. This function will retrieve the states of the following lines: DCD, CTS, DSR, and RI. On POSIX systems, the following additional lines can be monitored: DTR, RTS, Secondary TXD, and Secondary RXD. The value returned is an unsigned long with specific bits indicating which lines are high. The following constants should be used to examine the states of individual lines:
Mask Line ------ ---- LS_CTS CTS LS_DSR DSR LS_DCD DCD LS_RI RI LS_RTS RTS (POSIX only) LS_DTR DTR (POSIX only) LS_ST Secondary TXD (POSIX only) LS_SR Secondary RXD (POSIX only) This function will return 0 if the port associated with the class is not currently open. Implements QextSerialBase. 01084 {
01085 unsigned long Status=0, Temp=0;
01086 LOCK_MUTEX();
01087 if (portOpen) {
01088 ioctl(Posix_File->handle(), TIOCMGET, &Temp);
01089 if (Temp&TIOCM_CTS) {
01090 Status|=LS_CTS;
01091 }
01092 if (Temp&TIOCM_DSR) {
01093 Status|=LS_DSR;
01094 }
01095 if (Temp&TIOCM_RI) {
01096 Status|=LS_RI;
01097 }
01098 if (Temp&TIOCM_CD) {
01099 Status|=LS_DCD;
01100 }
01101 if (Temp&TIOCM_DTR) {
01102 Status|=LS_DTR;
01103 }
01104 if (Temp&TIOCM_RTS) {
01105 Status|=LS_RTS;
01106 }
01107 if (Temp&TIOCM_ST) {
01108 Status|=LS_ST;
01109 }
01110 if (Temp&TIOCM_SR) {
01111 Status|=LS_SR;
01112 }
01113 }
01114 UNLOCK_MUTEX();
01115 return Status;
01116 }
|
|
||||||||||||
|
Reads a block of data from the serial port. This function will read at most maxSize bytes from the serial port and place them in the buffer pointed to by data. Return value is the number of bytes actually read, or -1 on error. This function will have no effect if the serial port associated with the class is not currently open. Implements QextSerialBase. 01126 {
01127 LOCK_MUTEX();
01128 int retVal=0;
01129 if (portOpen) {
01130 retVal=Posix_File->read(data, maxSize);
01131 if (retVal==-1) {
01132 lastErr=E_READ_FAILED;
01133 }
01134 }
01135 UNLOCK_MUTEX();
01136 return retVal;
01137 }
|
|
||||||||||||
|
Writes a block of data to the serial port. This function will write maxSize bytes from the buffer pointed to by data to the serial port. Return value is the number of bytes actually written, or -1 on error. This function will have no effect if the serial port associated with the class is not currently open. Implements QextSerialBase. 01147 {
01148 LOCK_MUTEX();
01149 int retVal=0;
01150 if (portOpen) {
01151 retVal=Posix_File->write(data, maxSize);
01152 if (retVal==-1) {
01153 lastErr=E_WRITE_FAILED;
01154 }
01155 }
01156 UNLOCK_MUTEX();
01157 flush();
01158 return retVal;
01159 }
|
|
|
Common constructor function for setting up default port settings. (115200 Baud, 8N1, Hardware flow control where supported, otherwise no flow control, and 500 ms timeout). 00083 {
00084 Settings.BaudRate=BAUD115200;
00085 Settings.DataBits=DATA_8;
00086 Settings.Parity=PAR_NONE;
00087 Settings.StopBits=STOP_1;
00088 Settings.FlowControl=FLOW_HARDWARE;
00089 Settings.Timeout_Sec=0;
00090 Settings.Timeout_Millisec=500;
00091
00092 #ifdef QT_THREAD_SUPPORT
00093 if (!mutex) {
00094 mutex=new QMutex( QMutex::Recursive );
00095 }
00096 refCount++;
00097 #endif
00098
00099 portOpen=false;
00100 }
|
|
|
Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0". 00107 {
00108 port = name;
00109 }
|
|
|
Returns the name set by setPortName(). 00116 {
00117 return port;
00118 }
|
|
|
Returns the baud rate of the serial port. For a list of possible return values see the definition of the enum BaudRateType.
|
|
|
Returns the number of data bits used by the port. For a list of possible values returned by this function, see the definition of the enum DataBitsType.
|
|
|
Returns the type of parity used by the port. For a list of possible values returned by this function, see the definition of the enum ParityType.
|
|
|
Returns the number of stop bits used by the port. For a list of possible return values, see the definition of the enum StopBitsType.
|
|
|
Returns the type of flow control used by the port. For a list of possible values returned by this function, see the definition of the enum FlowType. 00166 {
00167 return Settings.FlowControl;
00168 }
|
|
|
Returns true if the port associated with the class is currently open, or false if it is not. 00175 {
00176 return portOpen;
00177 }
|
|
|
This function will return true if the input buffer is empty (or on error), and false otherwise. Call QextSerialBase::lastError() for error information. 00185 {
00186 if (size()) {
00187 return true;
00188 }
00189 return false;
00190 }
|
|
||||||||||||
|
This function will read a line of buffered input from the port, stopping when either maxSize bytes have been read, the port has no more data available, or a newline is encountered. The value returned is the length of the string that was read. 00199 {
00200 qint64 numBytes = bytesAvailable();
00201 char* pData = data;
00202
00203 /*if nothing waiting, return 0 length*/
00204 if (numBytes<1) {
00205 return 0;
00206 }
00207
00208 /*read a byte at a time for MIN(bytesAvail, maxSize) iterations, or until a newline*/
00209 while (pData<(data+numBytes) && --maxSize) {
00210 readData(pData, 1);
00211 if (*pData++ == '\n') {
00212 break;
00213 }
00214 }
00215 *pData++='\0';
00216
00217 /*return size of data read*/
00218 return (pData-data);
00219 }
|
|
|
Returns the code for the last error encountered by the port, or E_NO_ERROR if the last port operation was successful. Possible error codes are:
Error Explanation
--------------------------- -------------------------------------------------------------
E_NO_ERROR No Error has occured
E_INVALID_FD Invalid file descriptor (port was not opened correctly)
E_NO_MEMORY Unable to allocate memory tables (POSIX)
E_CAUGHT_NON_BLOCKED_SIGNAL Caught a non-blocked signal (POSIX)
E_PORT_TIMEOUT Operation timed out (POSIX)
E_INVALID_DEVICE The file opened by the port is not a character device (POSIX)
E_BREAK_CONDITION The port detected a break condition
E_FRAMING_ERROR The port detected a framing error
(usually caused by incorrect baud rate settings)
E_IO_ERROR There was an I/O error while communicating with the port
E_BUFFER_OVERRUN Character buffer overrun
E_RECEIVE_OVERFLOW Receive buffer overflow
E_RECEIVE_PARITY_ERROR The port detected a parity error in the received data
E_TRANSMIT_OVERFLOW Transmit buffer overflow
E_READ_FAILED General read operation failure
E_WRITE_FAILED General write operation failure
00248 {
00249 return lastErr;
00250 }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.4.1