| gclib 528 Communications API for Galil controllers and PLCs |

position_tracking_example.cpp
Go to the documentation of this file.
1
10 #include "examples.h"
11
12 #include <iostream> //std::cout
13 using namespace std;
14
16
21 int main(int argc, char * argv[])
22 {
23 GReturn rc = GALIL_EXAMPLE_OK;
24 char buf[G_SMALL_BUFFER];
25
26 //var used to refer to a unique connection. A valid connection is nonzero.
27 GCon g = 0;
28
29 try
30 {
31 if (argc < 2 || argc > 3) //Invalid number of arguments
32 {
33 cerr << "Incorrect number of arguments provided\n";
34 cerr << "Usage: position_tracking_example.exe <ADDRESS> <SPEED=5000>\n";
35 return GALIL_EXAMPLE_ERROR;
36 }
37
38 char* address = argv[1]; //Retrieve address from command line
39 e(GOpen(address, &g)); //Opens a connection at the provided address
40
41 if (argc == 3) //Position tracking with custom speed
42 {
43 //Retrieve speed from command line and convert to int
44 char* end;
45 int speed = strtol(argv[2], &end, 10);
46 //If this character is not a null character,
47 //the user did not enter a valid integer for speed
48 if (*end != '\0')
49 {
50 cerr << "An invalid speed was entered. "
51 "Please enter a valid integer for speed.\n"
52 "Usage: position_tracking_example.exe <ADDRESS> <SPEED=5000>\n";
53 return GALIL_EXAMPLE_ERROR;
54 }
55
56 //Puts controller into Position Tracking Mode and accepts user-entered positions
57 rc = position_tracking(g, speed);
58 }
59 else if (argc == 2) //Position tracking with default speed
60 {
61 //Puts controller into Position Tracking Mode and accepts user - entered positions
62 rc = position_tracking(g);
63 }
64 }
65 catch (GReturn gr)
66 {
67 GError(gr, buf, G_SMALL_BUFFER); //Get Error Information
68 cout << buf << '\n';
69 if (g)
70 {
71 GSize size = sizeof(buf);
72 GUtility(g, G_UTIL_ERROR_CONTEXT, buf, &size);
73 cout << buf << '\n'; //further context
74 }
75 return GALIL_EXAMPLE_ERROR;
76 }
77
78 return rc;
79 }
examples.h
GCon
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:92
GUtility
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.
main
int main(int argc, char *argv[])
Main function for Commands Example.
Definition: commands_example.cpp:20
G_UTIL_ERROR_CONTEXT
#define G_UTIL_ERROR_CONTEXT
GUtility(), provides additional error context, where available.
Definition: gclib.h:70
position_tracking
GReturn position_tracking(GCon g, int speed=5000)
Puts controller into Position Tracking Mode and accepts user-entered positions.
Definition: position_tracking.cpp:15
GOpen
GCLIB_DLL_EXPORTED GReturn GCALL GOpen(GCStringIn address, GCon *g)
Open a connection to a Galil Controller.
GError
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:445
GSize
unsigned int GSize
Size of buffers, etc.
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:87
e
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:30
GReturn
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:91