Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/async_comm/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

namespace async_comm
{
using serial_flow_control = boost::asio::serial_port_base::flow_control::type;

/**
* @class Serial
Expand All @@ -60,10 +61,15 @@ class Serial : public Comm
* @brief Open a serial port
* @param port The port to open (e.g. "/dev/ttyUSB0")
* @param baud_rate The baud rate for the serial port (e.g. 115200)
* @param flow_control The serial port flow control (none, software or hardware)
* this is optional and defaults to none.
* @param message_handler Custom message handler, or omit for default handler
*
*/
Serial(std::string port, unsigned int baud_rate, MessageHandler& message_handler = default_message_handler_);
Serial(std::string port, unsigned int baud_rate,
serial_flow_control flow_control,
MessageHandler& message_handler = default_message_handler_);
~Serial();


Expand All @@ -74,6 +80,17 @@ class Serial : public Comm
*/
bool set_baud_rate(unsigned int baud_rate);

/**
* @brief Set the serial port flow control
*
* @param flow_control the flow control option to configure
* serial_flow_control::none: No Flow Control
* serial_flow_control::software: Software Flow Control
* serial_flow_control::hardware: Hardware Flow Control
* @return True if successful
*/
bool set_flow_control (serial_flow_control flow_control);

private:
bool is_open() override;
bool do_init() override;
Expand All @@ -87,6 +104,8 @@ class Serial : public Comm
unsigned int baud_rate_;

boost::asio::serial_port serial_port_;

serial_flow_control flow_control_;
};

} // namespace async_comm
Expand Down
30 changes: 29 additions & 1 deletion src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ Serial::Serial(std::string port, unsigned int baud_rate, MessageHandler& message
Comm(message_handler),
port_(port),
baud_rate_(baud_rate),
flow_control_(serial_flow_control::none),
serial_port_(io_service_)
{
}

Serial::Serial(std::string port, unsigned int baud_rate,
serial_flow_control flow_control,
MessageHandler& message_handler) :
Comm(message_handler),
port_(port),
baud_rate_(baud_rate),
flow_control_(flow_control),
serial_port_(io_service_)
{
}
Expand All @@ -73,6 +85,22 @@ bool Serial::set_baud_rate(unsigned int baud_rate)
return true;
}

bool Serial::set_flow_control (serial_flow_control flow_control)
{
flow_control_ = flow_control;
try
{
serial_port_.set_option(serial_port_base::flow_control(flow_control_));
}
catch (boost::system::system_error e)
{
message_handler_.error(e.what());
return false;
}

return true;
}

bool Serial::is_open()
{
return serial_port_.is_open();
Expand All @@ -87,7 +115,7 @@ bool Serial::do_init()
serial_port_.set_option(serial_port_base::character_size(8));
serial_port_.set_option(serial_port_base::parity(serial_port_base::parity::none));
serial_port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one));
serial_port_.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none));
serial_port_.set_option(serial_port_base::flow_control(flow_control_));
}
catch (boost::system::system_error e)
{
Expand Down