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

Class implementation for unbuffered I/O for an interrupt driven application or one that needs to have more control. More...

#include <UnbufferedSerial.h>

Inheritance diagram for UnbufferedSerial:
SerialBase FileHandle NonCopyable< UnbufferedSerial > NonCopyable< FileHandle >

Public Member Functions

 UnbufferedSerial (PinName tx, PinName rx, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a serial port instance connected to the specified transmit and receive pins, with the specified baud rate. More...
 
 UnbufferedSerial (const serial_pinmap_t &static_pinmap, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
 Create a UnbufferedSerial port, connected to the specified transmit and receive pins, with a particular baud rate. More...
 
ssize_t write (const void *buffer, size_t size) override
 Write the contents of a buffer to a file. More...
 
ssize_t read (void *buffer, size_t size) override
 Read the contents of a file into a buffer. More...
 
off_t seek (off_t offset, int whence=SEEK_SET) override
 Move the file position to a given offset from from a given location. More...
 
off_t size () override
 Get the size of the file. More...
 
int isatty () override
 Check if the file in an interactive terminal device. More...
 
int close () override
 Close a file. More...
 
int enable_input (bool enabled) override
 Enable or disable input. More...
 
int enable_output (bool enabled) override
 Enable or disable output. More...
 
short poll (short events) const override
 Check for poll event flags Check the events listed in events to see if data can be read or written without blocking. More...
 
void set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC)
 Set the flow control type on the serial port. More...
 
void attach (Callback< void()> func, IrqType type=RxIrq)
 Attach a function to call whenever a serial interrupt is generated. More...
 
void baud (int baudrate)
 Set the baud rate of the serial port. More...
 
void format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
 Set the transmission format used by the serial port. More...
 
int readable ()
 Determine if there is a character available to read. More...
 
int writeable ()
 Determine if there is space available to write a character. More...
 
virtual int sync ()
 Flush any buffers associated with the file. 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 int truncate (off_t length)
 Truncate or extend a file. More...
 
virtual int set_blocking (bool blocking)
 Set blocking or nonblocking mode of the file operation like read/write. More...
 
virtual bool is_blocking () const
 Check current blocking or nonblocking mode for file operations. More...
 
bool writable () const
 Definition depends on the subclass implementing FileHandle. More...
 
bool readable () const
 Definition depends on the subclass implementing FileHandle. More...
 
virtual void sigio (Callback< void()> func)
 Register a callback on state change of the file. More...
 

Private Member Functions

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 implementation for unbuffered I/O for an interrupt driven application or one that needs to have more control.

Definition at line 47 of file UnbufferedSerial.h.

Member Function Documentation

◆ attach()

void attach ( Callback< void()>  func,
IrqType  type = RxIrq 
)

Attach a function to call whenever a serial interrupt is generated.

Parameters
funcA pointer to a void function, or 0 to set as none
typeWhich serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)

◆ baud()

void baud ( int  baudrate)

Set the baud rate of the serial port.

Parameters
baudrateThe baudrate of the serial port (default = platform.default-serial-baud-rate).

◆ format()

void format ( int  bits = 8,
Parity  parity = SerialBase::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 (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
stop_bitsThe number of stop bits (1 or 2; default = 1)

◆ readable() [1/2]

int readable ( )

Determine if there is a character available to read.

Returns
1 if there is a character available to read, 0 otherwise

◆ writeable()

int writeable ( )

Determine if there is space available to write a character.

Returns
1 if there is space to write a character, 0 otherwise

◆ sync()

virtual int sync ( )
virtualinherited

Flush any buffers associated with the file.

Returns
0 on success, negative error code on failure

Reimplemented in LocalFileHandle, Stream, File, and BufferedSerial.

Definition at line 99 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.

◆ 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.

◆ set_blocking()

virtual int set_blocking ( bool  blocking)
virtualinherited

Set blocking or nonblocking mode of the file operation like read/write.

Definition depends on the subclass implementing FileHandle. The default is blocking.

Parameters
blockingtrue for blocking mode, false for nonblocking mode.
Returns
0 on success
Negative error code on failure

Reimplemented in BufferedSerial.

Definition at line 165 of file FileHandle.h.

◆ is_blocking()

virtual bool is_blocking ( ) const
virtualinherited

Check current blocking or nonblocking mode for file operations.

Returns
true for blocking mode, false for nonblocking mode.

Reimplemented in BufferedSerial.

Definition at line 174 of file FileHandle.h.

◆ writable()

bool writable ( ) const
inherited

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.

◆ readable() [2/2]

bool readable ( ) const
inherited

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.

◆ sigio()

virtual void sigio ( Callback< void()>  func)
virtualinherited

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 is 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 in BufferedSerial.

Definition at line 270 of file FileHandle.h.