Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

Win_QextSerialPort Class Reference

#include <win_qextserialport.h>

Inheritance diagram for Win_QextSerialPort:

QextSerialBase List of all members.

Public Member Functions

 Win_QextSerialPort ()
 Win_QextSerialPort (Win_QextSerialPort const &s)
 Win_QextSerialPort (const QString &name)
 Win_QextSerialPort (const PortSettings &settings)
 Win_QextSerialPort (const QString &name, const PortSettings &settings)
Win_QextSerialPort & operator= (const Win_QextSerialPort &s)
virtual ~Win_QextSerialPort ()
virtual bool open (OpenMode mode=0)
virtual void close ()
virtual void flush ()
virtual qint64 size () const
virtual bool getChar (char *c)
virtual bool putChar (char c)
virtual void ungetChar (char c)
virtual void setFlowControl (FlowType)
virtual void setParity (ParityType)
virtual void setDataBits (DataBitsType)
virtual void setStopBits (StopBitsType)
virtual void setBaudRate (BaudRateType)
virtual void setDtr (bool set=true)
virtual void setRts (bool set=true)
virtual ulong lineStatus (void)
virtual qint64 bytesAvailable ()
virtual void translateError (ulong)
virtual void setTimeout (ulong, ulong)
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

HANDLE Win_Handle
COMMCONFIG Win_CommConfig
COMMTIMEOUTS Win_CommTimeouts
QString port
bool portOpen
PortSettings Settings
ulong lastErr

Static Protected Attributes

static QMutex * mutex = NULL
static ulong refCount = 0

Detailed Description

Version:
1.0.0
Author:
Stefan Sander
A cross-platform serial port class. This class encapsulates the Windows portion of QextSerialPort. The user will be notified of errors and possible portability conflicts at run-time by default - this behavior can be turned off by defining _TTY_NOWARN_ (to turn off all warnings) or _TTY_NOWARN_PORT_ (to turn off portability warnings) in the project. Note that defining _TTY_NOWARN_ also defines _TTY_NOWARN_PORT_.

Note:
On Windows NT/2000/XP this class uses Win32 serial port functions by default. The user may select POSIX behavior under NT, 2000, or XP ONLY by defining _TTY_POSIX_ in the project. I can make no guarantees as to the quality of POSIX support under NT/2000 however.


Constructor & Destructor Documentation

Win_QextSerialPort::Win_QextSerialPort (   ) 
 

Default constructor. Note that the name of the device used by a Win_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 associates the object with the first port on the system, e.g. COM1 for Windows platforms. See the other constructor if you need a port other than the first.

00047                                       :QextSerialBase() {
00048     Win_Handle=INVALID_HANDLE_VALUE;
00049 }

Win_QextSerialPort::Win_QextSerialPort (  Win_QextSerialPort const &  s  ) 
 

Win_QextSerialPort::Win_QextSerialPort(const Win_QextSerialPort&) Copy constructor.

00054                                                                  :QextSerialBase(s.port) {
00055     Win_Handle=INVALID_HANDLE_VALUE;
00056     portOpen=s.portOpen;
00057     lastErr=s.lastErr;
00058     port = s.port;
00059     Settings.FlowControl=s.Settings.FlowControl;
00060     Settings.Parity=s.Settings.Parity;
00061     Settings.DataBits=s.Settings.DataBits;
00062     Settings.StopBits=s.Settings.StopBits;
00063     Settings.BaudRate=s.Settings.BaudRate;
00064     Win_Handle=s.Win_Handle;
00065     memcpy(&Win_CommConfig, &s.Win_CommConfig, sizeof(COMMCONFIG));
00066     memcpy(&Win_CommTimeouts, &s.Win_CommTimeouts, sizeof(COMMTIMEOUTS));
00067 }

Win_QextSerialPort::Win_QextSerialPort (  const QString &  name  ) 
 

Constructs a serial port attached to the port specified by devName. devName is the name of the device, which is windowsystem-specific, e.g."COM2" or "/dev/ttyS0".

00075                                                           :QextSerialBase(name) {
00076     Win_Handle=INVALID_HANDLE_VALUE;
00077 }

Win_QextSerialPort::Win_QextSerialPort (  const PortSettings &  settings  ) 
 

Constructs a port with default name and specified settings.

00083                                                                    {
00084     Win_Handle=INVALID_HANDLE_VALUE;
00085     setBaudRate(settings.BaudRate);
00086     setDataBits(settings.DataBits);
00087     setStopBits(settings.StopBits);
00088     setParity(settings.Parity);
00089     setFlowControl(settings.FlowControl);
00090     setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
00091 }

Win_QextSerialPort::Win_QextSerialPort (  const QString &  name,
const PortSettings &  settings
) 
 

Constructs a port with specified name and settings.

00097                                                                                          {
00098     Win_Handle=INVALID_HANDLE_VALUE;
00099     setPortName(name);
00100     setBaudRate(settings.BaudRate);
00101     setDataBits(settings.DataBits);
00102     setStopBits(settings.StopBits);
00103     setParity(settings.Parity);
00104     setFlowControl(settings.FlowControl);
00105     setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec);
00106 }

Win_QextSerialPort::~Win_QextSerialPort (   )  [virtual]
 

Standard destructor.

00112                                         {
00113     if (portOpen) {
00114         close();
00115     }
00116 }


Member Function Documentation

Win_QextSerialPort & Win_QextSerialPort::operator= (  const Win_QextSerialPort &  s  ) 
 

overrides the = operator

00122                                                                              {
00123     portOpen=s.isOpen();
00124     lastErr=s.lastErr;
00125     port = s.port;
00126     Settings.FlowControl=s.Settings.FlowControl;
00127     Settings.Parity=s.Settings.Parity;
00128     Settings.DataBits=s.Settings.DataBits;
00129     Settings.StopBits=s.Settings.StopBits;
00130     Settings.BaudRate=s.Settings.BaudRate;
00131     Win_Handle=s.Win_Handle;
00132     memcpy(&Win_CommConfig, &s.Win_CommConfig, sizeof(COMMCONFIG));
00133     memcpy(&Win_CommTimeouts, &s.Win_CommTimeouts, sizeof(COMMTIMEOUTS));
00134     return *this;
00135 }

bool Win_QextSerialPort::open (  OpenMode  mode = 0  )  [virtual]
 

Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see Win_QextSerialPort::open(const char*). 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.

