gclib  1.36.3
Communications API for Galil controllers and PLCs
ip_assigner.cs
Go to the documentation of this file.
1 
10 using System;
11 using System.Linq;
12 
13 namespace examples
14 {
15  public static partial class Examples
16  {
35 
36  public static int IP_Assigner(gclib gclib, string serial_num, byte address)
37  {
38  bool controller_found = false;
39  string[] requests;
40  do //Loop while no requests are found.
41  {
42  Console.WriteLine("Searching...");
43 
44  //Listen for ~5 secods for controllers requesting IP addresses.
45  requests = gclib.GIpRequests();
46 
47  foreach (string request in requests)
48  {
49  Console.WriteLine(request);
50  }
51 
52  } while (requests.Count() < 1);
53 
54  foreach (string request in requests)
55  {
56  string[] controller_params = request.Split(new string[] { ", " }, StringSplitOptions.None);
57 
58  //Parameters are ordered as:
59  //[Model #], [Serial #], [MAC Address], [Connection Name], [IP Address]
60 
61  if (controller_params.Count() < 5)
62  {
63  Console.WriteLine("Unexpected controller format");
64  return GALIL_EXAMPLE_ERROR;
65  }
66 
67  string mac = controller_params[2];
68  string ip = controller_params[4];
69 
70  //If controller contains the user entered serial number
71  if (serial_num == controller_params[1])
72  {
73  Console.WriteLine("Controller Match Found");
74  controller_found = true;
75 
76  //Splits the found ip address into individual bytes
77  string[] ip_bytes = ip.Split('.');
78 
79  //Rebuild the ip address using the user provided address as the last byte
80  string new_ip = $"{ip_bytes[0]}.{ip_bytes[1]}.{ip_bytes[2]}.{address}";
81 
82  //Assign the new ip address to the controller
83  gclib.GAssign(new_ip, mac);
84 
85  //Open a connection at the new ip address
86  gclib.GOpen(new_ip);
87 
88  //Burns the newly assigned ip address to non-volatile EEPROM memory
89  gclib.GCommand("BN");
90 
91  Console.WriteLine("IP Address assigned");
92 
93  //Write the connection string to the console
94  Console.WriteLine(gclib.GInfo());
95 
96  break;
97  }
98  }
99 
100  if (!controller_found)
101  Console.Write("No controller matched the entered serial number");
102 
103  return GALIL_EXAMPLE_OK;
104  }
106  }
107 }
const int GALIL_EXAMPLE_OK
Examples success code.
Definition: examples.cs:29
const int GALIL_EXAMPLE_ERROR
Examples error code.
Definition: examples.cs:30
static int IP_Assigner(gclib gclib, string serial_num, byte address)
Assigns controller an IP Adress given a serial number and a 1 byte address.
Definition: ip_assigner.cs:36