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,
64 goby::middleware::protobuf::SerialConfig, boost::asio::serial_port,
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())
82 case protobuf::SerialCommand::SEND_BREAK:
88 case protobuf::SerialCommand::RTS_HIGH:
92 int RTS_flag = TIOCM_RTS;
94 ioctl(fd, TIOCMBIS, &RTS_flag);
98 case protobuf::SerialCommand::RTS_LOW:
102 int RTS_flag = TIOCM_RTS;
104 ioctl(fd, TIOCMBIC, &RTS_flag);
107 case protobuf::SerialCommand::DTR_HIGH:
111 int DTR_flag = TIOCM_DTR;
112 ioctl(fd, TIOCMBIS, &DTR_flag);
116 case protobuf::SerialCommand::DTR_LOW:
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())
195 case goby::middleware::protobuf::SerialConfig::NONE:
196 this->mutable_serial_port().set_option(
199 case goby::middleware::protobuf::SerialConfig::SOFTWARE:
200 this->mutable_serial_port().set_option(
201 serial_port_base::flow_control(serial_port_base::flow_control::software));
203 case goby::middleware::protobuf::SerialConfig::HARDWARE:
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));
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
const std::string & glog_group()
boost::asio::serial_port & mutable_socket()
Access the (mutable) socket (or serial_port) object.
bool socket_is_open()
Does the socket exist and is it open?
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.
goby::util::logger::GroupSetter group(std::string n)
detail namespace with internal helper functions
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::acomms::abc::protobuf::Config >, 11, false > config
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.