28 _fd = open(
_port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
30 int retry_counter = 5;
34 std::string msg =
"Error opening connection at " +
_port +
": " + strerror(errno);
42 _fd = open(
_port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
46 throw std::runtime_error(msg);
49 ROS_DEBUG(
"Toposens serial established with fd %d\n",
_fd);
53 memset(&tty, 0,
sizeof(tty));
56 if (tcgetattr(
_fd, &tty) != 0)
58 ROS_WARN(
"Error retrieving attributes at %s: %s",
_port.c_str(), strerror(errno));
63 cfsetispeed(&tty,
kBaud);
64 cfsetospeed(&tty,
kBaud);
67 tty.c_cflag |= CREAD | CLOCAL | CS8;
69 tty.c_cflag &= ~(PARENB | CSTOPB | CRTSCTS);
72 tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
75 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
77 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK);
79 tty.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL);
82 tty.c_oflag &= ~(OPOST | ONLCR);
91 if (tcsetattr(
_fd, TCSANOW, &tty) != 0)
93 ROS_WARN(
"Error configuring device at %s: %s",
_port.c_str(), strerror(errno));
98 "Serial settings updated:\n BaudRate = %d \n DataBits = 8 \n " 101 tcflush(
_fd, TCIFLUSH);
102 ROS_INFO(
"Device at %s ready for communication",
_port.c_str());
110 ROS_INFO(
"Closing serial connection...");
111 tcflush(
_fd, TCIOFLUSH);
113 if (close(
_fd) == -1)
115 ROS_ERROR(
"Error closing serial connection: %s", strerror(errno));
119 ROS_INFO(
"Serial connection killed");
135 memset(&buffer,
'\0',
sizeof(buffer));
136 nBytes = read(
_fd, &buffer,
sizeof(buffer));
149 if (buffer[nBytes - 1] ==
'E')
break;
166 std::string msg =
"Connection at " +
_port +
" unavailable: " + strerror(errno);
167 throw std::runtime_error(msg);
170 int tx_length = write(
_fd, bytes, strlen(bytes));
174 ROS_DEBUG(
"Bytes transmitted: %s", bytes);
179 ROS_ERROR(
"Failed to transmit %s: %s", bytes, strerror(errno));
182 catch (
const std::exception &e)
192 size_t frame_start = frame.find(
'S');
197 return (frame[frame_start + 7] ==
'C');
206 size_t frame_start = -1;
212 buffer.str(std::string());
bool isAcknowledgementFrame(std::string frame)
#define ROS_WARN_STREAM(args)
#define ROS_INFO_STREAM(args)
Generates firmware-compatible commands for tuning performance parameters.
bool waitForAcknowledgement(std::stringstream &buffer)
void sendCmd(Command cmd, std::stringstream &data)
void getFrame(std::stringstream &data)