| gclib 1.34.15 Communications API for Galil controllers and PLCs |

ip_assigner.cpp
Go to the documentation of this file.
1
10 #include "examples.h"
11
12 #include <iostream> //std::cout
13 #include <vector>
14 #include <string.h>
15 using namespace std;
16 typedef std::vector<string> tokens;
17
18 tokens string_split(const string& str, const string& token);
19
26 GReturn ip_assigner(char* serial_num, int address)
27 {
28 char buf[G_SMALL_BUFFER]; //traffic buffer
29 bool controller_found = false;
30
31 do //Loop while no requests are found.
32 {
33 cout << "Searching...\n";
34
35 //listen for ~5 seconds for controllers requesting IP addresses.
36 e(GIpRequests(buf, G_SMALL_BUFFER));
37 cout << buf << "\n";
38 } while (strlen(buf) <= 1);
39
40 //Get a list of all found controllers
41 tokens controllers = string_split(buf, "\n");
42 //Iterate over all found controllers
43 for (tokens::iterator it = controllers.begin(); it != controllers.end(); it++)
44 {
45 const string& controller = *it;
46 //Get a list of the parameters of the controller
47 tokens controller_params = string_split(controller, ", ");
48 //Parameters are ordered as:
49 //[Model #], [Serial #], [MAC Address], [Connection Name], [IP Address]
50 if (controller_params.size() < 5)
51 {
52 cerr << "Unexpected controller format";
53 return GALIL_EXAMPLE_ERROR;
54 }
55 const char* mac = controller_params[2].c_str();
56 const char* ip = controller_params[4].c_str();
57 //If controller contains the user entered serial number
58 if (string(serial_num) == controller_params[1])
59 {
60 controller_found = true;
61 int ip1 = 0, ip2 = 0, ip3 = 0, ip4 = 0;
62 //Parses the IP Address and breaks it into chunks in order to change the last byte
63 sscanf(ip, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
64
65 char ipaddress[G_SMALL_BUFFER];
66 //Store the first 3 bytes of the original IP Address with the user supplied address byte
67 sprintf(ipaddress, "%d.%d.%d.%d", ip1, ip2, ip3, address);
68
69 //Assign the controller using the newly created IP Address and MAC Address
70 e(GAssign(ipaddress, mac));
71
72 char info[G_SMALL_BUFFER];
73 GCon g = 0;
74 e(GOpen(ipaddress, &g)); //Opens a connection to the controller
75 if (g != 0)
76 {
77 //Burns newly assigned IP Address to non-volatile EEPROM memory
78 e(GCmd(g, "BN"));
79
80 e(GInfo(g, info, G_SMALL_BUFFER));
81 cout << info;
82 }
83 break;
84 }
85 }
86
87 if (!controller_found)
88 {
89 cout << "No controller matched the entered serial number";
90 }
91
92 return GALIL_EXAMPLE_OK;
93 }
94
96 tokens string_split(const string& str, const string& token)
97 {
98 tokens split; // A list that will contain strings separated by the given token
99 int prev_position = 0 - token.length(); // Treat first position as if it came after a token
100 int position = str.find(token); // Find position of first token
101
102 while (position != string::npos) // Find new position of token in string
103 {
104 // Add characters between previous token and next token to the list
105 split.push_back(str.substr(prev_position + token.length(),
106 position - prev_position - token.length()));
107 prev_position = position;
108 position = str.find(token, prev_position + token.length());
109 }
110
111 // If there are any remaining characters
112 if (str.length() > prev_position + token.length())
113 {
114 // Add remaining characters to the list
115 split.push_back(str.substr(prev_position + token.length(),
116 str.length() - prev_position - token.length()));
117 }
118
119 return split;
120 }
examples.h
GAssign
GCLIB_DLL_EXPORTED GReturn GCALL GAssign(GCStringIn ip, GCStringIn mac)
Uses GUtility(), G_UTIL_GCAPS_ASSIGN or G_UTIL_ASSIGN to assign an IP address over the Ethernet to a ...
Definition: gclibo.c:70
GIpRequests
GCLIB_DLL_EXPORTED GReturn GCALL GIpRequests(GCStringOut requests, GSize requests_len)
Uses GUtility(), G_UTIL_GCAPS_IPREQUEST or G_UTIL_IPREQUEST to provide a list of all Galil controller...
Definition: gclibo.c:106
GInfo
GCLIB_DLL_EXPORTED GReturn GCALL GInfo(GCon g, GCStringOut info, GSize info_len)
Uses GUtility() and G_UTIL_INFO to provide a useful connection string.
Definition: gclibo.c:49
GCmd
GCLIB_DLL_EXPORTED GReturn GCALL GCmd(GCon g, GCStringIn command)
Wrapper around GCommand for use when the return value is not desired.
Definition: gclibo.c:237
GOpen
GCLIB_DLL_EXPORTED GReturn GCALL GOpen(GCStringIn address, GCon *g)
Open a connection to a Galil Controller.
GReturn
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
G_SMALL_BUFFER
#define G_SMALL_BUFFER
Most reads from Galil are small. This value will easily hold most, e.g. TH, TZ, etc.
Definition: gclib.h:89
GCon
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
e
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
ip_assigner
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:26
string_split
tokens string_split(const string &str, const string &token)
Splits a string into a vector based on a token.
Definition: ip_assigner.cpp:96