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

A class for serial communication. More...

#include <Serial.h>

Public Member Functions

 Serial ()
 Constructor. More...
 
 ~Serial ()
 Desctructor. More...
 
bool begin (unsigned int port, unsigned int baud, unsigned int timeout)
 Begin the serial. More...
 
bool begin (unsigned int port, unsigned int baud)
 Begin the serial. More...
 
void flush ()
 Flush the serial. More...
 
bool end ()
 Close the serial. More...
 
bool write (unsigned char c)
 Send a byte. More...
 
size_t write (unsigned char *buf, size_t len)
 Send a buffer. More...
 
unsigned char read ()
 Read a byte. More...
 
size_t read (unsigned char *buf, size_t len)
 Read a buffer. More...
 
void setTimeout (unsigned int timeout)
 Set the timeout. More...
 
unsigned int getTimeout ()
 Get the timeout. More...
 
 operator bool ()
 Check if serial is open. More...
 

Static Public Member Functions

static std::vector< int > get_availableSerialPorts (unsigned int maxPortNumber)
 Get the available serial ports. More...
 

Static Public Attributes

static constexpr unsigned int TIMEOUT = 10
 Default timeout. More...
 

Private Attributes

int _serialPort
 serial port More...
 
struct termios _serialParams
 serial param struct More...
 
bool _isInit = false
 true if serial has been initialized More...
 
unsigned int _timeout = TIMEOUT
 timeout of the serial port More...
 

Detailed Description

A class for serial communication.

Class for managing a general-purpose serial communication in Windows (using COM port) and Linux (using /dev/ttyACM). This is just a C++ wrapper for the HANDLE type in Windows, and the open, write, read, etc functions in Linux.

Author
Stefano Lovato
Date
2022

Constructor & Destructor Documentation

◆ Serial()

Serial::Serial ( )

Constructor.

Default constructor for the serial class. No parameter is necessary.

◆ ~Serial()

Serial::~Serial ( )

Desctructor.

Default destructor for the serial class. No parameter is necessary. It automatically closes the communication if open.

Member Function Documentation

◆ begin() [1/2]

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

Begin the serial.

Function to begin the serial communication with the specified configuration (with a default timeout of 10ms).

Parameters
portthe port to be used for the serial communication.
baudthe baudrate of the serial communication.
Returns
true if the serial communication starts successfully, false otherwise.

◆ begin() [2/2]

bool Serial::begin ( unsigned int  port,
unsigned int  baud,
unsigned int  timeout 
)

Begin the serial.

Function to begin the serial communication with the specified configuration (including timeout).

Parameters
portthe port to be used for the serial communication.
baudthe baudrate of the serial communication.
timeoutthe timeout of the serial communication (in ms).
Returns
true if the serial communication starts successfully, false otherwise.

◆ end()

bool Serial::end ( )

Close the serial.

Function to close the serial communication. Serial must be opened before.

Returns
true if the serial communication is closed successfully.

◆ flush()

void Serial::flush ( )

Flush the serial.

Function to flush the receive buffer of the serial communication. Serial must be opened before.

◆ get_availableSerialPorts()

static std::vector< int > Serial::get_availableSerialPorts ( unsigned int  maxPortNumber)
static

Get the available serial ports.

Function to get the available serial ports. Ports from 0 to maxPorts are checked.

Parameters
maxPortNumberThe max port number to be checked.
Returns
A std::vector<int> containing the available serial ports.

◆ getTimeout()

unsigned int Serial::getTimeout ( )

Get the timeout.

Function to get the timeout the serial communication.

Returns
The timeout to use (in ms).

◆ operator bool()

Serial::operator bool ( )
inline

Check if serial is open.

Boolean operator to check if the serial communication has been open successfully.

Returns
true if the serial is open, false otherwise.

◆ read() [1/2]

unsigned char Serial::read ( )

Read a byte.

Function to read a byte via the serial communication. Serial must be opened before.

Returns
The byte read (null char if no available byte to read).

◆ read() [2/2]

size_t Serial::read ( unsigned char *  buf,
size_t  len 
)

Read a buffer.

Function to read a buffer via the serial communication. Serial must be opened before.

Parameters
bufThe pointer to the buffer where the read bytes are stored.
lenThe number of bytes to read.
Returns
The number of bytes actually read (equal to len if successfully).

◆ setTimeout()

void Serial::setTimeout ( unsigned int  timeout)

Set the timeout.

Function to set the timeout the serial communication.

Parameters
timeoutThe timeout to use (in ms).

◆ write() [1/2]

size_t Serial::write ( unsigned char *  buf,
size_t  len 
)

Send a buffer.

Function to send a buffer via the serial communication. Serial must be opened before.

Parameters
bufThe pointer to the buffer to be sent.
lenThe length of the buffer to be sent (in bytes).
Returns
The number of bytes sent (equal to len if all buffer is sent successfully).

◆ write() [2/2]

bool Serial::write ( unsigned char  c)

Send a byte.

Function to send a byte via the serial communication. Serial must be opened before.

Parameters
cThe byte to be sent.
Returns
true if the byte is sent successfully, false otherwise.

Member Data Documentation

◆ _isInit

bool Serial::_isInit = false
private

true if serial has been initialized

◆ _serialParams

struct termios Serial::_serialParams
private

serial param struct

◆ _serialPort

int Serial::_serialPort
private

serial port

◆ _timeout

unsigned int Serial::_timeout = TIMEOUT
private

timeout of the serial port

◆ TIMEOUT

constexpr unsigned int Serial::TIMEOUT = 10
staticconstexpr

Default timeout.