gclib  2.0.8
Communications API for Galil controllers and PLCs
x_programs.cpp
Go to the documentation of this file.
1 
7 #include "x_examples.h"
8 
10 {
11  cout << "\n************************************************************************\n";
12  cout << "Example GProgramDownload() and GProgramUpload() usage\n";
13  cout << "************************************************************************\n";
14  GReturn rc = G_NO_ERROR; //return code
15  char buf[1024]; //traffic buffer
16 
17  //-------------------------------------------------------------------------
18  //capping compression
19  string program = "#A;i=0;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;EN";
20  //Program line above is too large to fit on any Galil controller, however it can easily fit if broken up with level 4 compression.
21  //the value of i at the end of code execution is 20.
22 
23  if ((rc = GProgramDownload(g, program.c_str(), "--max 3"))
25  cout << "GProgramDownload() correctly errored. Can't fit with level 3 compression\n";
26  else
27  {
28  cout << "Unexpected GProgramDownload() behaviour\n";
29  return GALIL_EXAMPLE_ERROR;
30  }
31 
32  //-------------------------------------------------------------------------
33  //full compression
34  if ((rc = GProgramDownload(g, program.c_str(), "--max 4"))
35  == G_NO_ERROR)
36  cout << "Program Downloaded with compression level 4\n";
37  else
38  {
39  cout << "Unexpected GProgramDownload() behaviour\n";
40  return GALIL_EXAMPLE_ERROR;
41  }
42 
43  x_e(GProgramDownload(g, program.c_str(), 0)); //null for preprocessor equivalent to GProgramDownload(g, program.c_str(), "--compress 4")
44 
45  //-------------------------------------------------------------------------
46  //program upload
47  cout << "Uploading program:\n";
48  x_e(GProgramUpload(g, buf, sizeof(buf)));
49  cout << buf;
50 
51  //-------------------------------------------------------------------------
52  //program upload file
53  x_e(GProgramUploadFile(g, "temp.dmc"));
54 
55  //-------------------------------------------------------------------------
56  //program download file
57  x_e(GProgramDownload(g, "", 0)); //erase program
58  x_e(GProgramDownloadFile(g, "temp.dmc", 0));
59 
60  //-------------------------------------------------------------------------
61  //executing/verifying code
62  GCmd(g, "XQ"); //execute the code
63  GSleep(10); //wait a brief interval for the code to complete, TB could be checked with a mask too.
64  int val;
65  GCmdI(g, "i=?", &val);
66  if (val == 20)
67  cout << "\n\nProgram executed as expected";
68  else
69  {
70  cout << "\n\nUnexpected i value " << val << '\n';
71  return GALIL_EXAMPLE_ERROR;
72  }
73 
74  return GALIL_EXAMPLE_OK;
75 }
GCLIB_DLL_EXPORTED GReturn GCALL GProgramDownloadFile(GCon g, GCStringIn file_path, GCStringIn preprocessor)
Program download from file.
Definition: gclibo.c:387
GCLIB_DLL_EXPORTED void GCALL GSleep(unsigned int timeout_ms)
Uses GUtility() and G_UTIL_SLEEP to provide a blocking sleep call which can be useful for timing-base...
Definition: gclibo.c:24
GCLIB_DLL_EXPORTED GReturn GCALL GProgramUpload(GCon g, GBufOut buffer, GSize buffer_len)
Uploads a program from the controller's program buffer.
GCLIB_DLL_EXPORTED GReturn GCALL GProgramUploadFile(GCon g, GCStringIn file_path)
Program upload to file.
Definition: gclibo.c:430
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 GProgramDownload(GCon g, GCStringIn program, GCStringIn preprocessor)
Downloads a program to the controller's program buffer.
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
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
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
#define G_UNABLE_TO_COMPRESS_PROGRAM_TO_FIT
Program preprocessor could not compress the program within the user's constraints.
Definition: gclib_errors.h:70
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_programs(GCon g)
Example GProgramDownload() and GProgramUpload() usage.
Definition: x_programs.cpp:9