00144                                       {
00145     unsigned long confSize;
00146     LOCK_MUTEX();
00147     if (!portOpen) {
00148 
00149         /*open the port*/
00150         Win_Handle=CreateFileA(port.toAscii(), GENERIC_READ|GENERIC_WRITE,
00151                               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
00152         if (Win_Handle!=INVALID_HANDLE_VALUE) {
00153             portOpen=true;
00154 
00155             /*configure port settings*/
00156             GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
00157             GetCommState(Win_Handle, &(Win_CommConfig.dcb));
00158 
00159             /*set up parameters*/
00160             Win_CommConfig.dcb.fBinary=TRUE;
00161             Win_CommConfig.dcb.fInX=FALSE;
00162             Win_CommConfig.dcb.fOutX=FALSE;
00163             Win_CommConfig.dcb.fAbortOnError=FALSE;
00164             Win_CommConfig.dcb.fNull=FALSE;
00165             setBaudRate(Settings.BaudRate);
00166             setDataBits(Settings.DataBits);
00167             setStopBits(Settings.StopBits);
00168             setParity(Settings.Parity);
00169             setFlowControl(Settings.FlowControl);
00170             setTimeout(Settings.Timeout_Sec, Settings.Timeout_Millisec);
00171             SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00172         }
00173     }
00174     UNLOCK_MUTEX();
00175     return portOpen;
00176 }

void Win_QextSerialPort::close (   )  [virtual]
 

Closes a serial port. This function has no effect if the serial port associated with the class is not currently open.

Implements QextSerialBase.

00183                                {
00184     LOCK_MUTEX();
00185     CloseHandle(Win_Handle);
00186     portOpen=false;
00187     UNLOCK_MUTEX();
00188 }

void Win_QextSerialPort::flush (   )  [virtual]
 

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.

00195                                {
00196     LOCK_MUTEX();
00197     if (portOpen) {
00198         FlushFileBuffers(Win_Handle);
00199     }
00200     UNLOCK_MUTEX();
00201 }

qint64 Win_QextSerialPort::size (   )  const [virtual]
 

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 Win_QextSerialPort::bytesAvailable() instead.

Implements QextSerialBase.

00210                                       {
00211     int availBytes;
00212     COMSTAT Win_ComStat;
00213     DWORD Win_ErrorMask=0;
00214     ClearCommError(Win_Handle, &Win_ErrorMask, &Win_ComStat);
00215     availBytes = Win_ComStat.cbInQue;
00216     return (qint64)availBytes;
00217 }

bool Win_QextSerialPort::getChar (  char *  c  )  [virtual]
 

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.

00337                                          {
00338     LOCK_MUTEX();
00339     bool retVal=false;
00340     if (portOpen) {
00341         char readChar=0;
00342         COMSTAT Win_ComStat;
00343         DWORD Win_BytesRead=0;
00344         DWORD Win_ErrorMask=0;
00345         ClearCommError(Win_Handle, &Win_ErrorMask, &Win_ComStat);
00346         if (Win_ComStat.cbInQue) {
00347             if (!ReadFile(Win_Handle, (void*)&readChar, 1, &Win_BytesRead, NULL)
00348                 || Win_BytesRead==0) {
00349                 lastErr=E_READ_FAILED;
00350             }
00351             else {
00352                 *c = readChar;
00353                 retVal=true;
00354             }
00355         }
00356     }
00357     UNLOCK_MUTEX();
00358     return retVal;
00359 }

bool Win_QextSerialPort::putChar (  char  c  )  [virtual]
 

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.

00367                                        {
00368     LOCK_MUTEX();
00369     bool retVal=false;
00370     if (portOpen) {
00371         DWORD Win_BytesWritten;
00372         if (!WriteFile(Win_Handle, (void*)&c, 1, &Win_BytesWritten, NULL)) {
00373             lastErr=E_WRITE_FAILED;
00374         }
00375         else {
00376             retVal=true;
00377         }
00378     }
00379     UNLOCK_MUTEX();
00380     flush();
00381     return retVal;
00382 }

void Win_QextSerialPort::ungetChar (  char  c  )  [virtual]
 

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.

00390                                          {
00391 
00392     /*meaningless on unbuffered sequential device - return error and print a warning*/
00393     TTY_WARNING("Win_QextSerialPort: ungetChar() called on an unbuffered sequential device - operation is meaningless");
00394 }

void Win_QextSerialPort::setFlowControl (  FlowType  flow  )  [virtual]
 

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.

00405                                                      {
00406     LOCK_MUTEX();
00407     if (Settings.FlowControl!=flow) {
00408         Settings.FlowControl=flow;
00409     }
00410     if (portOpen) {
00411         switch(flow) {
00412 
00413             /*no flow control*/
00414             case FLOW_OFF:
00415                 Win_CommConfig.dcb.fOutxCtsFlow=FALSE;
00416                 Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;
00417                 Win_CommConfig.dcb.fInX=FALSE;
00418                 Win_CommConfig.dcb.fOutX=FALSE;
00419                 SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00420                 break;
00421 
00422             /*software (XON/XOFF) flow control*/
00423             case FLOW_XONXOFF:
00424                 Win_CommConfig.dcb.fOutxCtsFlow=FALSE;
00425                 Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;
00426                 Win_CommConfig.dcb.fInX=TRUE;
00427                 Win_CommConfig.dcb.fOutX=TRUE;
00428                 SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00429                 break;
00430 
00431             case FLOW_HARDWARE:
00432                 Win_CommConfig.dcb.fOutxCtsFlow=TRUE;
00433                 Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_HANDSHAKE;
00434                 Win_CommConfig.dcb.fInX=FALSE;
00435                 Win_CommConfig.dcb.fOutX=FALSE;
00436                 SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00437                 break;
00438         }
00439     }
00440     UNLOCK_MUTEX();
00441 }

void Win_QextSerialPort::setParity (  ParityType  parity  )  [virtual]
 

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.

00454                                                     {
00455     LOCK_MUTEX();
00456     if (Settings.Parity!=parity) {
00457         Settings.Parity=parity;
00458     }
00459     if (portOpen) {
00460         Win_CommConfig.dcb.Parity=(unsigned char)parity;
00461         switch (parity) {
00462 
00463             /*space parity*/
00464             case PAR_SPACE:
00465                 if (Settings.DataBits==DATA_8) {
00466                     TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems.");
00467                 }
00468                 Win_CommConfig.dcb.fParity=TRUE;
00469                 break;
00470 
00471             /*mark parity - WINDOWS ONLY*/
00472             case PAR_MARK:
00473                 TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning:  Mark parity is not supported by POSIX systems");
00474                 Win_CommConfig.dcb.fParity=TRUE;
00475                 break;
00476 
00477             /*no parity*/
00478             case PAR_NONE:
00479                 Win_CommConfig.dcb.fParity=FALSE;
00480                 break;
00481 
00482             /*even parity*/
00483             case PAR_EVEN:
00484                 Win_CommConfig.dcb.fParity=TRUE;
00485                 break;
00486 
00487             /*odd parity*/
00488             case PAR_ODD:
00489                 Win_CommConfig.dcb.fParity=TRUE;
00490                 break;
00491         }
00492         SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00493     }
00494     UNLOCK_MUTEX();
00495 }

void Win_QextSerialPort::setDataBits (  DataBitsType  dataBits  )  [virtual]
 

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

Note:
This function is subject to the following restrictions:
5 data bits cannot be used with 2 stop bits.
1.5 stop bits can only be used with 5 data bits.
8 data bits cannot be used with space parity on POSIX systems.

Implements QextSerialBase.

00517                                                           {
00518     LOCK_MUTEX();
00519     if (Settings.DataBits!=dataBits) {
00520         if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) ||
00521             (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5)) {
00522         }
00523         else {
00524             Settings.DataBits=dataBits;
00525         }
00526     }
00527     if (portOpen) {
00528         switch(dataBits) {
00529 
00530             /*5 data bits*/
00531             case DATA_5:
00532                 if (Settings.StopBits==STOP_2) {
00533                     TTY_WARNING("Win_QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");
00534                 }
00535                 else {
00536                     Win_CommConfig.dcb.ByteSize=5;
00537                     SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00538                 }
00539                 break;
00540 
00541             /*6 data bits*/
00542             case DATA_6:
00543                 if (Settings.StopBits==STOP_1_5) {
00544                     TTY_WARNING("Win_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");
00545                 }
00546                 else {
00547                     Win_CommConfig.dcb.ByteSize=6;
00548                     SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00549                 }
00550                 break;
00551 
00552             /*7 data bits*/
00553             case DATA_7:
00554                 if (Settings.StopBits==STOP_1_5) {
00555                     TTY_WARNING("Win_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");
00556                 }
00557                 else {
00558                     Win_CommConfig.dcb.ByteSize=7;
00559                     SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00560                 }
00561                 break;
00562 
00563             /*8 data bits*/
00564             case DATA_8:
00565                 if (Settings.StopBits==STOP_1_5) {
00566                     TTY_WARNING("Win_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");
00567                 }
00568                 else {
00569                     Win_CommConfig.dcb.ByteSize=8;
00570                     SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00571                 }
00572                 break;
00573         }
00574     }
00575     UNLOCK_MUTEX();
00576 }

void Win_QextSerialPort::setStopBits (  StopBitsType  stopBits  )  [virtual]
 

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

Note:
This function is subject to the following restrictions:
2 stop bits cannot be used with 5 data bits.
1.5 stop bits cannot be used with 6 or more data bits.
POSIX does not support 1.5 stop bits.

Implements QextSerialBase.

00596                                                           {
00597     LOCK_MUTEX();
00598     if (Settings.StopBits!=stopBits) {
00599         if ((Settings.DataBits==DATA_5 && stopBits==STOP_2) ||
00600             (stopBits==STOP_1_5 && Settings.DataBits!=DATA_5)) {
00601         }
00602         else {
00603             Settings.StopBits=stopBits;
00604         }
00605     }
00606     if (portOpen) {
00607         switch (stopBits) {
00608 
00609             /*one stop bit*/
00610             case STOP_1:
00611                 Win_CommConfig.dcb.StopBits=ONESTOPBIT;
00612                 SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00613                 break;
00614 
00615             /*1.5 stop bits*/
00616             case STOP_1_5:
00617                 TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: 1.5 stop bit operation is not supported by POSIX.");
00618                 if (Settings.DataBits!=DATA_5) {
00619                     TTY_WARNING("Win_QextSerialPort: 1.5 stop bits can only be used with 5 data bits");
00620                 }
00621                 else {
00622                     Win_CommConfig.dcb.StopBits=ONE5STOPBITS;
00623                     SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00624                 }
00625                 break;
00626 
00627             /*two stop bits*/
00628             case STOP_2:
00629                 if (Settings.DataBits==DATA_5) {
00630                     TTY_WARNING("Win_QextSerialPort: 2 stop bits cannot be used with 5 data bits");
00631                 }
00632                 else {
00633                     Win_CommConfig.dcb.StopBits=TWOSTOPBITS;
00634                     SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00635                 }
00636                 break;
00637         }
00638     }
00639     UNLOCK_MUTEX();
00640 }

