M5Unit-RS485 0.0.1 git rev:d5ba212
Loading...
Searching...
No Matches
m5::unit::UnitSP485 Class Reference

RS-485 unit using SP485 transceiver. More...

#include <unit_SP485.hpp>

Inheritance diagram for m5::unit::UnitSP485:

Classes

struct  config_t
 Settings for begin. More...
 

Public Member Functions

 UnitSP485 ()
 Constructor.
 
virtual ~UnitSP485 ()=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 any value
template<typename T , bool LittleEndian = true>
bool readValue (T &value)
 Read the any number value.
 
template<typename T , bool LittleEndian = true>
bool writeValue (const T value)
 Write the any number value.
 

Protected Member Functions

ISerial * iserial ()
 

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, std::true_type)
 
template<typename T >
static T byteswap_if_needed_impl (T v, std::false_type)
 

Detailed Description

RS-485 unit using SP485 transceiver.

Member Function Documentation

◆ available()

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

Returns the number of available bytes to read.

Returns
Number of bytes available

◆ availableForWrite()

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

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

Returns
Number of bytes available for write

◆ begin()

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

Begin using the registered UART adapter.

Returns
True if the adapter is valid and ready

◆ config() [1/2]

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

Gets the configuration.

Returns
Current configuration

◆ config() [2/2]

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

Sets the configuration.

Parameters
cfgConfiguration settings

◆ flush()

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

Flushes the serial port.

Parameters
txOnlyIf true, flush only TX

◆ peek()

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

Peeks the next incoming byte without removing it.

Returns
Next byte or -1 if none

◆ read() [1/3]

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

Reads one byte.

Returns
Byte read or -1 if none

◆ read() [2/3]

size_t m5::unit::UnitSP485::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::UnitSP485::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::UnitSP485::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::UnitSP485::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::UnitSP485::readValue ( T & value)
inline

Read the any number value.

Template Parameters
TType
LittleEndianIncoming data is little-endian if true
Parameters
valueReceive variable
Returns
True if successful

◆ write() [1/4]

size_t m5::unit::UnitSP485::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::UnitSP485::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::UnitSP485::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::UnitSP485::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 (0 if receive buffer is not empty due to half-duplex constraint)
Note
Returns 0 without writing when available() > 0, as RS485 half-duplex cannot transmit while data is pending

◆ writeValue()

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

Write the any number value.

Template Parameters
TType
LittleEndianOutgoing data is little-endian if true
Parameters
valueValue to write
Returns
True if successful