gclib  2.0.8
Communications API for Galil controllers and PLCs
x_gread_gwrite.cpp
Go to the documentation of this file.
1 
7 #include "x_examples.h"
8 
10 {
11  cout << "\n************************************************************************\n";
12  cout << "Example GRead() and GWrite() usage\n";
13  cout << "************************************************************************\n";
14 
15  char buf[1024]; //traffic buffer
16  GSize read_bytes = 0; //bytes read
17  GSize total_bytes = 0; //total bytes read
18 
19  //-------------------------------------------------------------------------
20  /*
21  * Ad hoc program downlader
22  *
23  * GProgramDownload() should be used for all DMC program downloads. DL is used here to demonstrate
24  * GRead()/GWrite() usage.
25  *
26  * GRead()/GWrite() are useful, for example, for a firmware NRE that provides a binary response, or for a command
27  * that does not follow the colon termination of typical Galil commands. DL is an example of such a command.
28  *
29  */
30 
31  string program = "i=0\r#loop\ri=i+1\rWT10\rJP#loop,i<10,\rEN\r"; //simple program for download
32 
33  x_e(GCmd(g, "AB")); //Abort any running programs
34 
35  x_e(GWrite(g, "DL\r", 3)); //Write the DL command to the controller to access the program buffer. The controller does not respond to this command.
36  //don't forget the carriage return when using GWrite() for commands.
37 
38  //GSleep(100); //some products need a delay to erase flash. 100ms is overkill, but sufficient.
39 
40  x_e(GWrite(g, program.c_str(), program.size())); //send the program to the program buffer
41  x_e(GWrite(g, "\\", 1)); //send a backslash to exit the program buffer.
42 
43 
44  /*
45  * Assuming the return format of DL is unknown, this demo reads until a read times out.
46  * A faster approach would be to read for a known terminating sequence that doesn't appear in the data.
47  * For example, standard Galil commands terminate data with a colon.
48  */
49  x_e(GTimeout(g, 100)); //adjust timeout
50  GReturn rc = G_NO_ERROR; //return code
51  while (rc == G_NO_ERROR) //read until timeout
52  {
53  total_bytes += read_bytes;
54  rc = GRead(g, buf, sizeof(buf), &read_bytes);
55  }
56 
57  x_e(GTimeout(g, G_USE_INITIAL_TIMEOUT)); //restore timeout
58  cout << "\nRead " << total_bytes << " byte(s)\n";
59  cout.write(buf, total_bytes); //print the received data
60  cout << "\n";
61 
62  //now test the downloaded program
63  x_e(GCmd(g, "i=0")); //force i to zero
64  x_e(GCmd(g, "XQ")); //execute the program
65  GSleep(200); //loop should take about 100ms
66 
67  int i;
68  x_e(GCmdI(g, "i=?", &i));// pull the controller's value of i into an int
69  if (i == 10)
70  {
71  cout << "Program test OK.\n";
72  return GALIL_EXAMPLE_OK;
73  }
74  else
75  {
76  cout << "Program test failed. " << i << "\n";
77  return GALIL_EXAMPLE_ERROR;
78  }
79 
80 
81 
82 
83 }
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 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 GWrite(GCon g, GBufIn buffer, GSize buffer_len)
Performs a write 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 GRead(GCon g, GBufOut buffer, GSize buffer_len, GSize *bytes_read)
Performs a read on the connection.
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
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_gread_gwrite(GCon g)
Example GRead() and GWrite() usage.