M5Unit-RS485 0.1.0 git rev:8a0af48
Loading...
Searching...
No Matches
m5::unit::RS485Component Class Reference

Non-instantiable base class for RS-485 transceiver units. More...

#include <rs485_component.hpp>

Inheritance diagram for m5::unit::RS485Component:
m5::unit::UnitSIT3088 m5::unit::UnitSP485 m5::unit::Tab5BuiltinRS485

Classes

struct  byteswap_float_tag
 
struct  byteswap_int_tag
 
struct  byteswap_none_tag
 
struct  byteswap_tag_of
 
struct  config_t
 Settings for begin. More...
 

Public Member Functions

virtual ~RS485Component ()=default
 Destructor.
 
virtual bool begin () override
 Begin using the registered UART adapter.
 
Settings for begin
config_t config ()
 Gets the configuration.
 
void config (const config_t &cfg)
 Sets the configuration.
 
Arduino Serial Compatibility API
Note
See also Arduino documents
 operator bool () const
 Returns true if the unit is registered.
 
int available ()
 Returns the number of available bytes to read.
 
int availableForWrite ()
 Returns the number of bytes that can be written without blocking.
 
int peek ()
 Peeks the next incoming byte without removing it.
 
int read ()
 Reads one byte.
 
size_t read (uint8_t *buffer, const size_t size)
 Reads up to size bytes into buffer.
 
size_t read (char *buffer, const size_t size)
 Reads up to size bytes into buffer.
 
size_t readBytes (uint8_t *buffer, const size_t length)
 Reads bytes with timeout defined by underlying Serial.
 
size_t readBytes (char *buffer, const size_t length)
 Reads bytes with timeout defined by underlying Serial.
 
void flush ()
 Flushes the serial port.
 
void flush (const bool txOnly)
 Flushes the serial port.
 
size_t write (const uint8_t d, const bool flush=true)
 Writes one byte.
 
size_t write (const uint8_t *buffer, const size_t size, const bool flush=true)
 Writes bytes from buffer.
 
size_t write (const char *buffer, const size_t size, const bool flush=true)
 Writes bytes from buffer.
 
size_t write (const char *s, const bool flush=true)
 Writes a null-terminated string.
 
Read/Write a trivially copyable value
template<typename T , bool LittleEndian = true>
bool readValue (T &value)
 Read a trivially copyable value.
 
template<typename T , bool LittleEndian = true>
bool writeValue (const T value)
 Write a trivially copyable value.
 

Protected Member Functions

 RS485Component (const uint8_t addr)
 Base constructor (concrete subclasses pass their DEFAULT_ADDRESS)
 
ISerial * iserial ()
 
virtual std::unique_ptr< ISerial > make_serial (AdapterUART *ad)
 Factory hook: build the concrete ISerial to be used by begin().
 

Static Protected Member Functions

template<typename T >
static T byteswap_if_needed (T v)
 
template<typename T >
static T byteswap_if_needed_impl (T v, byteswap_int_tag)
 
template<typename T >
static T byteswap_if_needed_impl (T v, byteswap_float_tag)
 
template<typename T >
static T byteswap_if_needed_impl (T v, byteswap_none_tag)
 

Protected Attributes

std::unique_ptr< ISerial > _serial {}
 
config_t _cfg {}
 

Detailed Description

Non-instantiable base class for RS-485 transceiver units.

Member Function Documentation

◆ available()

int m5::unit::RS485Component::available ( )
inline

Returns the number of available bytes to read.

Returns
Number of bytes available

◆ availableForWrite()

int m5::unit::RS485Component::availableForWrite ( )
inline

Returns the number of bytes that can be written without blocking.

Returns
Number of bytes available for write

◆ begin()

bool m5::unit::RS485Component::begin ( )
overridevirtual

Begin using the registered UART adapter.

Returns
True if the adapter is valid and ready

Reimplemented in m5::unit::UnitSIT3088.

◆ config() [1/2]

config_t m5::unit::RS485Component::config ( )
inline

Gets the configuration.

Returns
Current configuration

◆ config() [2/2]

void m5::unit::RS485Component::config ( const config_t & cfg)
inline

Sets the configuration.

Parameters
cfgConfiguration settings

◆ flush()

void m5::unit::RS485Component::flush ( const bool txOnly)
inline

Flushes the serial port.

Parameters
txOnlyIf true, flush only TX

◆ make_serial()

std::unique_ptr< RS485Component::ISerial > m5::unit::RS485Component::make_serial ( AdapterUART * ad)
protectedvirtual

