gclib  2.0.8
Communications API for Galil controllers and PLCs
x_gmessage.cpp
Go to the documentation of this file.
1 
7 #include "x_examples.h"
8 
10 {
11  cout << "\n************************************************************************\n";
12  cout << "Example GMessage() usage\n";
13  cout << "************************************************************************\n";
14 
15  //-------------------------------------------------------------------------
16  char buf[1024]; //traffic buffer
17  x_e(GProgramDownload(g, "i=0\r#A\rMGi\ri=i+1\rWT100\rJP#A,i<10\rEN", 0));
18  x_e(GCmd(g, "XQ"));
19 
20  int rc;
21  GTimeout(g, 200);//adjust timeout
22  int i = 0;
23  while ((rc = GMessage(g, buf, sizeof(buf))) == G_NO_ERROR)
24  {
25  cout << buf;
26  if (strstr(buf, ".") != 0) //each MG has a decimal point
27  i++; //count it
28  }
29  GTimeout(g, G_USE_INITIAL_TIMEOUT); //restore timeout
30  if (i == 10)
31  return GALIL_EXAMPLE_OK;
32  else
33  {
34  cout << "Expected 10 messages\n";
35  return GALIL_EXAMPLE_ERROR;
36  }
37 }
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 GMessage(GCon g, GCStringOut buffer, GSize buffer_len)
Provides access to unsolicited messages from the controller.
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
#define G_NO_ERROR
Return value if function succeeded.
Definition: gclib_errors.h:13
#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_gmessage(GCon g)
Example GMessage() usage.
Definition: x_gmessage.cpp:9