Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Serial Class Reference

#include <Devices/Serial.H>

Inheritance diagram for Serial:

Inheritance graph
[legend]
Collaboration diagram for Serial:

Collaboration graph
[legend]
List of all members.

Detailed Description

Interface to a serial port.

The port will be open at start() time; see ModelComponent.H for details. Because typically one may use several serial ports with different configs, this ModelComponent does not export any command-line option (there would be too many otherwise if many ports are used). Typically, thus, the port can be configured either via config files or using the various access functions provided in this class.

Definition at line 86 of file Serial.H.

Public Member Functions

 Serial (OptionManager &mgr, const std::string &descrName="Serial Port", const std::string &tagName="SerialPort")
 Constructor.
 ~Serial (void)
 destructor
void closePort ()
 Close the serial port device file.
void enablePort (std::string DeviceName)
 Enable the serial port.
void configure (const char *dev="", const int speed=9600, const char *format="8N1", const bool flowSoft=false, const bool flowHard=false, const int=0)
 Configure the port before it is started.
void configureSearch (const std::string DeviceDescription, const int speed=115200, const std::string SearchPrefix="ttyUSB", const char *format="8N1", const bool flowSoft=false, const bool flowHard=false, const int=0)
 Configure the serial port to search for a device.
serialError setSpeed (const int speed)
 Set serial port speed for both input and output.
serialError setFlowControl (const bool useHard, const bool useSoft)
 Set the serial port flow control on Input as well as Ouput.
serialError setCharBits (const int bits)
 Set character size.
serialError setParity (const bool useParity, const bool oddParity)
 Set parity mode.
serialError setStopBits (const int bits)
 Set number of stop bits.
serialError setBlocking (const bool blocking)
 Set the access to blocking or not.
void toggleDTR (const time_t millisec)
 Set the DTR mode off momentarily.
void sendBreak (void)
 transmit continuous stream of zero-valued bits for specific duration.
int read (void *buffer, const int nbytes)
 attempt to read up to nbytes from serial port into the buffer
std::vector< unsigned char > readFrame (const unsigned char frameStart, const unsigned char frameEnd=255, const int frameLength=-1, const double timeout=-1)
 Attempt to read a serial frame.
char read (void)
 attempts to get the next character from the serial port
int write (const void *buffer, const int nbytes)
 write bytes to the port
int write (const unsigned char character)
 write a single byte to the port - a simple wrapper around write(const void* buffer, const int nbytes)
void perror (void)
 print a verbal message indicating the last error.
void flush (void)
std::string getDeviceDescriptor ()
std::string getDevName ()

Protected Member Functions

void start2 ()
 open the port and get started
void stop2 ()
 close the port and get stopped

Protected Attributes

OModelParam< std::stringitsCmdDevName
 device name passed from cmd line
NModelParam< std::stringitsDeviceDescription
 device description to seach for
NModelParam< std::stringitsSearchPrefix
 device name prefix (e.g. ttyUSB)
NModelParam< int > itsBaud
 Baudrate.
NModelParam< int > itsCharBits
 number of useful bits per char
NModelParam< int > itsStopBits
 number of stop bits
NModelParam< bool > itsUseParity
 use parity if true
NModelParam< bool > itsParityOdd
 parity is odd if true, otherwise even
NModelParam< bool > itsFlowHard
 use hardware flow control if true
NModelParam< bool > itsFlowSoft
 use software flow control if true
NModelParam< int > itsRdTout
 read timeout in 1/10000s
NModelParam< bool > itsBlocking
 use blocking mode


Constructor & Destructor Documentation

Serial::Serial OptionManager mgr,
const std::string descrName = "Serial Port",
const std::string tagName = "SerialPort"
 

Constructor.

Definition at line 58 of file Serial.C.

Serial::~Serial void   ) 
 

destructor

Definition at line 770 of file Serial.C.


Member Function Documentation

void Serial::closePort  ) 
 

Close the serial port device file.

Definition at line 177 of file Serial.C.

Referenced by enablePort(), and stop2().

void Serial::configure const char *  dev = "",
const int  speed = 9600,
const char *  format = "8N1",
const bool  flowSoft = false,
const bool  flowHard = false,
const   int = 0
 

Configure the port before it is started.

This will update our internal ModelParam values to the specified ones; thus this should be called before start(). In contrast, the setXXX functions below are for reconfiguration at runtime, once the port is already open and running. This function is provided as a shortcut to set a bunch of ModelParam values in one simple call.