void Win_QextSerialPort::setBaudRate (  BaudRateType  baudRate  )  [virtual]
 

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.

00676                                                           {
00677     LOCK_MUTEX();
00678     if (Settings.BaudRate!=baudRate) {
00679         switch (baudRate) {
00680             case BAUD50:
00681             case BAUD75:
00682             case BAUD134:
00683             case BAUD150:
00684             case BAUD200:
00685                 Settings.BaudRate=BAUD110;
00686                 break;
00687 
00688             case BAUD1800:
00689                 Settings.BaudRate=BAUD1200;
00690                 break;
00691 
00692             case BAUD76800:
00693                 Settings.BaudRate=BAUD57600;
00694                 break;
00695 
00696             default:
00697                 Settings.BaudRate=baudRate;
00698                 break;
00699         }
00700     }
00701     if (portOpen) {
00702         switch (baudRate) {
00703 
00704             /*50 baud*/
00705             case BAUD50:
00706                 TTY_WARNING("Win_QextSerialPort: Windows does not support 50 baud operation.  Switching to 110 baud.");
00707                 Win_CommConfig.dcb.BaudRate=CBR_110;
00708                 break;
00709 
00710             /*75 baud*/
00711             case BAUD75:
00712                 TTY_WARNING("Win_QextSerialPort: Windows does not support 75 baud operation.  Switching to 110 baud.");
00713                 Win_CommConfig.dcb.BaudRate=CBR_110;
00714                 break;
00715 
00716             /*110 baud*/
00717             case BAUD110:
00718                 Win_CommConfig.dcb.BaudRate=CBR_110;
00719                 break;
00720 
00721             /*134.5 baud*/
00722             case BAUD134:
00723                 TTY_WARNING("Win_QextSerialPort: Windows does not support 134.5 baud operation.  Switching to 110 baud.");
00724                 Win_CommConfig.dcb.BaudRate=CBR_110;
00725                 break;
00726 
00727             /*150 baud*/
00728             case BAUD150:
00729                 TTY_WARNING("Win_QextSerialPort: Windows does not support 150 baud operation.  Switching to 110 baud.");
00730                 Win_CommConfig.dcb.BaudRate=CBR_110;
00731                 break;
00732 
00733             /*200 baud*/
00734             case BAUD200:
00735                 TTY_WARNING("Win_QextSerialPort: Windows does not support 200 baud operation.  Switching to 110 baud.");
00736                 Win_CommConfig.dcb.BaudRate=CBR_110;
00737                 break;
00738 
00739             /*300 baud*/
00740             case BAUD300:
00741                 Win_CommConfig.dcb.BaudRate=CBR_300;
00742                 break;
00743 
00744             /*600 baud*/
00745             case BAUD600:
00746                 Win_CommConfig.dcb.BaudRate=CBR_600;
00747                 break;
00748 
00749             /*1200 baud*/
00750             case BAUD1200:
00751                 Win_CommConfig.dcb.BaudRate=CBR_1200;
00752                 break;
00753 
00754             /*1800 baud*/
00755             case BAUD1800:
00756                 TTY_WARNING("Win_QextSerialPort: Windows does not support 1800 baud operation.  Switching to 1200 baud.");
00757                 Win_CommConfig.dcb.BaudRate=CBR_1200;
00758                 break;
00759 
00760             /*2400 baud*/
00761             case BAUD2400:
00762                 Win_CommConfig.dcb.BaudRate=CBR_2400;
00763                 break;
00764 
00765             /*4800 baud*/
00766             case BAUD4800:
00767                 Win_CommConfig.dcb.BaudRate=CBR_4800;
00768                 break;
00769 
00770             /*9600 baud*/
00771             case BAUD9600:
00772                 Win_CommConfig.dcb.BaudRate=CBR_9600;
00773                 break;
00774 
00775             /*14400 baud*/
00776             case BAUD14400:
00777                 TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 14400 baud operation.");
00778                 Win_CommConfig.dcb.BaudRate=CBR_14400;
00779                 break;
00780 
00781             /*19200 baud*/
00782             case BAUD19200:
00783                 Win_CommConfig.dcb.BaudRate=CBR_19200;
00784                 break;
00785 
00786             /*38400 baud*/
00787             case BAUD38400:
00788                 Win_CommConfig.dcb.BaudRate=CBR_38400;
00789                 break;
00790 
00791             /*56000 baud*/
00792             case BAUD56000:
00793                 TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 56000 baud operation.");
00794                 Win_CommConfig.dcb.BaudRate=CBR_56000;
00795                 break;
00796 
00797             /*57600 baud*/
00798             case BAUD57600:
00799                 Win_CommConfig.dcb.BaudRate=CBR_57600;
00800                 break;
00801 
00802             /*76800 baud*/
00803             case BAUD76800:
00804                 TTY_WARNING("Win_QextSerialPort: Windows does not support 76800 baud operation.  Switching to 57600 baud.");
00805                 Win_CommConfig.dcb.BaudRate=CBR_57600;
00806                 break;
00807 
00808             /*115200 baud*/
00809             case BAUD115200:
00810                 Win_CommConfig.dcb.BaudRate=CBR_115200;
00811                 break;
00812 
00813             /*128000 baud*/
00814             case BAUD128000:
00815                 TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 128000 baud operation.");
00816                 Win_CommConfig.dcb.BaudRate=CBR_128000;
00817                 break;
00818 
00819             /*256000 baud*/
00820             case BAUD256000:
00821                 TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: POSIX does not support 256000 baud operation.");
00822                 Win_CommConfig.dcb.BaudRate=CBR_256000;
00823                 break;
00824         }
00825         SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
00826     }
00827     UNLOCK_MUTEX();
00828 }

