24 #ifndef GOBY_MIDDLEWARE_IO_DETAIL_SERIAL_INTERFACE_H
25 #define GOBY_MIDDLEWARE_IO_DETAIL_SERIAL_INTERFACE_H
30 #include <sys/ioctl.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>;
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(
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(
213 this->mutable_serial_port().set_option(
214 serial_port_base::stop_bits(serial_port_base::stop_bits::one));