gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
ip_assigner_example.cpp
Go to the documentation of this file.
1 
18 #include "examples.h"
19 
20 #include <iostream> //std::cout
21 using namespace std;
22 
24 
28 int main(int argc, char * argv[])
29 {
30  GReturn rc = GALIL_EXAMPLE_OK;
31  char buf[G_SMALL_BUFFER];
32 
33  try
34  {
35  if (argc != 3) //Invalid number of arguments
36  {
37  cerr << "Incorrect number of arguments provided\n";
38  cerr << "Usage: ipassigner_example.exe <SERIAL #> <1 Byte Address>\n";
39  return GALIL_EXAMPLE_ERROR;
40  }
41  else
42  {
43  char* serial_num = argv[1];
44 
45  //Retrieve address from command line and convert to int
46  char* end;
47  int address = strtol(argv[2], &end, 10);
48  if (*end != '\0' || address < 0 || address > 255) //If invalid address
49  {
50  cerr << "Please enter a number between 0 and 255 for the address."
51  " This will be used as the last number in the IP Address\n";
52  "Usage: ipassigner_example.exe <SERIAL #> <1 Byte Address>\n";
53  return GALIL_EXAMPLE_ERROR;
54  }
55  //Assigns controller an IP Adress given a serial number and a 1 byte address
56  rc = ip_assigner(serial_num, address);
57  }
58  }
59  catch (GReturn gr)
60  {
61  GError(gr, buf, G_SMALL_BUFFER); //Get Error Information
62  cout << buf << '\n'; //further context
63  return GALIL_EXAMPLE_ERROR;
64  }
65 
66 
67  return rc;
68 }
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
int main(int argc, char *argv[])
Main function for IP Assigner Example.