SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
HostPort Class Reference

A class for host communication via serial port. More...

#include <HostPort.h>

Public Member Functions

 HostPort ()
 Constructor. More...
 
 ~HostPort ()
 Desctructor. More...
 
bool begin (unsigned int port, unsigned int baud)
 Begin the host port. More...
 
bool begin (unsigned int port, unsigned int baud, unsigned int header, unsigned int terminator)
 Begin the host port. More...
 
bool begin (unsigned int port, unsigned int baud, unsigned int header, unsigned int terminator, unsigned int timeout)
 Begin the host port. More...
 
bool read (unsigned char *packetPtr, unsigned int size)
 Read data buffer. More...
 
bool write (unsigned char *packetPtr, unsigned int size)
 Write data buffer. More...
 
bool close (void)
 Close the host port. More...
 
bool restart (void)
 Restart the host port. More...
 
bool setPort (unsigned int port)
 Set the serial port. More...
 
bool setBaud (unsigned int baud)
 Set the baudrate. More...
 
bool setHeader (unsigned int header)
 Set the header. More...
 
bool setTerminator (unsigned int terminator)
 Set the terminator. More...
 
unsigned int getPort (void)
 Get the serial port. More...
 
unsigned int getBaud (void)
 Get the baudate. More...
 
unsigned int getHeader (void)
 Get the header. More...
 
unsigned int getTerminator (void)
 Get the terminator. More...
 
bool isInit (void)
 Check if host port is open. More...
 
 operator bool ()
 Check if host port is open. More...
 

Static Public Attributes

static constexpr unsigned int HEADER = 0xFF812345
 Default header. More...
 
static constexpr unsigned int TERMINATOR = 0xFF8CABDE
 Default terminator. More...
 
static constexpr unsigned int TIMEOUT = 100
 Default timeout. More...
 

Private Member Functions

bool init (unsigned int port, unsigned int baud, unsigned int timeout)
 Private initialization function. More...
 

Private Attributes

Serial serial
 Serial object. More...
 
unsigned int _port
 Serial port. More...
 
unsigned int _baud
 Baudrate. More...
 
unsigned int _terminator
 Terminator bytes. More...
 
unsigned int _header
 Header bytes. More...
 
unsigned int _timeout
 Timeout. More...
 
unsigned char _tx_buf [1024]
 Tx buffer. More...
 
bool _isFirstRead = false
 True if firt packer read. More...
 

Static Private Attributes

static constexpr unsigned int MASK = 0xFF
 Mask for parsing. More...
 

Detailed Description

A class for host communication via serial port.

Class for host communication via serial port with a specified communication protocol. Data packet consists of

  • 4-bytes header
  • data (with specified length)
  • 4-bytes terminator No check sum is performed.
    Author
    Stefano Lovato
    Date
    2022

Constructor & Destructor Documentation

◆ HostPort()

HostPort::HostPort ( )

Constructor.

Default constructor.

◆ ~HostPort()

HostPort::~HostPort ( )

Desctructor.

Default destructor. It automatically destroy the serial object.

Member Function Documentation

◆ begin() [1/3]

bool HostPort::begin ( unsigned int  port,
unsigned int  baud 
)

Begin the host port.

Function to begin the host port communication with default header, terminator, and timeout.

Parameters
portThe serial port to use.
baudThe baudrate of the serial communication.
Returns
true if success, false otherwise.

◆ begin() [2/3]

bool HostPort::begin ( unsigned int  port,
unsigned int  baud,
unsigned int  header,
unsigned int  terminator 
)

Begin the host port.

Function to begin the host port communication with given header and terminator and default timeout.

Parameters
portThe serial port to use.
baudThe baudrate of the serial communication.
headerThe header to use (as a HEX number of 4-bytes).
terminatorThe terminator to use (as a HEX number of 4-bytes).
Returns
true if success, false otherwise.

◆ begin() [3/3]

bool HostPort::begin ( unsigned int  port,
unsigned int  baud,
unsigned int  header,
unsigned int  terminator,
unsigned int  timeout 
)

Begin the host port.

Function to begin the host port communication with given header, terminator and timeout.

