gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
ip_assigner.cpp
Go to the documentation of this file.
1 
6 #include "examples.h"
7 
8 #include <iostream> //std::cout
9 #include <vector>
10 #include <string.h>
11 using namespace std;
12 typedef std::vector<string> tokens;
13 
14 tokens string_split(const string& str, const string& token);
15 
22 GReturn ip_assigner(char* serial_num, int address)
23 {
24  char buf[G_SMALL_BUFFER]; //traffic buffer
25  bool controller_found = false;
26 
27  do //Loop while no requests are found.
28  {
29  cout << "Searching...\n";
30 
31  //listen for ~5 seconds for controllers requesting IP addresses.
32  e(GIpRequests(buf, G_SMALL_BUFFER));
33  cout << buf << "\n";
34  } while (strlen(buf) <= 1);
35 
36  //Get a list of all found controllers
37  tokens controllers = string_split(buf, "\n");
38  //Iterate over all found controllers
39  for (tokens::iterator it = controllers.begin(); it != controllers.end(); it++)
40  {
41  const string& controller = *it;
42  //Get a list of the parameters of the controller
43  tokens controller_params = string_split(controller, ", ");
44  //Parameters are ordered as:
45  //[Model #], [Serial #], [MAC Address], [Connection Name], [IP Address]
46  if (controller_params.size() < 5)
47  {
48  cerr << "Unexpected controller format";
49  return GALIL_EXAMPLE_ERROR;
50  }
51  const char* mac = controller_params[2].c_str();
52  const char* ip = controller_params[4].c_str();
53  //If controller contains the user entered serial number
54  if (string(serial_num) == controller_params[1])
55  {
56  controller_found = true;
57  int ip1 = 0, ip2 = 0, ip3 = 0, ip4 = 0;
58  //Parses the IP Address and breaks it into chunks in order to change the last byte
59  sscanf(ip, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
60 
61  char ipaddress[G_SMALL_BUFFER];
62  //Store the first 3 bytes of the original IP Address with the user supplied address byte
63  sprintf(ipaddress, "%d.%d.%d.%d", ip1, ip2, ip3, address);
64 
65  //Assign the controller using the newly created IP Address and MAC Address
66  e(GAssign(ipaddress, mac));
67 
68  char info[G_SMALL_BUFFER];
69  GCon g = 0;
70  e(GOpen(ipaddress, &g)); //Opens a connection to the controller
71  if (g != 0)
72  {
73  //Burns newly assigned IP Address to non-volatile EEPROM memory
74  e(GCmd(g, "BN"));
75 
76  e(GInfo(g, info, G_SMALL_BUFFER));
77  cout << info;
78  }
79  break;
80  }
81  }
82 
83  if (!controller_found)
84  {
85  cout << "No controller matched the entered serial number";
86  }
87 
88  return GALIL_EXAMPLE_OK;
89 }
90 
92 tokens string_split(const string& str, const string& token)
93 {
94  tokens split; // A list that will contain strings separated by the given token
95  int prev_position = 0 - token.length(); // Treat first position as if it came after a token
96  int position = str.find(token); // Find position of first token
97 
98  while (position != string::npos) // Find new position of token in string
99  {
100  // Add characters between previous token and next token to the list
101  split.push_back(str.substr(prev_position + token.length(),
102  position - prev_position - token.length()));
103  prev_position = position;
104  position = str.find(token, prev_position + token.length());
105  }
106 
107  // If there are any remaining characters
108  if (str.length() > prev_position + token.length())
109  {
110  // Add remaining characters to the list
111  split.push_back(str.substr(prev_position + token.length(),
112  str.length() - prev_position - token.length()));
113  }
114 
115  return split;
116 }
tokens string_split(const string &str, const string &token)
Splits a string into a vector based on a token.
Definition: ip_assigner.cpp:92
GReturn ip_assigner(char *serial_num, int address)
Assigns controller an IP Adress given a serial number and a 1 byte address.
Definition: ip_assigner.cpp:22
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:22