Async 1.8.0
Async::Serial Class Reference

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

#include <AsyncSerial.h>

Inheritance diagram for Async::Serial:

Public Types

enum  Parity { PARITY_NONE , PARITY_EVEN , PARITY_ODD }
 A type that defines the possible choices for parity. More...
 
enum  Flow { FLOW_NONE , FLOW_HW , FLOW_XONOFF }
 A type that defines the possible choices for flow control. More...
 
enum  Pin {
  PIN_NONE , PIN_RTS , PIN_DTR , PIN_CTS ,
  PIN_DSR , PIN_DCD , PIN_RI
}
 A type that defines the read/write pins in the serial port. More...
 

Public Member Functions

 Serial (const std::string &serial_port)
 Constuctor.
 
 ~Serial (void)
 Destructor.
 
bool setParams (int speed, Parity parity, int bits, int stop_bits, Flow flow)
 Setup the serial port communications parameters.
 
bool open (bool flush=false)
 Open the serial port.
 
bool close (void)
 Close a previously opened serial port.
 
int write (const char *buf, size_t count)
 Write data to the serial port.
 
bool setCanonical (bool canonical)
 Set or clear canonical mode.
 
bool stopInput (bool stop)
 Stop/start input of data.
 
bool setPin (Pin pin, bool set)
 Set the state of one of the output pins in the serial port.
 
bool getPin (Pin pin, bool &is_set)
 Get the state of one of the input pins in the serial port.
 

Public Attributes

sigc::signal< void, char *, int > charactersReceived
 A signal that is emitted when there is data to read.
 

Static Public Attributes

static const int READ_BUFSIZE = 1024
 The maximum number of characters that can be read at once.
 

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 <iostream>
#include <cstdlib>
#include <cstdio>
#include <AsyncSerial.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
serial = new Serial("/dev/ttyS0");
serial->charactersReceived.connect(
mem_fun(*this, &MyClass::onCharactersReceived));
if (!serial->open(true))
{
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)
{
MyClass my_class;
app.exec();
}
The core class for writing asyncronous cpp applications.
A class for using asynchronous serial communications.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
A class for using asyncronous serial communications.
Namespace for the asynchronous programming classes.
Examples
AsyncSerial_demo.cpp.

Definition at line 129 of file AsyncSerial.h.

Member Enumeration Documentation

◆ Flow

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.

◆ Parity

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.

◆ Pin

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

◆ Serial()

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

Constuctor.

Parameters
serial_portThe 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.

◆ ~Serial()

Async::Serial::~Serial ( void )

Destructor.

Member Function Documentation

◆ close()

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.

◆ getPin()

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

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

Parameters
pinThe pin to get the state of. See Serial::Pin.
is_setThe 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.

◆ open()

bool Async::Serial::open ( bool flush = false)

Open the serial port.

Parameters
flushFlush (discard) pending RX/TX data on open
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.

◆ setCanonical()

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.

◆ setParams()

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

Setup the serial port communications parameters.

Parameters
speedThe serial speed to use
parityThe parity to use (see Serial::Parity)
bitsThe number of bits in each serial word
stop_bitsThe number of stop bits to use
flowThe 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.

◆ setPin()

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

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

Parameters
pinThe pin to set. See Serial::Pin.
setIf 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.

◆ stopInput()

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

Stop/start input of data.

Parameters
stopStop 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.

◆ write()

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

Write data to the serial port.

Parameters
bufA buffer containing the data to write
countThe 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 244 of file AsyncSerial.h.

Member Data Documentation

◆ charactersReceived

sigc::signal<void, char*, int> Async::Serial::charactersReceived

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

Parameters
bufA buffer containing the data that has been read
countThe 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 315 of file AsyncSerial.h.

◆ READ_BUFSIZE

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: