M5Unit-RS485 0.2.0 git rev:0956c20
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_none_tag
 
struct  byteswap_tag_of
 
struct  byteswap_value_tag
 
struct  config_t
 Settings for begin. More...
 

Public Member Functions

virtual ~RS485Component ()=default
 Destructor.
 
virtual bool begin () override
 Begin using the registered UART adapter.
 
Constructor
Warning
COPY PROHIBITED
 RS485Component (const RS485Component &)=delete
 Deleted: a component owns its serial backend, so it cannot be copied.
 
Assignment
Warning
COPY PROHIBITED
RS485Component & operator= (const RS485Component &)=delete
 Deleted: a component owns its serial backend, so it cannot be copied.
 
Settings for begin
config_t config () const
 Gets the configuration.
 
void config (const config_t &cfg)
 Sets the configuration.
 
Arduino Serial Compatibility API
Note
See also Arduino documents
write() only queues the bytes; flush() is what waits for the transmission to complete. A subclass that drives a direction pin (UnitSIT3088) has to drain inside its own ISerial before releasing the line, so its write() blocks even though this one does not.
 operator bool () const
 Returns true if the unit is registered.
 
uint32_t baudRate () const
 Returns the baud rate the serial port is running at.
 
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.
 
size_t readBytes (uint8_t *buffer, const size_t length, const uint32_t timeout_ms)
 Reads bytes with an explicit timeout.
 
size_t readBytes (char *buffer, const size_t length, const uint32_t timeout_ms)
 Reads bytes with an explicit timeout.
 
void flush ()
 Flushes the serial port.
 
void flush (const bool txOnly)
 Flushes the serial port.
 
size_t write (const uint8_t d)
 Writes one byte.
 
size_t write (const uint8_t *buffer, const size_t size)
 Writes bytes from buffer.
 
size_t write (const char *buffer, const size_t size)
 Writes bytes from buffer.
 
size_t write (const char *s)
 Writes a null-terminated string.
 
template<typename Size , typename std::enable_if<!std::is_integral< Size >::value||std::is_same< Size, char >::value||std::is_same< Size, signed char >::value||std::is_same< Size, bool >::value, int >::type = 0>
size_t write (const uint8_t *buffer, const Size size)=delete
 Deleted: the second argument of write() is a byte count.
 
template<typename Size , typename std::enable_if<!std::is_integral< Size >::value||std::is_same< Size, char >::value||std::is_same< Size, signed char >::value, int >::type = 0>
size_t write (const char *buffer, const Size size)=delete
 Deleted: the second argument of write() is a byte count.
 
Deprecated Arduino Serial Compatibility API
template<typename Wait , typename std::enable_if< std::is_same< Wait, bool >::value, int >::type = 0>
size_t write (const uint8_t d, const Wait wait)
 Writes one byte, optionally waiting for the transmission to complete.
 
size_t write (const uint8_t *buffer, const size_t size, const bool wait)
 Writes bytes from buffer, optionally waiting for the transmission to complete.
 
size_t write (const char *buffer, const size_t size, const bool wait)
 Writes bytes from buffer, optionally waiting for the transmission to complete.
 
template<typename Wait , typename std::enable_if< std::is_same< Wait, bool >::value, int >::type = 0>
size_t write (const char *s, const Wait wait)
 Writes a null-terminated string, optionally waiting for the transmission to complete.
 
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)
 
 RS485Component (RS485Component &&other) noexcept
 Move constructor.
 
RS485Component & operator= (RS485Component &&other) noexcept
 Move assignment.
 
size_t write_waiting (const uint8_t *buffer, const size_t size)
 
ISerial * iserial ()
 
void install_serial (std::unique_ptr< ISerial > serial)
 Installs a serial backend, taking over ownership and replacing the current one.
 
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_value_tag)
 
template<typename T >
static T byteswap_if_needed_impl (T v, byteswap_none_tag)
 

Protected Attributes

config_t _cfg {}
 

Detailed Description

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

Constructor & Destructor Documentation

◆ RS485Component()

m5::unit::RS485Component::RS485Component ( RS485Component && other)
protectednoexcept

Move constructor.

Parameters
otherComponent to move from

The serial backend is taken over and told about its new owner, so a backend that points back at the component it serves (UnitSIT3088 drives the DIR pin through the unit) follows the move. The moved-from component is left with the null-behavior backend, so its Serial-compatible methods keep answering.

Note
Protected on purpose: moving through a reference to this base would hand a backend built for one unit to a component of another type. The concrete units declare their own move members, so a unit is movable as a unit.
Do not move a unit once UnitUnified::add() has registered it: UnitUnified keeps the address it was given, and the links to a parent or child component are copied rather than redirected.
Recreate any UnitSP485Stream that wraps the unit: it holds a reference to the component it was built from, which after a move answers through the null backend.

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

◆ baudRate()

uint32_t m5::unit::RS485Component::baudRate ( ) const
inline

Returns the baud rate the serial port is running at.

Returns
Baud rate in bps, or 0 while no serial backend is installed, or if the driver cannot report it
Note
No backend is installed before the first successful begin(), and on a component that has been moved from. A begin() that fails leaves the backend already installed.

◆ 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 ( ) const
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

◆ install_serial()

void m5::unit::RS485Component::install_serial ( std::unique_ptr< ISerial > serial)
protected

Installs a serial backend, taking over ownership and replacing the current one.

Parameters
serialBackend to install, typically the result of make_serial()
Note
A null argument is ignored: every Serial-compatible method dereferences the backend unconditionally, so the component must never be left without one.

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

◆ operator=()

RS485Component & m5::unit::RS485Component::operator= ( RS485Component && other)
protectednoexcept

Move assignment.

Parameters
otherComponent to move from
Returns
Reference to this component

Hands over the serial backend as the move constructor does, and releases the backend this component held.

Note
The same three cautions as for the move constructor apply.

◆ 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/4]

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/4]

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

Reads bytes with an explicit timeout.

Parameters
bufferDestination buffer
lengthNumber of bytes to read
timeout_msMaximum time to wait for the requested bytes
Returns
Number of bytes actually read

◆ readBytes() [3/4]

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

◆ readBytes() [4/4]

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

Reads bytes with an explicit timeout.

Parameters
bufferDestination buffer
lengthNumber of bytes to read
timeout_msMaximum time to wait for the requested bytes
Returns
Number of bytes actually read
Note
Prefer this over the two-argument form: it does not depend on the backend's timeout state, which is only reachable through framework-specific API.

◆ 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.
Waits for sizeof(T) bytes with the backend's own timeout, as the two-argument readBytes() does: the one set by HardwareSerial::setTimeout() on Arduino, and a fixed 1000 ms on ESP-IDF native. Use readBytes() with an explicit timeout to control it.
sizeof(T) must be 256 bytes or less: the value goes through a stack buffer, and 256 bytes is the largest Modbus RTU frame as well as this library's default RX buffer size.

◆ write() [1/10]

template<typename Size , typename std::enable_if<!std::is_integral< Size >::value||std::is_same< Size, char >::value||std::is_same< Size, signed char >::value, int >::type = 0>
size_t m5::unit::RS485Component::write ( const char * buffer,
const Size size )
delete

Deleted: the second argument of write() is a byte count.

The same rule as write(const uint8_t *, const Size), except that bool is still accepted here: write(s, wait) is a released signature, so the deprecated overload below has to keep binding it. For a uint8_t buffer the wait form was always write(buffer, size, wait), so there bool is rejected.

◆ write() [2/10]

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

Writes bytes from buffer.

Parameters
bufferSource buffer
sizeNumber of bytes to write
Returns
Number of bytes written
Note
Queues the bytes; call flush() to wait for the transmission to complete.

◆ write() [3/10]

size_t m5::unit::RS485Component::write ( const char * buffer,
const size_t size,
const bool wait )
inline

Writes bytes from buffer, optionally waiting for the transmission to complete.

Parameters
bufferSource buffer
sizeNumber of bytes to write
waitIf true, wait for TX to drain before returning
Returns
Number of bytes written
Deprecated
write() no longer waits; write the bytes and call flush() yourself.

◆ write() [4/10]

size_t m5::unit::RS485Component::write ( const char * s)

Writes a null-terminated string.

Parameters
sNull-terminated string
Returns
Number of bytes written
Note
Queues the bytes; call flush() to wait for the transmission to complete.

◆ write() [5/10]

template<typename Wait , typename std::enable_if< std::is_same< Wait, bool >::value, int >::type = 0>
size_t m5::unit::RS485Component::write ( const char * s,
const Wait wait )
inline

Writes a null-terminated string, optionally waiting for the transmission to complete.

Parameters
sNull-terminated string
waitIf true, wait for TX to drain before returning
Returns
Number of bytes written
Deprecated
write() no longer waits; write the bytes and call flush() yourself.
Note
Only an exact bool binds here, so write(buffer, length) with a plain integer length is never mistaken for this overload.

◆ write() [6/10]

template<typename Size , typename std::enable_if<!std::is_integral< Size >::value||std::is_same< Size, char >::value||std::is_same< Size, signed char >::value||std::is_same< Size, bool >::value, int >::type = 0>
size_t m5::unit::RS485Component::write ( const uint8_t * buffer,
const Size size )
delete

Deleted: the second argument of write() is a byte count.

Rejects what cannot be a count – char and signed char (write(buffer, 'a') would otherwise mean 97 bytes), bool, and any non-integral type, including the bool-convertible proxies of std::vector<bool> / std::bitset<N>, which used to slip through as a length. An integer length is filtered out by SFINAE and keeps resolving to write(const uint8_t *, size_t).

◆ write() [7/10]

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

Writes bytes from buffer.

Parameters
bufferSource buffer
sizeNumber of bytes to write
Returns
Number of bytes written
Note
Queues the bytes; call flush() to wait for the transmission to complete.

◆ write() [8/10]

size_t m5::unit::RS485Component::write ( const uint8_t * buffer,
const size_t size,
const bool wait )
inline

Writes bytes from buffer, optionally waiting for the transmission to complete.

Parameters
bufferSource buffer
sizeNumber of bytes to write
waitIf true, wait for TX to drain before returning
Returns
Number of bytes written
Deprecated
write() no longer waits; write the bytes and call flush() yourself.

◆ write() [9/10]

size_t m5::unit::RS485Component::write ( const uint8_t d)

Writes one byte.

Parameters
dByte to write
Returns
Number of bytes written
Note
Queues the byte, as Arduino's Print::write() does. Call flush() when the caller has to know the byte has left the wire.

◆ write() [10/10]

template<typename Wait , typename std::enable_if< std::is_same< Wait, bool >::value, int >::type = 0>
size_t m5::unit::RS485Component::write ( const uint8_t d,
const Wait wait )
inline

Writes one byte, optionally waiting for the transmission to complete.

Parameters
dByte to write
waitIf true, wait for TX to drain before returning
Returns
Number of bytes written
Deprecated
write() no longer waits; write the bytes and call flush() yourself.

◆ 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.
Unlike write(), this waits for the transmission to complete before returning, so a value is always fully on the wire when it reports success.
sizeof(T) must be 256 bytes or less: the value goes through a stack buffer, and 256 bytes is the largest Modbus RTU frame as well as this library's default RX buffer size.