Parameters:
dev device name (e.g., "/dev/ttyS0")
speed speed in bauds. See setSpeed() for valid values
format a 3-char string for charbits, parity, and stop bits; for example "8N1"; parity should be "N", "E" or "O".
flowSoft use software flow control if true
flowHard use hardware flow control if true
tout read timeout in 1/10000s or 0 for no timeout

Definition at line 197 of file Serial.C.

References itsBaud, itsCharBits, itsFlowHard, itsFlowSoft, itsParityOdd, itsRdTout, itsStopBits, itsUseParity, LFATAL, and NModelParam< T >::setVal().

Referenced by configureSearch().

void Serial::configureSearch const std::string  DeviceDescription,
const int  speed = 115200,
const std::string  SearchPrefix = "ttyUSB",
const char *  format = "8N1",
const bool  flowSoft = false,
const bool  flowHard = false,
const   int = 0
 

Configure the serial port to search for a device.

Definition at line 186 of file Serial.C.

References configure(), itsDeviceDescription, itsSearchPrefix, and NModelParam< T >::setVal().

void Serial::enablePort std::string  DeviceName  ) 
 

Enable the serial port.

enablePort will do all of the work of actually setting the config properties that have been requested, as well as opening the device file, etc...

Definition at line 142 of file Serial.C.

References closePort(), NModelParam< T >::getVal(), itsBaud, itsBlocking, itsCharBits, itsFlowHard, itsFlowSoft, itsParityOdd, itsUseParity, perror(), setBlocking(), setCharBits(), setFlowControl(), setParity(), and setSpeed().

Referenced by start2().

void Serial::perror void   ) 
 

print a verbal message indicating the last error.

Definition at line 712 of file Serial.C.

References LERROR.

Referenced by enablePort().

char Serial::read void   ) 
 

attempts to get the next character from the serial port

for comptability with dirk's code

Definition at line 571 of file Serial.C.

Referenced by read(), and readFrame().

int Serial::read void *  buffer,
const int  nbytes
 

attempt to read up to nbytes from serial port into the buffer

Parameters:
buffer holds bytes after read number of bytes to attempt to read
Returns:
0 on a timeout, -1 on error, number of bytes actually read on a success

Definition at line 556 of file Serial.C.

References n, and read().

std::vector< unsigned char > Serial::readFrame const unsigned char  frameStart,
const unsigned char  frameEnd = 255,
const int  frameLength = -1,
const double  timeout = -1
 

Attempt to read a serial frame.

The frame returned by the device should have the following format: [start byte] [optional number of data bytes] [data...] ... [...data] [end byte]

Parameters:
frameStart holds the byte signifying the start of the frame
frameEnd holds the byte signifying the end of the frame
frameLength optionally specifies the number of bytes in the frame. If this is -1, then the second byte of the frame is assumed to contain the total number of bytes in the frame.
timeout the number of seconds to wait before giving up (default of -1 is no timeout)
Returns:
the data contained in the frame

Definition at line 580 of file Serial.C.

References data, Timer::getSecs(), read(), Timer::reset(), and timer.

Referenced by start2().

void Serial::sendBreak void   ) 
 

transmit continuous stream of zero-valued bits for specific duration.

Definition at line 462 of file Serial.C.

serialError Serial::setBlocking const bool  blocking  ) 
 

Set the access to blocking or not.

If blocking, a write will block until all data has actually been sent through; otherwise it will return immediately while the data is being transmitted.

Definition at line 426 of file Serial.C.

References itsBlocking, PLERROR, and NModelParam< T >::setVal().

Referenced by enablePort().

serialError Serial::setCharBits const int  bits  ) 
 

Set character size.

This function is to change the char size while already started.

Returns:
0 on success.
Parameters:
bits character size to use; valid values are 5, 6, 7, 8.

Definition at line 338 of file Serial.C.

References itsCharBits, and NModelParam< T >::setVal().

Referenced by enablePort().

serialError Serial::setFlowControl const bool  useHard,
const bool  useSoft
 

Set the serial port flow control on Input as well as Ouput.

This function is to change the flow control while already started.

Parameters:
useHard use hardware flow control if true
useSoft use software flow control if true
Returns:
0 or serialErrSuccess on success

Definition at line 310 of file Serial.C.

References itsFlowHard, itsFlowSoft, and NModelParam< T >::setVal().

Referenced by enablePort().

serialError Serial::setParity const bool  useParity,
const bool  oddParity
 

Set parity mode.

This function is to change the parity while already started.

Parameters:
useParity use some parity if true
oddParity parity is odd if true, otherwise even
Returns:
0 on success.

Definition at line 370 of file Serial.C.

