Mbed OS Reference
Loading...
Searching...
No Matches
BufferedSerial Class Reference

Class providing buffered UART communication functionality using separate circular buffer for send and receive channels. More...

#include <BufferedSerial.h>

Inheritance diagram for BufferedSerial:
SerialBase FileHandle NonCopyable< BufferedSerial > NonCopyable< FileHandle > EMW3080B_UART

Public Member Functions

 BufferedSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a BufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate. More...
 
 BufferedSerial (const serial_pinmap_t &static_pinmap, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a BufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate. More...
 
short poll (short events) const final
 Equivalent to POSIX poll(). More...
 
ssize_t write (const void *buffer, size_t length) override
 Write the contents of a buffer to a file. More...
 
ssize_t read (void *buffer, size_t length) override
 Read the contents of a file into a buffer. More...
 
int close () override
 Close a file. More...
 
int isatty () override
 Check if the file in an interactive terminal device. More...
 
off_t seek (off_t offset, int whence) override
 Move the file position to a given offset from from a given location. More...
 
int sync () override
 Flush any buffers associated with the file. More...
 
int set_blocking (bool blocking) override
 Set blocking or non-blocking mode The default is blocking. More...
 
bool is_blocking () const override
 Check current blocking or non-blocking mode for file operations. More...
 
int enable_input (bool enabled) override
 Enable or disable input. More...
 
int enable_output (bool enabled) override
 Enable or disable output. More...
 
void sigio (Callback< void()> func) override
 Register a callback on state change of the file. More...
 
void set_data_carrier_detect (PinName dcd_pin, bool active_high=false)
 Setup interrupt handler for DCD line. More...
 
void set_baud (int baud)
 Set the baud rate. More...
 
void set_format (int bits=8, Parity parity=BufferedSerial::None, int stop_bits=1)
 Set the transmission format used by the serial port. More...
 
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port. More...
 
bool readable () const
 Definition depends on the subclass implementing FileHandle. More...
 
bool writable () const
 Definition depends on the subclass implementing FileHandle. More...
 
virtual off_t tell ()
 Get the file position of the file. More...
 
virtual void rewind ()
 Rewind the file position to the beginning of the file. More...
 
virtual off_t size ()
 Get the size of the file. More...
 
virtual int truncate (off_t length)
 Truncate or extend a file. More...
 

Private Member Functions

int readable ()
 Determine if there is a character available to read. More...
 
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port. More...
 
void set_flow_control (Flow type, const serial_fc_pinmap_t &static_pinmap)
 Set the flow control type on the serial port. More...
 
int write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 8bit buffer. More...
 
int write (const uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
 Begin asynchronous write using 16bit buffer. More...
 
int read (uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
 Begin asynchronous reading using 8bit buffer. More...
 
int read (uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
 Begin asynchronous reading using 16bit buffer. More...
 

Detailed Description

Class providing buffered UART communication functionality using separate circular buffer for send and receive channels.

Definition at line 52 of file BufferedSerial.h.

Constructor & Destructor Documentation

◆ BufferedSerial() [1/2]

BufferedSerial ( PinName  tx,
PinName  rx,
int  baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE 
)

Create a BufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate.

Parameters
txTransmit pin
rxReceive pin
baudThe baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE, which is set via platform.default-serial-baud-rate in mbed_app.json)

◆ BufferedSerial() [2/2]

BufferedSerial ( const serial_pinmap_t static_pinmap,
int  baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE 
)

Create a BufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate.

Parameters
static_pinmapreference to structure which holds static pinmap
baudThe baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE, which is set via platform.default-serial-baud-rate in mbed_app.json)

Member Function Documentation

◆ poll()

short poll ( short  events) const
finalvirtual

Equivalent to POSIX poll().

Derived from FileHandle. Provides a mechanism to multiplex input/output over a set of file handles. The events that can be reported are POLLIN, POLLOUT, POLLHUP.

Reimplemented from FileHandle.

◆ write()

ssize_t write ( const void *  buffer,
size_t  length 
)
overridevirtual

Write the contents of a buffer to a file.

Follows POSIX semantics:

  • if blocking, block until all data is written
  • if no data can be written, and non-blocking set, return -EAGAIN
  • if some data can be written, and non-blocking set, write partial
Parameters
bufferThe buffer to write from
lengthThe number of bytes to write
Returns
The number of bytes written, negative error on failure

Implements FileHandle.

◆ read()

ssize_t read ( void *  buffer,
size_t  length 
)
overridevirtual

Read the contents of a file into a buffer.

Follows POSIX semantics:

  • if no data is available, and non-blocking set return -EAGAIN
  • if no data is available, and blocking set, wait until data is available
  • If any data is available, call returns immediately
Parameters
bufferThe buffer to read in to
lengthThe number of bytes to read
Returns
The number of bytes read, 0 at end of file, negative error on failure

Implements FileHandle.

◆ close()

int close ( )
overridevirtual

Close a file.

Returns
0 on success, negative error code on failure

Implements FileHandle.

◆ isatty()

int isatty ( )
overridevirtual

Check if the file in an interactive terminal device.

Returns
True if the file is a terminal
False if the file is not a terminal
Negative error code on failure

Reimplemented from FileHandle.

◆ seek()

off_t seek ( off_t  offset,
int  whence 
)
overridevirtual

Move the file position to a given offset from from a given location.

Not valid for a device type FileHandle like BufferedSerial. In case of BufferedSerial, returns ESPIPE

Parameters
offsetThe offset from whence to move to
whenceThe start of where to seek SEEK_SET to start from beginning of file, SEEK_CUR to start from current position in file, SEEK_END to start from end of file
Returns
The new offset of the file, negative error code on failure

Implements FileHandle.

◆ sync()

int sync ( )
overridevirtual

Flush any buffers associated with the file.

Returns
0 on success, negative error code on failure

Reimplemented from FileHandle.

◆ set_blocking()

int set_blocking ( bool  blocking)
overridevirtual

Set blocking or non-blocking mode The default is blocking.

Parameters
blockingtrue for blocking mode, false for non-blocking mode.

Reimplemented from FileHandle.

Definition at line 170 of file BufferedSerial.h.

◆ is_blocking()

bool is_blocking ( ) const
overridevirtual

Check current blocking or non-blocking mode for file operations.

Returns
true for blocking mode, false for non-blocking mode.

Reimplemented from FileHandle.

Definition at line 180 of file BufferedSerial.h.

◆ enable_input()

int enable_input ( bool  enabled)
overridevirtual

Enable or disable input.

Control enabling of device for input. This is primarily intended for temporary power-saving; the overall ability of the device to operate for input and/or output may be fixed at creation time, but this call can allow input to be temporarily disabled to permit power saving without losing device state.

Parameters
enabledtrue to enable input, false to disable.
Returns
0 on success
Negative error code on failure

Reimplemented from FileHandle.

◆ enable_output()

int enable_output ( bool  enabled)
overridevirtual

Enable or disable output.

Control enabling of device for output. This is primarily intended for temporary power-saving; the overall ability of the device to operate for input and/or output may be fixed at creation time, but this call can allow output to be temporarily disabled to permit power saving without losing device state.

Parameters
enabledtrue to enable output, false to disable.
Returns
0 on success
Negative error code on failure

Reimplemented from FileHandle.

◆ sigio()

void sigio ( Callback< void()>  func)
overridevirtual

Register a callback on state change of the file.

The specified callback will be called on state changes such as when the file can be written to or read from.

The callback may be called in an interrupt context and should not perform expensive operations.

Note! This is not intended as an attach-like asynchronous api, but rather as a building block for constructing such functionality.

The exact timing of when the registered function is called is not guaranteed and susceptible to change. It should be used as a cue to make read/write/poll calls to find the current state.

Parameters
funcFunction to call on state change

Reimplemented from FileHandle.

◆ set_data_carrier_detect()

void set_data_carrier_detect ( PinName  dcd_pin,
bool  active_high = false 
)

Setup interrupt handler for DCD line.

If DCD line is connected, an IRQ handler will be setup. Does nothing if DCD is NC, i.e., not connected.

Parameters
dcd_pinPin-name for DCD
active_higha boolean set to true if DCD polarity is active low

◆ set_baud()

void set_baud ( int  baud)

Set the baud rate.

Parameters
baudThe baud rate

◆ set_format()

void set_format ( int  bits = 8,
Parity  parity = BufferedSerial::None,
int  stop_bits = 1 
)

Set the transmission format used by the serial port.

Parameters
bitsThe number of bits in a word (5-8; default = 8)
parityThe parity used (None, Odd, Even, Forced1, Forced0; default = None)
stop_bitsThe number of stop bits (1 or 2; default = 1)

◆ set_flow_control()

void set_flow_control ( Flow  type,
PinName  flow1 = NC,
PinName  flow2 = NC 
)

Set the flow control type on the serial port.

Parameters
typethe flow control type (Disabled, RTS, CTS, RTSCTS)
flow1the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
flow2the second flow control pin (CTS for RTSCTS)

◆ readable()

bool readable ( ) const

Definition depends on the subclass implementing FileHandle.

For example, if the FileHandle is of type Stream, readable() could return true when there is something available to read.

Returns
true when there is something available to read.

Definition at line 248 of file FileHandle.h.

◆ writable()

bool writable ( ) const

Definition depends on the subclass implementing FileHandle.

For example, if the FileHandle is of type Stream, writable() could return true when there is ample buffer space available for write() calls.

Returns
true if the FileHandle is writable.

Definition at line 237 of file FileHandle.h.

◆ tell()

virtual off_t tell ( )
virtualinherited

Get the file position of the file.

Note
This is equivalent to seek(0, SEEK_CUR)
Returns
The current offset in the file, negative error code on failure

Reimplemented in Stream, and File.

Definition at line 121 of file FileHandle.h.

◆ rewind()

virtual void rewind ( )
virtualinherited

Rewind the file position to the beginning of the file.

Note
This is equivalent to seek(0, SEEK_SET)

Reimplemented in Stream, and File.

Definition at line 130 of file FileHandle.h.

◆ size()

virtual off_t size ( )
virtualinherited

Get the size of the file.

Returns
Size of the file in bytes

Reimplemented in LocalFileHandle, Stream, File, SerialWireOutput, and UnbufferedSerial.

◆ truncate()

virtual int truncate ( off_t  length)
virtualinherited

Truncate or extend a file.

The file's length is set to the specified value. The seek pointer is not changed. If the file is extended, the extended area appears as if it were zero-filled.

Parameters
lengthThe requested new length for the file
Returns
Zero on success, negative error code on failure

Reimplemented in File.

Definition at line 151 of file FileHandle.h.