gclib  2.0.8
Communications API for Galil controllers and PLCs
x_examples.cpp
Go to the documentation of this file.
1 
6 #include "x_examples.h"
7 
8 #include <iomanip>
9 
10 int main(int argc, char * argv[])
11 {
12  int rc = GALIL_EXAMPLE_OK; //return code
13  int buf_size = G_SMALL_BUFFER;
14  char buf[G_SMALL_BUFFER]; //traffic buffer
15 
16  GCon g = 0; //var used to refer to a unique connection. A valid connection is nonzero.
17  try
18  {
19 
20  x_e(GVersion(buf, sizeof(buf))); //library version
21  cout << "Library versions: " << buf << "\n";
22 
23 #if 0
24  /*
25  * Change above line to "#if 1" to run examples below.
26  * Listening to requests for IP addresses.
27  */
28  x_e(GIpRequests(buf, sizeof(buf))); //listen for ~5 seconds for controllers requesting IP addresses
29  cout << "Controllers without IP Address:\n";
30  if (strlen(buf) != 0)
31  cout << buf << "\n\n";
32  else
33  cout << "none\n\n";
34 
35 #endif
36 
37 #if 0
38  /*
39  * Change above line to "#if 1" to run examples below.
40  * Assign IP addresses.
41  */
42  x_e(GAssign("192.168.42.100", "00:50:4C:20:52:90")); //assign an ip address to a known MAC
43  //NOTE: GAssign does not burn the IP address with BN. This can be done after assignment through GOpen() and GCmd().
44 #endif
45 
46 #if 0
47  /*
48  * Change above line to "#if 1" to run examples below.
49  * Listing available hardware addresses.
50  */
51  x_e(GAddresses(buf, sizeof(buf))); //list available addresses
52  cout << "Available addresses:\n";
53  if (strlen(buf) != 0)
54  cout << buf << "\n\n";
55  else
56  cout << "none\n\n";
57 #endif
58 
59  cout << "Connecting to hardware\n";
60 
61  //Basic connections
62  x_e(GOpen("192.168.42.100 --subscribe ALL", &g)); //connect and assign a value to g.
63  //x_e(GOpen("/dev/galilpci0 --subscribe ALL", &g));
64  //x_e(GOpen("COM1 --baud 115200 --subscribe ALL", &g));
65 
66  x_e(GInfo(g, buf, sizeof(buf))); //grab connection string
67  cout << buf << '\n';
68 
69  //x_e(GCmd(g, "BN")); //example to burn the IP address if it was set above
70 
71 #if 0
72  /*
73  * Change above line to "#if 1" to run examples below.
74  * Comment out the function calls below to be avoided.
75  * Note some calls attempt to move motors and not all
76  * functions are compatible with all Galil products.
77  */
78 
79 
80  x_e(x_gread_gwrite(g)); //call examples for GRead() and GWrite().
81  x_e(x_gcommand(g)); //call examples for GCommand().
82  x_e(x_programs(g)); //call examples for GProgramDownload() and GProgramUpload().
83  x_e(x_arrays(g)); //call examples for GArrayDownload() and GArrayUpload().
84  x_e(x_grecord(g)); //call examples for GRecord(). WARNING, this call will attempt to move motors.
85  x_e(x_gmessage(g)); //call examples for GMessage().
86  x_e(x_ginterrupt(g)); //call examples for GInterrupt(). WARNING, this call will attempt to move motors.
87  x_e(x_gmotioncomplete(g)); //call examples for GMotionComplete. WARNING, this call will attempt to move motors.
88  x_e(x_nonblocking(g)); //call examples for using GRecord(), GMessage(), and GInterrupt() in a non-blocking mode.
89 
90 
91 #endif
92 
93 #if 0
94  /*
95  * Change above line to "#if 1" to run examples below.
96  * Loading Firmware
97  */
98  x_e(GFirmwareDownload(g, "c:/temp/d212r10r2.hex"));
99  x_e(GInfo(g, buf, sizeof(buf)));
100  cout << buf << '\n';
101 #endif
102 
103 #if 0
104  /*
105  * Change above line to "#if 1" to run examples below.
106  * Calling GSetupDownloadFile
107  */
108  GOption opt = 0;
109  const char* file_path = "C:/dev/test/gcb/test.gcb";
110  opt = GSetupDownloadFile(g, file_path, opt, buf, buf_size);
111  cout << "Setup file " << file_path << endl;
112  cout << "Parameters " << (opt & 0x02 ? "present" : "absent") << endl;
113  cout << "Variables " << (opt & 0x08 ? "present" : "absent") << endl;
114  cout << "Arrays " << (opt & 0x10 ? "present" : "absent") << endl;
115  cout << "Program " << (opt & 0x20 ? "present" : "absent") << endl;
116  //cout << buf; //print the setup info
117 
118  x_e(GSetupDownloadFile(g, "C:/dev/test/gcb/test.gcb", 0xff, 0, 0));
119 #endif
120 
121  if (g) x_e(GClose(g)); g = 0; //close g
122 
123 
124  }//try
125  catch (GReturn gr) //for x_e() function
126  {
127  if (gr == GALIL_EXAMPLE_ERROR)
128  cout << "ERROR: Example code failed\n";
129  else
130  {
131  cout << "Function returned " << gr << '\n';
132  GError(gr, buf, sizeof(buf));
133  cout << buf << '\n';
134  GSize size = sizeof(buf);
135 
136  if (g)
137  {
138  GUtility(g, G_UTIL_ERROR_CONTEXT, buf, &size);
139  cout << buf << '\n'; //further context
140  }
141 
142  }
143  rc = GALIL_EXAMPLE_ERROR;
144  if (g) GClose(g); g = 0; //close g
145  }
146  catch (std::exception& e)
147  {
148  std::cerr << "Unexpected std::exception... Kaboom. " << e.what() << std::endl;
149  rc = GALIL_EXAMPLE_ERROR;
150  if (g) GClose(g); g = 0; //close g
151  }
152  catch (...)
153  {
154  cout << "Unexpected error... Kaboom." << endl;
155  rc = GALIL_EXAMPLE_ERROR;
156  if (g) GClose(g); g = 0; //close g
157  }
158 
159  if (argc == 1) //if no args on command line, report and pause
160  {
161  cout << endl << endl;
162  if (rc == GALIL_EXAMPLE_OK)
163  cout << "examples.cpp executed OK\n";
164  else
165  cout << "examples.cpp returning error " << rc << '\n';
166 
167  cout << "main() is finished. Press Enter to exit:";
168  getchar(); //keep window open
169  }
170  return rc;
171 }
GCLIB_DLL_EXPORTED GReturn GCALL GUtility(GCon g, GOption request, GMemory memory1, GMemory memory2)
Provides read/write access to driver settings and convenience features based on the request variable.
GCLIB_DLL_EXPORTED GReturn GCALL GFirmwareDownload(GCon g, GCStringIn filepath)
Upgrade firmware.
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
GCLIB_DLL_EXPORTED GReturn GCALL GClose(GCon g)
Closes a connection to a Galil Controller.
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
GCLIB_DLL_EXPORTED GReturn GCALL GSetupDownloadFile(GCon g, GCStringIn file_path, GOption options, GCStringOut info, GSize info_len)
Download a saved controller configuration from a file.
Definition: arrays.c:476
GCLIB_DLL_EXPORTED GReturn GCALL GVersion(GCStringOut ver, GSize ver_len)
Uses GUtility(), G_UTIL_VERSION and G_UTIL_GCAPS_VERSION to provide the library and gcaps version num...
Definition: gclibo.c:29
GCLIB_DLL_EXPORTED void GCALL GError(GReturn rc, GCStringOut error, GSize error_len)
Provides a human-readable description string for return codes.
Definition: gclibo.c:459
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
GCLIB_DLL_EXPORTED GReturn GCALL GAddresses(GCStringOut addresses, GSize addresses_len)
Uses GUtility(), G_UTIL_GCAPS_ADDRESSES or G_UTIL_ADDRESSES to provide a listing of all available con...
Definition: gclibo.c:54
GCLIB_DLL_EXPORTED GReturn GCALL GOpen(GCStringIn address, GCon *g)
Open a connection to a Galil Controller.
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
int GOption
Option integer for various formatting, etc.
Definition: gclib.h:96
#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
unsigned int GSize
Size of buffers, etc.
Definition: gclib.h:95
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
#define G_UTIL_ERROR_CONTEXT
GUtility(), provides additional error context, where available.
Definition: gclib.h:70
int main(int argc, char *argv[])
Main function for Commands Example.
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
int x_arrays(GCon g)
Example GArrayDownload() and GArrayUpload() usage.
Definition: x_arrays.cpp:9
int x_grecord(GCon g)
Example GRecord() usage.
Definition: x_grecord.cpp:10
int x_gcommand(GCon g)
Example GCommand() usage.
Definition: x_gcommand.cpp:9
int x_gread_gwrite(GCon g)
Example GRead() and GWrite() usage.
int x_ginterrupt(GCon g)
Example GInterrupt() usage.
Definition: x_ginterrupt.cpp:9
void x_e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: x_examples.h:30
int x_nonblocking(GCon g)
Examples of using non-blocking operation of GRecord(), GInterrupt(), and GMessage().
int x_gmotioncomplete(GCon g)
Example GMotionComplete() usage.
int x_programs(GCon g)
Example GProgramDownload() and GProgramUpload() usage.
Definition: x_programs.cpp:9
int x_gmessage(GCon g)
Example GMessage() usage.
Definition: x_gmessage.cpp:9