Skeletal Animation Multithread Face
Public Member Functions | Private Attributes | List of all members
SerialCOM Class Reference

Class SerialCOM. More...

#include <serial_com.h>

Public Member Functions

 SerialCOM (std::string port, unsigned int baud_rate)
 Constructor. More...
 
void writeString (std::string s)
 Write a string to the serial device. More...
 
std::string readLine (void)
 Blocks until a line is received from the serial device. More...
 
void initialize_stream (int iter=3)
 Setup up stream by reading the values a couple times first. More...
 

Private Attributes

boost::asio::io_service io
 Boost io service. More...
 
boost::asio::serial_port serial
 Boost serial port handle. More...
 

Detailed Description

Class SerialCOM.

This class handles all the serial communication between the PC and the exoskeleton board. It's based on boost::asio. See https://www.boost.org/doc/libs/1_75_0/doc/html/boost_asio.html.

Definition at line 12 of file serial_com.h.

Constructor & Destructor Documentation

◆ SerialCOM()

SerialCOM::SerialCOM ( std::string  port,
unsigned int  baud_rate 
)
inline

Constructor.

Constructor.

Parameters
portdevice name, example "/dev/ttyUSB0" or "COM4".
baud_ratecommunication speed, example 9600 or 115200.
Exceptions
boost::system::system_errorif cannot open the serial device.

Definition at line 23 of file serial_com.h.

24 : io(), serial(io,port)
25 {
26 serial.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
27 }
boost::asio::io_service io
Boost io service.
Definition: serial_com.h:40
boost::asio::serial_port serial
Boost serial port handle.
Definition: serial_com.h:43

Member Function Documentation

◆ initialize_stream()

void SerialCOM::initialize_stream ( int  iter = 3)

Setup up stream by reading the values a couple times first.

Parameters
iterNumber of times to read for warming up.

Definition at line 45 of file serial_com.cpp.

46{
47 // Read first (iter) lines to start
48 for (size_t i = 0; i < iter; i++)
49 {
50 std::string incoming_str = this->readLine();
51 }
52}
std::string readLine(void)
Blocks until a line is received from the serial device.
Definition: serial_com.cpp:19
Here is the call graph for this function:

◆ readLine()

std::string SerialCOM::readLine ( void  )

Blocks until a line is received from the serial device.

Blocks until a line is received from the serial device. Eventual '
' or '\r
' characters at the end of the string are removed.

Returns
a string containing the received line.
Exceptions
boost::system::system_erroron failure.

Definition at line 19 of file serial_com.cpp.

20{
21 //Reading data char by char, code is optimized for simplicity, not speed
22 using namespace boost;
23 char c;
24 std::string result;
25 for(;;)
26 {
27 asio::read(serial,asio::buffer(&c,1));
28 switch(c)
29 {
30 case '\r':
31 break;
32 case '\n':
33 return result;
34 default:
35 result+=c;
36 }
37 }
38}
Here is the caller graph for this function:

◆ writeString()

void SerialCOM::writeString ( std::string  s)

Write a string to the serial device.

Write a string to the serial device.

Parameters
sstring to write.
Exceptions
boost::system::system_erroron failure.

Definition at line 8 of file serial_com.cpp.

9{
10 boost::asio::write(serial,boost::asio::buffer(s.c_str(),s.size()));
11}

Member Data Documentation

◆ io

boost::asio::io_service SerialCOM::io
private

Boost io service.

Definition at line 40 of file serial_com.h.

◆ serial

boost::asio::serial_port SerialCOM::serial
private

Boost serial port handle.

Definition at line 43 of file serial_com.h.


The documentation for this class was generated from the following files: