serial_com.cpp
Go to the documentation of this file.
1 
4 
5 
6 void SerialCom::connect(std::string port, int baudrate) {
7  baudrate_ = baudrate;
8  file_handle_ = ::open(port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
9  if (file_handle_ < 0)
10  throw ric_interface::ConnectionExeption("[ric_interface]: Failed to open RIC board port");
11 
12  /* sleep for 10ms and then clear serial buffer */
13  usleep(10000);
14  tcflush(file_handle_,TCIOFLUSH);
15 
16  /* Check if the file descriptor is pointing to a TTY device or not */
17  if (!isatty(file_handle_))
18  throw ric_interface::ConnectionExeption("[ric_interface]: RIC Board port is not a tty device");
19 
20  setAttributes();
21 }
22 
24  struct termios tty;
25 
26  if (tcgetattr(file_handle_, &tty) < 0)
27  throw ric_interface::ConnectionExeption("[ric_interface]: Failed to get RIC board port attributes");
28 
29  cfsetospeed(&tty, (speed_t) baudrate_);
30  cfsetispeed(&tty, (speed_t) baudrate_);
31  tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
32  tty.c_cflag &= ~CSIZE;
33  tty.c_cflag |= CS8; /* 8-bit characters */
34  tty.c_cflag &= ~PARENB; /* no parity bit */
35  tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
36  tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
37 
38  /* setup for non-canonical mode */
39  tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
40  tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
41  tty.c_oflag &= ~OPOST;
42 
43  /* fetch bytes as they become available */
44  tty.c_cc[VMIN] = 1;
45  tty.c_cc[VTIME] = 1;
46  if (tcsetattr(file_handle_, TCSANOW, &tty) != 0)
47  throw ric_interface::ConnectionExeption("[ric_interface]: Failed to set RIC board port attributes");
48 
49  tcflush(file_handle_, TCIOFLUSH);
50 }
51 
52 int SerialCom::read(byte buff[], size_t size) {
53 
54  const int max_bad_reads = 200;
55  int bad_reads = 0;
56  int indx;
57  for (indx=0; indx<size && bad_reads<max_bad_reads;)
58  {
59  byte incoming_byte;
60  int i = ::read(file_handle_, &incoming_byte, 1);
61  if (i == 1)
62  {
63  //printf("new: %i\n", incoming_byte);
64  buff[indx++] = incoming_byte;
65  }
66  else
67  {
68  bad_reads++;
69  }
70  }
71  if (bad_reads == max_bad_reads)
72  return -1;
73  return indx;
74 }
75 
77 {
78  byte incoming_byte;
79  int i = ::read(file_handle_, &incoming_byte, 1);
80  if (i == 1)
81  return incoming_byte;
82  return -1;
83 }
84 
85 bool SerialCom::send(const byte buff[], size_t size) {
86  char i = buff[0];
87  int write_len = ::write(file_handle_, buff, size);
88  if (write_len != size)
89  return false;
90  return true;
91 }
int file_handle_
Definition: serial_com.h:22
void setAttributes()
Definition: serial_com.cpp:23
int read()
Definition: serial_com.cpp:76
int baudrate_
Definition: serial_com.h:22
bool send(const byte buff[], size_t size)
Definition: serial_com.cpp:85
uint8_t byte
Definition: crc8.h:11
void connect(std::string port, int baudrate)
Definition: serial_com.cpp:6


ric_interface
Author(s):
autogenerated on Wed Jan 3 2018 03:48:20