void Win_QextSerialPort::setDtr (  bool  set = true  )  [virtual]
 

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.

00835                                         {
00836     LOCK_MUTEX();
00837     if (portOpen) {
00838         if (set) {
00839             EscapeCommFunction(Win_Handle, SETDTR);
00840         }
00841         else {
00842             EscapeCommFunction(Win_Handle, CLRDTR);
00843         }
00844     }
00845     UNLOCK_MUTEX();
00846 }

void Win_QextSerialPort::setRts (  bool  set = true  )  [virtual]
 

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.

00853                                         {
00854     LOCK_MUTEX();
00855     if (portOpen) {
00856         if (set) {
00857             EscapeCommFunction(Win_Handle, SETRTS);
00858         }
00859         else {
00860             EscapeCommFunction(Win_Handle, CLRRTS);
00861         }
00862     }
00863     UNLOCK_MUTEX();
00864 }

ulong Win_QextSerialPort::lineStatus (  void   )  [virtual]
 

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

This function will return 0 if the port associated with the class is not currently open.

Implements QextSerialBase.

00885                                          {
00886     unsigned long Status=0, Temp=0;
00887     LOCK_MUTEX();
00888     if (portOpen) {
00889         GetCommModemStatus(Win_Handle, &Temp);
00890         if (Temp&MS_CTS_ON) {
00891             Status|=LS_CTS;
00892         }
00893         if (Temp&MS_DSR_ON) {
00894             Status|=LS_DSR;
00895         }
00896         if (Temp&MS_RING_ON) {
00897             Status|=LS_RI;
00898         }
00899         if (Temp&MS_RLSD_ON) {
00900             Status|=LS_DCD;
00901         }
00902     }
00903     UNLOCK_MUTEX();
00904     return Status;
00905 }

