WPILib  2014.0
WPIRoboticsLibraryforFRC
 All Classes Functions Variables Pages
Public Types | Public Member Functions | List of all members
SerialPort Class Reference

#include <SerialPort.h>

Inheritance diagram for SerialPort:
ErrorBase

Public Types

enum  Parity {
  kParity_None =0, kParity_Odd =1, kParity_Even =2, kParity_Mark =3,
  kParity_Space =4
}
 
enum  StopBits { kStopBits_One =10, kStopBits_OnePointFive =15, kStopBits_Two =20 }
 
enum  FlowControl { kFlowControl_None =0, kFlowControl_XonXoff =1, kFlowControl_RtsCts =2, kFlowControl_DtrDsr =4 }
 
enum  WriteBufferMode { kFlushOnAccess =1, kFlushWhenFull =2 }
 

Public Member Functions

 SerialPort (uint32_t baudRate, uint8_t dataBits=8, Parity parity=kParity_None, StopBits stopBits=kStopBits_One)
 
 ~SerialPort ()
 
void SetFlowControl (FlowControl flowControl)
 
void EnableTermination (char terminator= '\n')
 
void DisableTermination ()
 
int32_t GetBytesReceived ()
 
void Printf (const char *writeFmt,...)
 
void Scanf (const char *readFmt,...)
 
uint32_t Read (char *buffer, int32_t count)
 
uint32_t Write (const char *buffer, int32_t count)
 
void SetTimeout (float timeout)
 
void SetReadBufferSize (uint32_t size)
 
void SetWriteBufferSize (uint32_t size)
 
void SetWriteBufferMode (WriteBufferMode mode)
 
void Flush ()
 
void Reset ()
 
- Public Member Functions inherited from ErrorBase
virtual ErrorGetError ()
 Retrieve the current error. Get the current error information associated with this sensor.
 
virtual const ErrorGetError () const
 
virtual void SetErrnoError (const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
 Set error information associated with a C library call that set an error to the "errno" global variable. More...
 
virtual void SetImaqError (int success, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
 Set the current error information associated from the nivision Imaq API. More...
 
virtual void SetError (Error::Code code, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void SetWPIError (const char *errorMessage, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void CloneError (ErrorBase *rhs) const
 
virtual void ClearError () const
 Clear the current error information associated with this sensor.
 
virtual bool StatusIsFatal () const
 Check if the current error code represents a fatal error. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from ErrorBase
static void SetGlobalError (Error::Code code, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber)
 
static void SetGlobalWPIError (const char *errorMessage, const char *contextMessage, const char *filename, const char *function, uint32_t lineNumber)
 
static ErrorGetGlobalError ()
 
- Protected Member Functions inherited from ErrorBase
 ErrorBase ()
 Initialize the instance status to 0 for now.
 
- Protected Attributes inherited from ErrorBase
Error m_error
 
- Static Protected Attributes inherited from ErrorBase
static SEM_ID _globalErrorMutex = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE)
 
static Error _globalError
 

Detailed Description

Driver for the RS-232 serial port on the cRIO.

The current implementation uses the VISA formatted I/O mode. This means that all traffic goes through the fomatted buffers. This allows the intermingled use of Printf(), Scanf(), and the raw buffer accessors Read() and Write().

More information can be found in the NI-VISA User Manual here: http://www.ni.com/pdf/manuals/370423a.pdf and the NI-VISA Programmer's Reference Manual here: http://www.ni.com/pdf/manuals/370132c.pdf

Constructor & Destructor Documentation

SerialPort::SerialPort ( uint32_t  baudRate,
uint8_t  dataBits = 8,
SerialPort::Parity  parity = kParity_None,
SerialPort::StopBits  stopBits = kStopBits_One 
)

Create an instance of a Serial Port class.

Parameters
baudRateThe baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
dataBitsThe number of data bits per transfer. Valid values are between 5 and 8 bits.
paritySelect the type of parity checking to use.
stopBitsThe number of stop bits to use as defined by the enum StopBits.
SerialPort::~SerialPort ( )

Destructor.

Member Function Documentation

void SerialPort::DisableTermination ( )

Disable termination behavior.

void SerialPort::EnableTermination ( char  terminator = '\n')

Enable termination and specify the termination character.

Termination is currently only implemented for receive. When the the terminator is recieved, the Read() or Scanf() will return fewer bytes than requested, stopping after the terminator.

Parameters
terminatorThe character to use for termination.
void SerialPort::Flush ( )

Force the output buffer to be written to the port.

This is used when SetWriteBufferMode() is set to kFlushWhenFull to force a flush before the buffer is full.

int32_t SerialPort::GetBytesReceived ( )

Get the number of bytes currently available to read from the serial port.

Returns
The number of bytes available to read.
void SerialPort::Printf ( const char *  writeFmt,
  ... 
)

Output formatted text to the serial port.

Bug:
All pointer-based parameters seem to return an error.
Parameters
writeFmtA string that defines the format of the output.
uint32_t SerialPort::Read ( char *  buffer,
int32_t  count 
)

Read raw bytes out of the buffer.

Parameters
bufferPointer to the buffer to store the bytes in.
countThe maximum number of bytes to read.
Returns
The number of bytes actually read into the buffer.
void SerialPort::Reset ( )

Reset the serial port driver to a known state.

Empty the transmit and receive buffers in the device and formatted I/O.

void SerialPort::Scanf ( const char *  readFmt,
  ... 
)

Input formatted text from the serial port.

Bug:
All pointer-based parameters seem to return an error.
Parameters
readFmtA string that defines the format of the input.
void SerialPort::SetFlowControl ( SerialPort::FlowControl  flowControl)

Set the type of flow control to enable on this port.

By default, flow control is disabled.

void SerialPort::SetReadBufferSize ( uint32_t  size)

Specify the size of the input buffer.

Specify the amount of data that can be stored before data from the device is returned to Read or Scanf. If you want data that is recieved to be returned immediately, set this to 1.

It the buffer is not filled before the read timeout expires, all data that has been received so far will be returned.

Parameters
sizeThe read buffer size.
void SerialPort::SetTimeout ( float  timeout)

Configure the timeout of the serial port.

This defines the timeout for transactions with the hardware. It will affect reads and very large writes.

Parameters
timeoutThe number of seconds to to wait for I/O.
void SerialPort::SetWriteBufferMode ( SerialPort::WriteBufferMode  mode)

Specify the flushing behavior of the output buffer.

When set to kFlushOnAccess, data is synchronously written to the serial port after each call to either Printf() or Write().

When set to kFlushWhenFull, data will only be written to the serial port when the buffer is full or when Flush() is called.

Parameters
modeThe write buffer mode.
void SerialPort::SetWriteBufferSize ( uint32_t  size)

Specify the size of the output buffer.

Specify the amount of data that can be stored before being transmitted to the device.

Parameters
sizeThe write buffer size.
uint32_t SerialPort::Write ( const char *  buffer,
int32_t  count 
)

Write raw bytes to the buffer.

Parameters
bufferPointer to the buffer to read the bytes from.
countThe maximum number of bytes to write.
Returns
The number of bytes actually written into the port.

The documentation for this class was generated from the following files: