DisplayIpAddress.cpp
Go to the documentation of this file.
1 // Serial port interface program to write the IP-Address on the youBot display
4 
5 #include <stdio.h> // standard input / output functions
6 #include <string.h> // string function definitions
7 #include <unistd.h> // UNIX standard function definitions
8 #include <fcntl.h> // File control definitions
9 #include <errno.h> // Error number definitions
10 #include <termios.h> // POSIX terminal control definitionss
11 #include <time.h> // time calls
12 #include <iostream>
13 #include <string>
14 #include <sys/types.h>
15 #include <ifaddrs.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <cstdlib>
19 
21  line2 = 0x02,
22  line3 = 0x03
23 };
24 
26  battery1 = 0x04,
27  battery2 = 0x05,
28  powersupply = 0x0c
29 };
30 
31 int open_port(std::string port) {
32  int fd; // file description for the serial port
33 
34  fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
35 
36  if (fd == -1) // if open is unsucessful
37  {
38  throw ("unable to open port: " + port);
39  } else {
40  fcntl(fd, F_SETFL, 0);
41  printf("port is open.\n");
42  }
43 
44  return (fd);
45 }
46 
47 int configure_port(int fd) // configure the port
48 {
49  struct termios port_settings; // structure to store the port settings in
50 
51  tcgetattr(fd, &port_settings);
52 
53  cfsetispeed(&port_settings, B0); // set baud rates
54  cfsetospeed(&port_settings, B0);
55 
56 
57  port_settings.c_cflag |= (CLOCAL | CREAD); //Enable the receiver and set local mode...
58 
59  //port_settings.c_cflag |= CRTSCTS; //Enable Hardware Flow Control
60  port_settings.c_cflag &= ~CRTSCTS; //Disable Hardware Flow Control
61 
62  port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
63  port_settings.c_cflag &= ~CSTOPB;
64  port_settings.c_cflag &= ~CSIZE;
65  port_settings.c_cflag |= CS8;
66 
67  port_settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //Choosing Raw Input
68  port_settings.c_iflag &= ~(IXON | IXOFF | IXANY); //disable software flow control
69  port_settings.c_oflag &= ~OPOST; //Choosing Raw Output
70  port_settings.c_cc[VMIN] = 0;
71  port_settings.c_cc[VTIME] = 10; /* set raw input, 1 second timeout */
72 
73  tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
74  return (fd);
75 
76 }
77 
78 bool setText(int fd, displayline line, std::string text) {
79  const int size = 18;
80  if (text.size() > size - 2) {
81  printf("The text is to long!\n");
82  return false;
83  }
84 
85  unsigned char send_bytes[size];
86 
87  send_bytes[0] = line;
88  for (unsigned int i = 0; i < size - 2; i++) {
89  if (i < text.size())
90  send_bytes[i + 1] = text[i];
91  else
92  send_bytes[i + 1] = ' ';
93  }
94  send_bytes[size - 1] = 0x0D; //trailing \CR
95 
96  write(fd, send_bytes, size); //Send data
97  printf("[");
98  for (int i = 1; i < size - 1; i++) {
99  printf("%c", send_bytes[i]);
100  }
101  printf("]\n");
102 
103  return true;
104 }
105 
106 //return the voltage in [Volt]
107 double getVoltage(int fd, voltagesource source) {
108  unsigned char send_bytes[1];
109  send_bytes[0] = source;
110 
111  write(fd, send_bytes, 1); //Send data
112 
113  const int readsize = 20;
114  char read_bytes[readsize] = {0};
115 
116  int nobytesread = 0;
117  nobytesread = read(fd, read_bytes, readsize);
118  read_bytes[nobytesread-1] = 0; //delete the last tow character \CR+\LF
119  read_bytes[nobytesread-2] = 0;
120 
121  std::string value(read_bytes);
122 
123  return (double)atoi(value.c_str())/1000;
124 }
125 
126 void getIPAdress(std::string lanname, std::string wlanname, std::string& lanip, std::string& wlanip) {
127  struct ifaddrs * ifAddrStruct = NULL;
128  struct ifaddrs * ifa = NULL;
129  void * tmpAddrPtr = NULL;
130  lanip = "L";
131  wlanip = "W";
132  getifaddrs(&ifAddrStruct);
133 
134  for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
135  if (ifa ->ifa_addr->sa_family == AF_INET) { // check it is IP4
136  // is a valid IP4 Address
137  tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
138  char addressBuffer[INET_ADDRSTRLEN];
139  inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
140  // printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
141  std::string interface(ifa->ifa_name);
142  if (interface == lanname)
143  lanip.append(addressBuffer);
144  if (interface == wlanname)
145  wlanip.append(addressBuffer);
146  }
147  }
148  if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
149 
150  return;
151 }
152 
153 int main(int argc, char* argv[]) {
154  try {
155  if (argc != 4) {
156  std::cout << "invalid arguments \n ./displayipaddress 'serial port' 'ethernet' 'wlan' \n./displayipaddress /dev/ttyACM0 eth1 wlan0" << std::endl;
157  return 0;
158  }
159  int fd = open_port(argv[1]);
160  configure_port(fd);
161 
162  std::string lanip;
163  std::string wlanip;
164 
165  while (true) {
166  sleep(2);
167  getIPAdress(argv[2], argv[3], lanip, wlanip);
168  setText(fd, line2, lanip);
169  setText(fd, line3, wlanip);
170  // std::cout << "Bat1: " << getVoltage(fd, battery1) << std::endl;
171  // std::cout << "Bat2: " << getVoltage(fd, battery2) << std::endl;
172  // std::cout << "powersupply: " << getVoltage(fd, powersupply) << std::endl;
173  }
174 
175  close(fd);
176  } catch (std::string &e) {
177  std::cout << "Error: " << e << std::endl;
178  }
179  return (0);
180 }
181 
int main(int argc, char *argv[])
void getIPAdress(std::string lanname, std::string wlanname, std::string &lanip, std::string &wlanip)
displayline
bool setText(int fd, displayline line, std::string text)
double getVoltage(int fd, voltagesource source)
int configure_port(int fd)
voltagesource
int open_port(std::string port)


youbot_driver
Author(s): Jan Paulus
autogenerated on Mon Jun 10 2019 15:46:24