Parameters
portThe serial port to use.
baudThe baudrate of the serial communication.
headerThe header to use (as a HEX number of 4-bytes).
terminatorThe terminator to use (as a HEX number of 4-bytes).
timeoutThe timeout of the serial communcication.
Returns
true if success, false otherwise.

◆ close()

bool HostPort::close ( void  )

Close the host port.

Function to close the host port. Host port must be opened before.

Returns
true if success, false otherwise.

◆ getBaud()

unsigned int HostPort::getBaud ( void  )

Get the baudate.

Function to get the baudrate used for the serial communication.

Returns
The baudrate.

◆ getHeader()

unsigned int HostPort::getHeader ( void  )

Get the header.

Function to get the header used for the host communication.

Returns
The header.

◆ getPort()

unsigned int HostPort::getPort ( void  )

Get the serial port.

Function to get the serial port used for the host communication.

Returns
The serial port number.

◆ getTerminator()

unsigned int HostPort::getTerminator ( void  )

Get the terminator.

Function to get the terminator used for the host communication.

Returns
The terminator.

◆ init()

bool HostPort::init ( unsigned int  port,
unsigned int  baud,
unsigned int  timeout 
)
private

Private initialization function.

◆ isInit()

bool HostPort::isInit ( void  )

Check if host port is open.

Function to check if the host port has been open successfully.

Returns
true if the host port is open, false otherwise.

◆ operator bool()

HostPort::operator bool ( )
inline

Check if host port is open.

Boolean operator to check if the host port has been open successfully.

Returns
true if the host port is open, false otherwise.

◆ read()

bool HostPort::read ( unsigned char *  packetPtr,
unsigned int  size 
)

Read data buffer.

Function to read a data buffer with given length from the host port. Host port must be opened before.

Parameters
packetPtrThe pointer to the data buffer.
sizeThe size of the data buffer.
Returns
true if success, false otherwise.

◆ restart()

bool HostPort::restart ( void  )

Restart the host port.

Function to restart the host port. Host port must be opened before.

Returns
true if success, false otherwise.

◆ setBaud()

bool HostPort::setBaud ( unsigned int  baud)

Set the baudrate.

Function to set the baudrate of the serial communiation. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setHeader()

bool HostPort::setHeader ( unsigned int  header)

Set the header.

Function to set the header of the host communication. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setPort()

bool HostPort::setPort ( unsigned int  port)

Set the serial port.

Function to set the serial port. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ setTerminator()

bool HostPort::setTerminator ( unsigned int  terminator)

Set the terminator.

Function to set the terminator of the host communication. The restart function must be called to make the change effective.

Returns
true if success, false otherwise.

◆ write()

bool HostPort::write ( unsigned char *  packetPtr,
unsigned int  size 
)

Write data buffer.

Function to write a data buffer with given length to the host port. Host port must be opened before.

Parameters
packetPtrThe pointer to the data buffer.
sizeThe size of the data buffer.
Returns
true if success, false otherwise.

Member Data Documentation

◆ _baud

unsigned int HostPort::_baud
private

Baudrate.

◆ _header

unsigned int HostPort::_header
private

Header bytes.

◆ _isFirstRead

bool HostPort::_isFirstRead = false
private

True if firt packer read.

◆ _port

unsigned int HostPort::_port
private

Serial port.

◆ _terminator

unsigned int HostPort::_terminator
private

Terminator bytes.

◆ _timeout

unsigned int HostPort::_timeout
private

Timeout.

◆ _tx_buf

unsigned char HostPort::_tx_buf[1024]
private

Tx buffer.

◆ HEADER

constexpr unsigned int HostPort::HEADER = 0xFF812345
staticconstexpr

Default header.

◆ MASK

constexpr unsigned int HostPort::MASK = 0xFF
staticconstexprprivate

Mask for parsing.

◆ serial

Serial HostPort::serial
private

Serial object.

◆ TERMINATOR

constexpr unsigned int HostPort::TERMINATOR = 0xFF8CABDE
staticconstexpr

Default terminator.

◆ TIMEOUT

constexpr unsigned int HostPort::TIMEOUT = 100
staticconstexpr

Default timeout.