Async::Serial Class Reference

A class for using asyncronous serial communications. More...

#include <AsyncSerial.h>

List of all members.

Public Types

Public Member Functions

Public Attributes

Static Public Attributes


Detailed Description

A class for using asyncronous serial communications.

Author:
Tobias Blomberg
Date:
2004-08-02
Note:
The flow control code is untested. Report success/failure to me.

This class is used to communicate over an asynchronous serial link (e.g. RS-232 port). An example of how to use it is shown below.

#include <stdio.h>
#include <iostream>
#include <cstdlib>

#include <AsyncCppApplication.h>
#include <AsyncSerial.h>

using namespace std;
using namespace Async;

class MyClass : public SigC::Object
{
  public:
    MyClass(void)
    {
      serial = new Serial("/dev/ttyS0");
      serial->charactersReceived.connect(
          slot(*this, &MyClass::onCharactersReceived));

      if (!serial->open())
      {
        perror("Open serial port failed");
        exit(1);
      }
      serial->setParams(9600, Serial::PARITY_NONE, 8, 1, Serial::FLOW_NONE);
      
      serial->write("Hello, serial\n", 14);
    }
    
    ~MyClass(void)
    {
      serial->close();
      delete serial;
    }

  private:
    Serial *serial;
    
    void onCharactersReceived(char *buf, int count)
    {
      cout << "Read " << count << " characters from serial port: "
           << buf << endl;
    }
};

int main(int argc, char **argv)
{
  CppApplication app;
  MyClass my_class;
  app.exec();
}
Examples:

AsyncSerial_demo.cpp.

Definition at line 129 of file AsyncSerial.h.


Member Enumeration Documentation

A type that defines the possible choices for flow control.

Enumerator:
FLOW_NONE 

No flow control in use.

FLOW_HW 

Use hardware flow control.

FLOW_XONOFF 

Use software (XON/XOFF) flow control.

Definition at line 145 of file AsyncSerial.h.

A type that defines the possible choices for parity.

Enumerator:
PARITY_NONE 

Parity not used.

PARITY_EVEN 

Use even parity.

PARITY_ODD 

Use odd parity.

Definition at line 135 of file AsyncSerial.h.

A type that defines the read/write pins in the serial port.

Enumerator:
PIN_NONE 

No pin.

PIN_RTS 

Output: Request To Send.

PIN_DTR 

Output: Data Terminal Ready.

PIN_CTS 

Input: Clear To Send.

PIN_DSR 

Input: Data Set Ready.

PIN_DCD 

Input: Data Carrier Detect.

PIN_RI 

Input: Ring Indicate.

Definition at line 155 of file AsyncSerial.h.


Constructor & Destructor Documentation

Async::Serial::Serial ( const std::string &  serial_port  )  [explicit]

Constuctor.

Parameters:
serial_port The device name of the serial port to use

This is the constructor for the serial port class. It is possible to create multiple instances connected to the same serial port. For this to work, the given device name string must be exactly the same in all instances.

Async::Serial::~Serial ( void   ) 

Destructor.


Member Function Documentation

bool Async::Serial::close ( void   ) 

Close a previously opened serial port.

Returns:
Return true on success or else false on failue. On failure the global variable errno will be set to indicate the cause of the error.

Use this method to close a previously opened serial port. If the port is already closed, true is returned and nothing is done. If the port has been opened by multiple instances, it will not be closed until the last instance has closed it.

bool Async::Serial::getPin ( Pin  pin,
bool &  is_set 
)

Get the state of one of the input pins in the serial port.

Parameters:
pin The pin to get the state of. See Serial::Pin.
is_set The result will be returned in this variable.
Returns:
Return true on success or else false on failue. On failure the global variable errno will be set to indicate the cause of the error.

Use this function to read the state of one of the input pins in the serial port. For a list of available pins see Serial::Pin.

bool Async::Serial::open ( void   ) 

Open the serial port.

Returns:
Return true on success or else false on failue. On failure the global variable errno will be set to indicate the cause of the error.

Call this method to open the serial port. No special setup of the port is done during the open call. The old setup is saved and restored when the port is closed. To setup the port operating parameters, use the setParams method after calling open. If the open method is called when the port is already opened, it just returns true without doing anything.

bool Async::Serial::setCanonical ( bool  canonical  ) 

Set or clear canonical mode.

Returns:
Return true on success or else false on failue. On failure the global variable errno will be set to indicate the cause of the error.

Call this method to configure the serial port for canonical mode. In this mode, a couple of control characters are parsed by the kernel driver and interpreted in special ways. Most notably, only whole lines of text are sent up to the application. Don't use canonical mode when the serial stream is not readable text that is split into lines with a line feed character.

This function may be called both before or after the port has been opened and the setting is remembered after a close.

bool Async::Serial::setParams ( int  speed,
Parity  parity,
int  bits,
int  stop_bits,
Flow  flow 
)

Setup the serial port communications parameters.

Parameters:
speed The serial speed to use
parity The parity to use (see Serial::Parity)
bits The number of bits in each serial word
stop_bits The number of stop bits to use
flow The type of flow control to use (see Serial::Flow)
Returns:
Return true on success or else false on failure. On failure the global variable errno will be set to indicate the cause of the error.

This function is used to setup the serial communications parameters for the serial port. This must be done after calling the open function.

bool Async::Serial::setPin ( Pin  pin,
bool  set 
)

Set the state of one of the output pins in the serial port.

Parameters:
pin The pin to set. See Serial::Pin.
set If true the pin is set, if false the pin is cleared
Returns:
Return true on success or else false on failue. On failure the global variable errno will be set to indicate the cause of the error.

Use this function to control one of the output pins in the serial port. For a list of available pins see Serial::Pin.

bool Async::Serial::stopInput ( bool  stop  ) 

Stop/start input of data.

Parameters:
stop Stop input if true or start input if false
Returns:
Return true on success or else false on failue. On failure the global variable errno will be set to indicate the cause of the error.

Use this function to start/stop input of data when the flow control has been enabled.

int Async::Serial::write ( const char *  buf,
size_t  count 
) [inline]

Write data to the serial port.

Parameters:
buf A buffer containing the data to write
count The number of bytes to write
Returns:
The number of bytes written is returned on success. If an error occurs, -1 is returned and the global variable errno is set to indicate the cause of the error.

Definition at line 243 of file AsyncSerial.h.


Member Data Documentation

SigC::Signal2<void, char*, int> Async::Serial::charactersReceived

A signal that is emitted when there is data to read.

Parameters:
buf A buffer containing the data that has been read
count The number of bytes that was read
Note:
For maximum buffer size see Serial::READ_BUFSIZE

This signal is emitted whenever one or more characters has been received on the serial port. The buffer is always null-terminated but the null is not included in the count.

Definition at line 314 of file AsyncSerial.h.

const int Async::Serial::READ_BUFSIZE = 1024 [static]

The maximum number of characters that can be read at once.

Definition at line 169 of file AsyncSerial.h.


The documentation for this class was generated from the following file:
Generated by  doxygen 1.6.2-20100208