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