24#ifndef GOBY_MIDDLEWARE_IO_DETAIL_SERIAL_INTERFACE_H
25#define GOBY_MIDDLEWARE_IO_DETAIL_SERIAL_INTERFACE_H
32#include <boost/asio/serial_port.hpp>
57 PubSubLayer subscribe_layer,
template <
class>
class ThreadType,
58 bool use_indexed_groups =
false>
60 goby::middleware::protobuf::SerialConfig,
61 boost::asio::serial_port, ThreadType, use_indexed_groups>
63 using Base =
IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
65 ThreadType, use_indexed_groups>;
72 :
Base(config, index,
std::string(
"serial: ") + config.port())
74 auto command_out_callback =
75 [
this](std::shared_ptr<const goby::middleware::protobuf::SerialCommand> cmd) {
77 << cmd->ShortDebugString() << std::endl;
78 if (!cmd->has_index() || cmd->index() == this->index())
80 switch (cmd->command())
92 int RTS_flag = TIOCM_RTS;
94 ioctl(fd, TIOCMBIS, &RTS_flag);
102 int RTS_flag = TIOCM_RTS;
104 ioctl(fd, TIOCMBIC, &RTS_flag);
111 int DTR_flag = TIOCM_DTR;
112 ioctl(fd, TIOCMBIS, &DTR_flag);
120 int DTR_flag = TIOCM_DTR;
121 ioctl(fd, TIOCMBIC, &DTR_flag);
129 this->
template subscribe_out<goby::middleware::protobuf::SerialCommand>(
130 command_out_callback);
133 this->interthread().template publish<line_in_group>(ready);
138 this->
template unsubscribe_out<goby::middleware::protobuf::SerialCommand>();
146 void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg)
override
151 void publish_status()
153 auto status_msg = std::make_shared<protobuf::SerialStatus>();
154 if (this->index() != -1)
155 status_msg->set_index(this->index());
162 ioctl(fd, TIOCMGET, &status);
163 status_msg->set_rts(status & TIOCM_RTS);
164 status_msg->set_dtr(status & TIOCM_DTR);
168 << status_msg->ShortDebugString() << std::endl;
170 this->publish_in(status_msg);
173 void open_socket()
override;
184 bool use_indexed_groups>
186 subscribe_layer, ThreadType,
187 use_indexed_groups>::open_socket()
189 this->mutable_serial_port().open(this->cfg().port());
190 using boost::asio::serial_port_base;
191 this->mutable_serial_port().set_option(serial_port_base::baud_rate(this->cfg().baud()));
193 switch (this->cfg().flow_control())
196 this->mutable_serial_port().set_option(
197 serial_port_base::flow_control(serial_port_base::flow_control::none));
200 this->mutable_serial_port().set_option(
201 serial_port_base::flow_control(serial_port_base::flow_control::software));
204 this->mutable_serial_port().set_option(
205 serial_port_base::flow_control(serial_port_base::flow_control::hardware));
210 this->mutable_serial_port().set_option(serial_port_base::character_size(8));
211 this->mutable_serial_port().set_option(
212 serial_port_base::parity(serial_port_base::parity::none));
213 this->mutable_serial_port().set_option(
214 serial_port_base::stop_bits(serial_port_base::stop_bits::one));
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
bool socket_is_open()
Does the socket exist and is it open?
const std::string & glog_group()
SocketType & mutable_socket()
Access the (mutable) socket (or serial_port) object.
friend void basic_async_write(IOThreadImplementation *this_thread, std::shared_ptr< const goby::middleware::protobuf::IOData > io_msg)
SerialThread(const goby::middleware::protobuf::SerialConfig &config, int index=-1)
Constructs the thread.
virtual ~SerialThread() override
boost::asio::serial_port & mutable_serial_port()
Access the (mutable) serial_port object.
static constexpr Command DTR_LOW
static constexpr Command DTR_HIGH
static constexpr Command RTS_HIGH
static constexpr Command RTS_LOW
static constexpr Command SEND_BREAK
static constexpr FlowControl HARDWARE
static constexpr FlowControl SOFTWARE
static constexpr FlowControl NONE
goby::util::logger::GroupSetter group(std::string n)
detail namespace with internal helper functions
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.