qint64 Win_QextSerialPort::bytesAvailable (   )  [virtual]
 

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 Win_QextSerialPort::getLastError().

Implements QextSerialBase.

00225                                           {
00226     LOCK_MUTEX();
00227     if (portOpen) {
00228         DWORD Errors;
00229         COMSTAT Status;
00230         bool success=ClearCommError(Win_Handle, &Errors, &Status);
00231         translateError(Errors);
00232         if (success) {
00233             lastErr=E_NO_ERROR;
00234             UNLOCK_MUTEX();
00235             return Status.cbInQue;
00236         }
00237         UNLOCK_MUTEX();
00238         return (unsigned int)-1;
00239     }
00240     UNLOCK_MUTEX();
00241     return 0;
00242 }

void Win_QextSerialPort::translateError (  ulong  error  )  [virtual]
 

Translates a system-specific error code to a QextSerialPort error code. Used internally.

Implements QextSerialBase.

00248                                                    {
00249     if (error&CE_BREAK) {
00250         lastErr=E_BREAK_CONDITION;
00251     }
00252     else if (error&CE_FRAME) {
00253         lastErr=E_FRAMING_ERROR;
00254     }
00255     else if (error&CE_IOE) {
00256         lastErr=E_IO_ERROR;
00257     }
00258     else if (error&CE_MODE) {
00259         lastErr=E_INVALID_FD;
00260     }
00261     else if (error&CE_OVERRUN) {
00262         lastErr=E_BUFFER_OVERRUN;
00263     }
00264     else if (error&CE_RXPARITY) {
00265         lastErr=E_RECEIVE_PARITY_ERROR;
00266     }
00267     else if (error&CE_RXOVER) {
00268         lastErr=E_RECEIVE_OVERFLOW;
00269     }
00270     else if (error&CE_TXFULL) {
00271         lastErr=E_TRANSMIT_OVERFLOW;
00272     }
00273 }

void Win_QextSerialPort::setTimeout (  ulong  sec,
ulong  millisec
)  [virtual]
 

Sets the read and write timeouts for the port to sec seconds and millisec milliseconds.

Implements QextSerialBase.

00911                                                              {
00912     LOCK_MUTEX();
00913     Settings.Timeout_Sec=sec;
00914     Settings.Timeout_Millisec=millisec;
00915     if(portOpen) {
00916         Win_CommTimeouts.ReadIntervalTimeout = sec*1000+millisec;
00917         Win_CommTimeouts.ReadTotalTimeoutMultiplier = sec*1000+millisec;
00918         Win_CommTimeouts.ReadTotalTimeoutConstant = 0;
00919         Win_CommTimeouts.WriteTotalTimeoutMultiplier = sec*1000+millisec;
00920         Win_CommTimeouts.WriteTotalTimeoutConstant = 0;
00921         SetCommTimeouts(Win_Handle, &Win_CommTimeouts);
00922     }
00923     UNLOCK_MUTEX();
00924 }

qint64 Win_QextSerialPort::readData (  char *  data,
qint64  maxSize
)  [virtual]
 

Reads a block of data from the serial port. This function will read at most maxlen 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.

00283 {
00284     LOCK_MUTEX();
00285     int retVal=0;
00286     if (portOpen) {
00287         COMSTAT Win_ComStat;
00288         DWORD Win_BytesRead=0;
00289         DWORD Win_ErrorMask=0;
00290         ClearCommError(Win_Handle, &Win_ErrorMask, &Win_ComStat);
00291         if (Win_ComStat.cbInQue &&
00292             (!ReadFile(Win_Handle, (void*)data, (DWORD)maxSize, &Win_BytesRead, NULL)
00293             || Win_BytesRead==0)) {
00294             lastErr=E_READ_FAILED;
00295             retVal=-1;
00296         }
00297         else {
00298             retVal=((int)Win_BytesRead);
00299         }
00300     }
00301     UNLOCK_MUTEX();
00302     return retVal;
00303 }

qint64 Win_QextSerialPort::writeData (  const char *  data,
qint64  maxSize
)  [virtual]
 

Writes a block of data to the serial port. This function will write len 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.

00313 {
00314     LOCK_MUTEX();
00315     int retVal=0;
00316     if (portOpen) {
00317         DWORD Win_BytesWritten;
00318         if (!WriteFile(Win_Handle, (void*)data, (DWORD)maxSize, &Win_BytesWritten, NULL)) {
00319             lastErr=E_WRITE_FAILED;
00320             retVal=-1;
00321         }
00322         else {
00323             retVal=((int)Win_BytesWritten);
00324         }
00325     }
00326     UNLOCK_MUTEX();
00327     flush();
00328     return retVal;
00329 }

void QextSerialBase::construct (   )  [virtual, inherited]
 

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 }

void QextSerialBase::setPortName (  const QString &  name  )  [virtual, inherited]
 

Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0".

00107 {
00108     port = name;
00109 }

QString QextSerialBase::portName (   )  const [virtual, inherited]
 

Returns the name set by setPortName().

00116 {
00117     return port;
00118 }

BaudRateType QextSerialBase::baudRate (  void   )  const [virtual, inherited]
 

Returns the baud rate of the serial port. For a list of possible return values see the definition of the enum BaudRateType.

00126 {
00127     return Settings.BaudRate;
00128 }

DataBitsType QextSerialBase::dataBits (   )  const [virtual, inherited]
 

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.

00136 {
00137     return Settings.DataBits;
00138 }

ParityType QextSerialBase::parity (   )  const [virtual, inherited]
 

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.

00146 {
00147     return Settings.Parity;
00148 }

StopBitsType QextSerialBase::stopBits (   )  const [virtual, inherited]
 

Returns the number of stop bits used by the port. For a list of possible return values, see the definition of the enum StopBitsType.

00156 {
00157     return Settings.StopBits;
00158 }

FlowType QextSerialBase::flowControl (   )  const [virtual, inherited]
 

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 }

bool QextSerialBase::isOpen (   )  const [virtual, inherited]
 

Returns true if the port associated with the class is currently open, or false if it is not.

00175 {
00176     return portOpen;
00177 }

bool QextSerialBase::atEnd (   )  const [virtual, inherited]
 

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 }

qint64 QextSerialBase::readLine (  char *  data,
qint64  maxSize
)  [virtual, inherited]
 

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 }

ulong QextSerialBase::lastError (   )  const [virtual, inherited]
 

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 }


Member Data Documentation

HANDLE Win_QextSerialPort::Win_Handle [protected]
 

COMMCONFIG Win_QextSerialPort::Win_CommConfig [protected]
 

COMMTIMEOUTS Win_QextSerialPort::Win_CommTimeouts [protected]
 

QString QextSerialBase::port [protected, inherited]
 

bool QextSerialBase::portOpen [protected, inherited]
 

PortSettings QextSerialBase::Settings [protected, inherited]
 

ulong QextSerialBase::lastErr [protected, inherited]
 

QMutex * QextSerialBase::mutex = NULL [static, protected, inherited]
 

unsigned long QextSerialBase::refCount = 0 [static, protected, inherited]
 


The documentation for this class was generated from the following files:
Generated on Sun Oct 16 22:07:21 2005 for QextSerialPort by  doxygen 1.4.1