gclib  2.0.8
Communications API for Galil controllers and PLCs
x_nonblocking.cpp
Go to the documentation of this file.
1 
7 #include "x_examples.h"
8 #include <iomanip> //hex printing
9 
10 int cur = 0;
11 char chars[] = { '|', '\\', '-','/'};
12 //simple progress indicator.
13 void progress()
14 {
15  cout << chars[cur] << '\r';
16  cout.flush();
17  ++cur %= 4;
18 }
19 
20 
21 //demonstrates non-blocking calls of GMessage(), GInterrupt(), and GRecord().
23 {
24 
25  // *** we assume -s ALL was passed to Open. ***
26  GReturn rc;
27  char buf[1024]; //read buffer
28 
29  cout << "\n************************************************************************\n";
30  cout << "Example GMessage non-blocking usage\n";
31  cout << "************************************************************************\n";
32 
33  x_e(GProgramDownload(g, "WT2000;MG TIME;EN", 0)); //wait 2 seconds, then print message
34  x_e(GCmd(g, "XQ"));
35 
36  x_e(GTimeout(g, 0)); //set timeout to zero for non-blocking read
37 
38  for (int i = 0;
39  (rc = GMessage(g, buf, sizeof(buf))) == G_GCLIB_NON_BLOCKING_READ_EMPTY;
40  i++)
41  {
42  x_e(GCmd(g, "MGTIME")); //do something useful here...
43  progress(); //and here
44  }
45 
46  x_e(rc); //verify the return code
47  x_e(GTimeout(g, -1)); //put the timeout back to the GOpen() setting
48  cout << buf << '\n'; //print the message
49 
50  cout << "\n************************************************************************\n";
51  cout << "Example GInterrupt non-blocking usage\n";
52  cout << "************************************************************************\n";
53  x_e(GProgramDownload(g, "WT2000;UI1;EN", 0)); //wait 2 seconds, then fire interrupt
54  x_e(GCmd(g, "XQ")); //start code
55 
56  GStatus byte = 0; //variable for the interrupt byte
57  x_e(GTimeout(g, 0)); //set timeout to zero for non-blocking read
58 
59  while ((rc = GInterrupt(g, &byte)) == G_GCLIB_NON_BLOCKING_READ_EMPTY)
60  {
61  x_e(GCmd(g, "MGTIME")); //do something useful here...
62  progress(); //and here
63  }
64  x_e(rc); //verify the return code
65  x_e(GTimeout(g, -1)); //put the timeout back to the GOpen() setting
66  cout << " " << hex << uppercase << (int) byte << dec << '\n'; //print the byte in hex
67 
68 
69  cout << "\n************************************************************************\n";
70  cout << "Example GRecord non-blocking usage\n";
71  cout << "************************************************************************\n";
72 
73  GDataRecord dr;
74  x_e(GRecordRate(g, 0)); //turn off data record
75  x_e(GTimeout(g, 0)); //set timeout to zero for non-blocking read
76  GRecord(g, &dr, G_DR); //throw away a waiting record
77  x_e(GTimeout(g, -1)); //put the timeout back to the GOpen() setting
78  x_e(GRecordRate(g, 2000)); //turn on data record
79  x_e(GTimeout(g, 0)); //set timeout to zero for non-blocking read
80  while ((rc = GRecord(g, &dr, G_DR)) == G_GCLIB_NON_BLOCKING_READ_EMPTY)
81  {
82  x_e(GCmd(g, "MGTIME")); //do something useful here...
83  progress(); //and here
84  }
85  x_e(rc); //verify the return code
86  x_e(GTimeout(g, -1)); //put the timeout back to the GOpen() setting
87  x_e(GRecordRate(g, 0)); //turn off data record
88  cout << " " << dr.dmc4000.sample_number << '\n';
89 
90  return GALIL_EXAMPLE_OK;
91 }
GCLIB_DLL_EXPORTED GReturn GCALL GRecord(GCon g, union GDataRecord *record, GOption method)
Provides a fresh copy of the controller's data record. Data is cast into a union, GDataRecord.
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 GInterrupt(GCon g, GStatus *status_byte)
Provides access to PCI and UDP interrupts from the controller.
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 GRecordRate(GCon g, double period_ms)
Sets the asynchronous data record to a user-specified period via DR.
Definition: gclibo.c:342
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_DR
Value for GRecord() method variable for acquiring a data record via DR mode.
Definition: gclib.h:50
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
unsigned char GStatus
Interrupt status byte.
Definition: gclib.h:101
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
#define G_GCLIB_NON_BLOCKING_READ_EMPTY
GMessage, GInterrupt, and GRecord can be called with a zero timeout. If there wasn't data waiting in ...
Definition: gclib_errors.h:25
UW sample_number
sample number.
Definition: gclib_record.h:45
Data record union, containing all structs and a generic byte array accessor.
struct GDataRecord4000 dmc4000
The DMC-4000 data record.
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_nonblocking(GCon g)
Examples of using non-blocking operation of GRecord(), GInterrupt(), and GMessage().