tx.cpp
Go to the documentation of this file.
1 
26 #include "nmea_comms/rx.h"
27 #include "nmea_comms/checksum.h"
28 
29 #include <stdio.h>
30 #include <poll.h>
31 #include <boost/algorithm/string.hpp>
32 
33 #include "ros/ros.h"
34 #include "nmea_msgs/Sentence.h"
35 
36 
37 void tx_msg_callback(const nmea_msgs::SentenceConstPtr sentence_msg_ptr, int fd)
38 {
39  static int consecutive_errors = 0;
40 
41  char buffer[256];
42  int buffer_length = snprintf(buffer, 256, "%s\r\n", sentence_msg_ptr->sentence.c_str());
43 
44  // No guarantee that write() will write everything, so we use poll() to block
45  // on the availability of the fd for writing until the whole message has been
46  // written out.
47  const char* buffer_write = buffer;
48  struct pollfd pollfds[] = { { fd, POLLOUT, 0 } };
49  while (ros::ok())
50  {
51  int retval = poll(pollfds, 1, 1000);
52 
53  if (pollfds[0].revents & POLLHUP)
54  {
55  ROS_INFO("Device hangup occurred on attempted write.");
56  return;
57  //ros::shutdown();
58  }
59 
60  if (pollfds[0].revents & POLLERR)
61  {
62  ROS_FATAL("Killing node due to device error.");
63  ros::shutdown();
64  }
65 
66  retval = write(fd, buffer_write, buffer_length - (buffer_write - buffer));
67  if (retval > 0)
68  {
69  buffer_write += retval;
70  }
71  else
72  {
73  ROS_WARN("Device write error; abandoning message (%s).",
74  sentence_msg_ptr->sentence.c_str());
75  if (++consecutive_errors >= 10)
76  {
77  ROS_FATAL("Killing node due to %d consecutive write errors.", consecutive_errors);
78  ros::shutdown();
79  }
80  break;
81  }
82  if (buffer_write - buffer >= buffer_length)
83  {
84  consecutive_errors = 0;
85  break;
86  }
87  }
88 }
#define ROS_FATAL(...)
void tx_msg_callback(const nmea_msgs::SentenceConstPtr sentence_msg_ptr, int fd)
Definition: tx.cpp:37
#define ROS_WARN(...)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
ROSCPP_DECL void shutdown()


nmea_comms
Author(s): Mike Purvis
autogenerated on Mon Aug 5 2019 03:53:18