References itsParityOdd, itsUseParity, and NModelParam< T >::setVal().

Referenced by enablePort().

serialError Serial::setSpeed const int  speed  ) 
 

Set serial port speed for both input and output.

This function is to change the speed while already started.

Parameters:
speed to select. 0 signifies modem "hang up" and possible values are 0, 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200.
Returns:
0 or serialErrSuccess on success. The perror() method can be used to print a longer description of the error.

Definition at line 234 of file Serial.C.

References itsBaud, and NModelParam< T >::setVal().

Referenced by enablePort().

serialError Serial::setStopBits const int  bits  ) 
 

Set number of stop bits.

This function is to change the stop bits while started.

Returns:
0 on success.
Parameters:
bits stop bits (only values 1 and 2 are valid).

Definition at line 401 of file Serial.C.

References itsStopBits, and NModelParam< T >::setVal().

void Serial::start2  )  [protected, virtual]
 

open the port and get started

Reimplemented from ModelComponent.

Definition at line 82 of file Serial.C.

References enablePort(), OModelParam< T >::getVal(), NModelParam< T >::getVal(), itsCmdDevName, itsDeviceDescription, itsSearchPrefix, LFATAL, LINFO, readFrame(), OModelParam< T >::setVal(), and write().

void Serial::stop2  )  [protected, virtual]
 

close the port and get stopped

Reimplemented from ModelComponent.

Definition at line 171 of file Serial.C.

References closePort().

void Serial::toggleDTR const time_t  millisec  ) 
 

Set the DTR mode off momentarily.

Parameters:
millisec number of milliseconds.

Definition at line 439 of file Serial.C.

int Serial::write const unsigned char  character  ) 
 

write a single byte to the port - a simple wrapper around write(const void* buffer, const int nbytes)

Parameters:
character the single byte to write
Returns:
-1 on failure, 1 on successs.

Definition at line 695 of file Serial.C.

References write().

int Serial::write const void *  buffer,
const int  nbytes
 

write bytes to the port

Parameters:
buffer begin writing from the location buffer.
nbytes number of bytes to write
Returns:
-1 on failure, number of bytes written on success

Definition at line 670 of file Serial.C.

References n.

Referenced by start2(), and write().


Member Data Documentation

NModelParam<int> Serial::itsBaud [protected]
 

Baudrate.

Definition at line 233 of file Serial.H.

Referenced by configure(), enablePort(), and setSpeed().

NModelParam<bool> Serial::itsBlocking [protected]
 

use blocking mode

Definition at line 241 of file Serial.H.

Referenced by enablePort(), and setBlocking().

NModelParam<int> Serial::itsCharBits [protected]
 

number of useful bits per char

Definition at line 234 of file Serial.H.

Referenced by configure(), enablePort(), and setCharBits().

OModelParam<std::string> Serial::itsCmdDevName [protected]
 

device name passed from cmd line

Definition at line 226 of file Serial.H.

Referenced by start2().

NModelParam<std::string> Serial::itsDeviceDescription [protected]
 

device description to seach for

Definition at line 231 of file Serial.H.

Referenced by configureSearch(), getDeviceDescriptor(), and start2().

NModelParam<bool> Serial::itsFlowHard [protected]
 

use hardware flow control if true

Definition at line 238 of file Serial.H.

Referenced by configure(), enablePort(), and setFlowControl().

NModelParam<bool> Serial::itsFlowSoft [protected]
 

use software flow control if true

Definition at line 239 of file Serial.H.

Referenced by configure(), enablePort(), and setFlowControl().

NModelParam<bool> Serial::itsParityOdd [protected]
 

parity is odd if true, otherwise even

Definition at line 237 of file Serial.H.

Referenced by configure(), enablePort(), and setParity().

NModelParam<int> Serial::itsRdTout [protected]
 

read timeout in 1/10000s

Definition at line 240 of file Serial.H.

Referenced by configure().

NModelParam<std::string> Serial::itsSearchPrefix [protected]
 

device name prefix (e.g. ttyUSB)

Definition at line 232 of file Serial.H.

Referenced by configureSearch(), and start2().

NModelParam<int> Serial::itsStopBits [protected]
 

number of stop bits

Definition at line 235 of file Serial.H.

Referenced by configure(), and setStopBits().

NModelParam<bool> Serial::itsUseParity [protected]
 

use parity if true

Definition at line 236 of file Serial.H.

Referenced by configure(), enablePort(), and setParity().


The documentation for this class was generated from the following files:
Generated on Sun Nov 22 13:46:26 2009 for iLab Neuromorphic Vision Toolkit by  doxygen 1.4.4