| gclib 1.35.5 Communications API for Galil controllers and PLCs |

commands.cpp
Go to the documentation of this file.
1
10 #include "examples.h"
11
12 #include <iostream> //std::cout
13
14 using namespace std;
15
16 GReturn commands(GCon g)
17 {
18 char buf[G_SMALL_BUFFER]; //traffic buffer
19 GSize read_bytes = 0; //bytes read in GCommand
20 int value;
21 double d_value;
22
23 cout << "*****************************************************************************\n";
24 cout << "************************** GCmdT() example *****************************\n";
25 cout << "*****************************************************************************\n";
26 cout << "GCmdT() will return a trimmed response of GCommand()\n";
27 cout << "The command 'PR ?,?' will return the relative "
28 "position of the A and B axes\n";
29 e(GCommand(g, "PR ?,?", buf, G_SMALL_BUFFER, &read_bytes));
30 cout << "<<PR ?,? with GCommand(): " << buf << ">>\n";
31 e(GCmdT(g, "PR ?,?", buf, G_SMALL_BUFFER, NULL));
32 cout << "<<PR ?,? with GCmdT(): " << buf << ">>\n";
33 char* front; //this must not be a pointer on the heap, it will be modified.
34 e(GCmdT(g, "MG TIME", buf, sizeof(buf), &front)); //Trim back and front.
35 cout << "<<MG TIME with GCmdT() and front trimmed: " << front << ">>" << "\n\n";
36
37 cout << "*****************************************************************************\n";
38 cout << "************************** GCmdI() example *****************************\n";
39 cout << "*****************************************************************************\n";
40 cout << "GCmdI() will return the value of GCommand() parsed as an int\n";
41 cout << "The command 'MG _LMS' will return the available "
42 "space in the vector buffer of the S plane.\n";
43 e(GCmdT(g, "MG _LMS", buf, G_SMALL_BUFFER, NULL));
44 cout << "MG _LMS with GCmdT(): " << buf << "\n";
45 e(GCmdI(g, "MG _LMS", &value));
46 cout << "MG _LMS with GCmdI(): " << value << "\n\n";
47
48 cout << "*****************************************************************************\n";
49 cout << "************************** GCmd() example ******************************\n";
50 cout << "*****************************************************************************\n";
51 cout << "GCmd() will execute the given command but does not return a value.\n";
52 cout << "GCmd is useful for basic operations such as beginning"
53 " motion or setting speed\n";
54 e(GCmd(g, "BG A"));
55 e(GCmd(g, "SP 5000"));
56 cout << "GCmd(g, \"BG A\");\n";
57 cout << "GCmd(g, \"SP 5000\");\n\n";
58
59
60 cout << "*****************************************************************************\n";
61 cout << "************************** GCmdD() example ******************************\n";
62 cout << "*****************************************************************************\n";
63 cout << "GCmdD() will return the value of GCommand parsed as a double\n";
64 cout << "The command 'MG @AN[1]' will return the value of Analog Input 1\n";
65 e(GCmdD(g, "MG @AN[1]", &d_value));
66 cout << "MG @AN[1] with GCmdD(): " << d_value << "\n\n";
67
68 cout << "*****************************************************************************\n";
69 cout << "************************ Galil Double Format ***************************\n";
70 cout << "*****************************************************************************\n";
71 double d_val = 0.00235;
72 sprintf(buf, "%.4f", d_val);
73 cout << "Galil Controllers expect double values to be formatted to 4 "
74 "decimal places\n";
75 cout << "Unformatted double value: " << d_val << "\n";
76 cout << "Formatted double value rounded to 4 decimal places: " << buf << "\n\n";
77
78 cout << "*****************************************************************************\n";
79 cout << "******************* G_UTIL_ERROR_CONTEXT example ***********************\n";
80 cout << "*****************************************************************************\n";
81 //To check any OS errors - call GUtility with G_UTIL_ERROR_CONTEXT
82 GSize size = sizeof(buf);
83 GUtility(g, G_UTIL_ERROR_CONTEXT, buf, &size);
84 cout << "GUtility() with G_UTIL_ERROR_CONTEXT: " << buf << "\n";
85
86 return GALIL_EXAMPLE_OK;
87 }
examples.h
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.
GCmdT
GCLIB_DLL_EXPORTED GReturn GCALL GCmdT(GCon g, GCStringIn command, GCStringOut trimmed_response, GSize response_len, GCStringOut *front)
Wrapper around GCommand that trims the response.
Definition: gclibo.c:243
GCommand
GCLIB_DLL_EXPORTED GReturn GCALL GCommand(GCon g, GCStringIn command, GBufOut buffer, GSize buffer_len, GSize *bytes_returned)
Performs a command-and-response transaction on the connection.
GCmdI
GCLIB_DLL_EXPORTED GReturn GCALL GCmdI(GCon g, GCStringIn command, int *value)
Wrapper around GCommand that provides the return value of a command parsed into an int.
Definition: gclibo.c:278
GCmdD
GCLIB_DLL_EXPORTED GReturn GCALL GCmdD(GCon g, GCStringIn command, double *value)
Wrapper around GCommand that provides the return value of a command parsed into a double.
Definition: gclibo.c:289
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
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
GSize
unsigned int GSize
Size of buffers, etc.
Definition: gclib.h:95
GCon
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
G_UTIL_ERROR_CONTEXT
#define G_UTIL_ERROR_CONTEXT
GUtility(), provides additional error context, where available.
Definition: gclib.h:70
e
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
commands
GReturn commands(GCon g)
Demonstrates various uses of GCommand() and GUtility().
Definition: commands.cpp:16