Factory hook: build the concrete ISerial to be used by begin().

Parameters
adAdapterUART obtained via asAdapter<AdapterUART>() (never null at call site)
Returns
Owning pointer to the ISerial implementation, or a null-behavior implementation on frameworks not yet supported.
Note
Default implementation wraps the Arduino HardwareSerial exposed by the adapter. Chip-specific subclasses (e.g. UnitSIT3088) override to install a decorator that adds direction-pin control, etc.

Reimplemented in m5::unit::UnitSIT3088.

◆ peek()

int m5::unit::RS485Component::peek ( )
inline

Peeks the next incoming byte without removing it.

Returns
Next byte or -1 if none

◆ read() [1/3]

int m5::unit::RS485Component::read ( )
inline

Reads one byte.

Returns
Byte read or -1 if none

◆ read() [2/3]

size_t m5::unit::RS485Component::read ( char * buffer,
const size_t size )
inline

Reads up to size bytes into buffer.

Parameters
bufferDestination buffer
sizeMaximum number of bytes to read
Returns
Number of bytes read

◆ read() [3/3]

size_t m5::unit::RS485Component::read ( uint8_t * buffer,
const size_t size )
inline

Reads up to size bytes into buffer.

Parameters
bufferDestination buffer
sizeMaximum number of bytes to read
Returns
Number of bytes read

◆ readBytes() [1/2]

size_t m5::unit::RS485Component::readBytes ( char * buffer,
const size_t length )
inline

Reads bytes with timeout defined by underlying Serial.

Parameters
bufferDestination buffer
lengthNumber of bytes to read
Returns
Number of bytes actually read

◆ readBytes() [2/2]

size_t m5::unit::RS485Component::readBytes ( uint8_t * buffer,
const size_t length )
inline

Reads bytes with timeout defined by underlying Serial.

Parameters
bufferDestination buffer
lengthNumber of bytes to read
Returns
Number of bytes actually read

◆ readValue()

template<typename T , bool LittleEndian = true>
bool m5::unit::RS485Component::readValue ( T & value)
inline

Read a trivially copyable value.

Template Parameters
TType (must be trivially copyable and standard-layout)
LittleEndianIncoming data is little-endian if true
Parameters
valueReceive variable
Returns
True if successful
Note
LittleEndian byteswap is applied for integral / enum types and for 32-bit / 64-bit floating-point types (via bit-cast). Aggregate / struct types are stored as-is in host byte order; the LittleEndian parameter is ignored for those.
LittleEndian only declares the wire byte order; the peer must agree on the same convention. Floating-point special values (NaN, +/-inf, +/-0) are byte-reversed as raw bit patterns without special-casing.

◆ write() [1/4]

size_t m5::unit::RS485Component::write ( const char * buffer,
const size_t size,
const bool flush = true )

Writes bytes from buffer.

Parameters
bufferSource buffer
sizeNumber of bytes to write
flushIf true, flush TX before returning
Returns
Number of bytes written

◆ write() [2/4]

size_t m5::unit::RS485Component::write ( const char * s,
const bool flush = true )

Writes a null-terminated string.

Parameters
sNull-terminated string
flushIf true, flush TX before returning
Returns
Number of bytes written

◆ write() [3/4]

size_t m5::unit::RS485Component::write ( const uint8_t * buffer,
const size_t size,
const bool flush = true )

Writes bytes from buffer.

Parameters
bufferSource buffer
sizeNumber of bytes to write
flushIf true, flush TX before returning
Returns
Number of bytes written

◆ write() [4/4]

size_t m5::unit::RS485Component::write ( const uint8_t d,
const bool flush = true )

Writes one byte.

Parameters
dByte to write
flushIf true, flush TX before returning
Returns
Number of bytes written

◆ writeValue()

template<typename T , bool LittleEndian = true>
bool m5::unit::RS485Component::writeValue ( const T value)
inline

Write a trivially copyable value.

Template Parameters
TType (must be trivially copyable and standard-layout)
LittleEndianOutgoing data is little-endian if true
Parameters
valueValue to write
Returns
True if successful
Note
LittleEndian byteswap is applied for integral / enum types and for 32-bit / 64-bit floating-point types (via bit-cast). Aggregate / struct types are sent as-is in host byte order; the LittleEndian parameter is ignored for those.
LittleEndian only declares the wire byte order; the peer must agree on the same convention. Floating-point special values (NaN, +/-inf, +/-0) are byte-reversed as raw bit patterns without special-casing.