gclib  2.0.8
Communications API for Galil controllers and PLCs
x_gcommand.cpp
Go to the documentation of this file.
1 
7 #include "x_examples.h"
8 
10 {
11  cout << "\n************************************************************************\n";
12  cout << "Example GCommand() usage\n";
13  cout << "************************************************************************\n";
14 
15  char buf[1024]; //traffic buffer
16  GSize read_bytes = 0; //bytes read in GCommand
17 
18  //-------------------------------------------------------------------------
19  cout << "Revision report, ^R^V\n";
20  x_e(GCommand(g, "\x12\x16", buf, sizeof(buf), &read_bytes));
21  cout << buf << "\n\n";
22 
23  //-------------------------------------------------------------------------
24  //Simple Command, no response accessible.
25  x_e(GCmd(g, "ST"));
26 
27  //-------------------------------------------------------------------------
28  cout << "Command Values\n";
29  x_e(GCmd(g, "val=10")); //set initial value to an integer
30  int vali = 0; //integer to be used
31  x_e(GCmdI(g, "val=?", &vali)); //stuff value into integer
32  cout << "val is " << vali << '\n';
33  vali++;
34  sprintf(buf, "val=%d", vali);
35  x_e(GCmd(g, buf));
36  x_e(GCmdI(g, "val=?", &vali));
37  cout << "val is " << vali << '\n';
38 
39  x_e(GCmd(g, "val=3.1415")); //set initial value to a decimal
40  double vald = 0; //double to be used
41  x_e(GCmdD(g, "val=?", &vald));
42  cout << "val is " << vald << '\n';
43  vald *= vald; //square
44  sprintf(buf, "val=%f", vald);
45  x_e(GCmd(g, buf));
46  x_e(GCmdD(g, "val=?", &vald));
47  cout << "val is " << vald << '\n';
48 
49  //-------------------------------------------------------------------------
50  cout << "\nCommand Trimming\n";
51  x_e(GCommand(g, "MGTIME", buf, sizeof(buf), &read_bytes)); //standard command call.
52  cout << ">" << buf << "<" << '\n';
53 
54  x_e(GCmdT(g, "MG TIME", buf, sizeof(buf), 0)); //Trim back.
55  cout << ">" << buf << "<" << '\n';
56 
57  char* front; //this must not be a pointer on the heap, it will be modified.
58  x_e(GCmdT(g, "MG TIME", buf, sizeof(buf), &front)); //Trim back and front.
59  cout << ">" << front << "<" << '\n';
60 
61  //-------------------------------------------------------------------------
62  cout << "\nReceiving Binary Data\n";
63  x_e(GCommand(g, "QR", buf, sizeof(buf), &read_bytes)); //QR Response is binary
64  cout << "QR read " << read_bytes << " bytes\n"; //Normally use GRecord() for QR.
65 
66  //-------------------------------------------------------------------------
67  cout << "\nError handling\n";
68  GReturn rc = G_NO_ERROR;
69  if ((rc = GCommand(g, "QDA[]", buf, sizeof(buf), &read_bytes)) == G_COMMAND_CALLED_WITH_ILLEGAL_COMMAND)
70  cout << "QD correctly trapped, not allowed, try GArrayDownload()\n";
71  else
72  {
73  cout << "Unexpected QD behaviour\n";
74  return GALIL_EXAMPLE_ERROR;
75  }
76 
77  if ((rc = GCommand(g, "DL\rvar=3.14\rEN\r\\", buf, sizeof(buf), &read_bytes)) == G_COMMAND_CALLED_WITH_ILLEGAL_COMMAND)
78  cout << "DL correctly trapped, not allowed, try GProgramDownload()\n";
79  else
80  {
81  cout << "Unexpected DL behaviour\n";
82  return GALIL_EXAMPLE_ERROR;
83  }
84 
85  //-------------------------------------------------------------------------
86  cout << "\nModifying timeout\n";
87  x_e(GTimeout(g, 10000)); //increase timeout for BP. 10 seconds is excessive, but sufficient for all products.
88  cout << "Burning program...";
89  x_e(GCommand(g, "BP", buf, sizeof(buf), &read_bytes));
90  x_e(GTimeout(g, G_USE_INITIAL_TIMEOUT)); //restore timeout
91  cout << "OK\n";
92 
93  return GALIL_EXAMPLE_OK;
94 }
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
GCLIB_DLL_EXPORTED GReturn GCALL GTimeout(GCon g, short timeout_ms)
Uses GUtility() and G_UTIL_TIMEOUT_OVERRIDE to set the library timeout.
Definition: gclibo.c:65
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.
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
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
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
#define G_COMMAND_CALLED_WITH_ILLEGAL_COMMAND
GCommand() was called with an illegal command, e.g. ED, DL or QD.
Definition: gclib_errors.h:49
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
#define G_NO_ERROR
Return value if function succeeded.
Definition: gclib_errors.h:13
unsigned int GSize
Size of buffers, etc.
Definition: gclib.h:95
#define G_USE_INITIAL_TIMEOUT
GUtility(), for timeout override. Set G_UTIL_TIMEOUT_OVERRIDE to this value to use initial GOpen() ti...
Definition: gclib.h:61
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
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_gcommand(GCon g)
Example GCommand() usage.
Definition: x_gcommand.cpp:9