gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
message.cpp
Go to the documentation of this file.
1 
4 #include "examples.h"
5 
6 #include <iostream> //std::cout
7 #include <string.h>
8 using namespace std;
9 
10 GReturn message(GCon g)
11 {
12  char buf[G_SMALL_BUFFER]; //traffic buffer
13  cout << "***************************************************************\n";
14  cout << "Example GMessage() usage\n";
15  cout << "***************************************************************\n";
16 
17  e(GCmd(g, "TR0")); // Turn off trace
18 
19  //This program will force one message to appear as two separate packets.
20  e(GProgramDownload(g,
21  "MG \"HELLO \" {N}\r"
22  "MG \"WORLD \"\r"
23  "EN", 0));
24  e(GCmd(g, "XQ")); //Begins execution of program on controller
25 
26  int rc = 0;
27  char message[G_SMALL_BUFFER];
28  int b = 0; //iterator for buf
29  int m = 0; //iterator for message
30 
31  // It is important to note that a message can be too large to read in one
32  // GMessage() call. Keep calling GMessage() while there are no errors to
33  // get the full message.
34 
35  //While still receiving messages
36  while ((rc = GMessage(g, buf, G_SMALL_BUFFER)) == G_NO_ERROR)
37  {
38  b = 0; //reset buffer index
39 
40  while (buf[b] != '\0') //While message characters are in the buffer
41  {
42  message[m] = buf[b]; //Copy chars from buffer to message
43 
44  //If the message ends in "\r\n" its ready to be terminated
45  if (m > 0 && message[m] == '\n' && message[m - 1] == '\r')
46  {
47  message[m + 1] = '\0'; //Null terminate the message
48  cout << '<' << message << ">\n";
49  m = 0; //Reset message index
50  }
51  else
52  {
53  m++; //Increment message index
54  }
55 
56  b++; //Increment buf index
57  }
58  }
59 
60  //Downloads program to the controller
61  e(GCmd(g, "TR1")); // Turn on trace
62  e(GProgramDownload(g, "i=0\r"
63  "#A\r"
64  "MGi\r"
65  "i=i+1\r"
66  "WT100\r"
67  "JP#A,i<1\r"
68  "i=i/0\r"
69  "EN", 0));
70  e(GCmd(g, "XQ")); //Begins execution of program on controller
71 
72  m = 0; //Reset message buffer
73 
74  // Lines returned by GMessage() can be one of three types:
75  // 1) Standard Lines begin with a space (" ")
76  // 2) Crashed code begins with a question mark ("?")
77  // 3) Trace Lines begin with a line number ("1,6,15...")
78 
79  //While still receiving messages
80  while ((rc = GMessage(g, buf, G_SMALL_BUFFER)) == G_NO_ERROR)
81  {
82  b = 0; //reset buf index
83 
84  while (buf[b] != '\0') //While message characters are in the buffer
85  {
86  message[m] = buf[b]; //Copy chars from buffer to message
87 
88  //If the message ends in "\r\n" its ready to be terminated
89  if (m > 0 && message[m] == '\n' && message[m - 1] == '\r')
90  {
91  message[m + 1] = '\0'; //Null terminate the message
92 
93  if (message[0] == ' ') //Standard Lines begin with a space (" ")
94  cout << "Standard Line: ";
95  else if (message[0] == '?') //Crashed code begins with a question mark ("?")
96  cout << "Crashed Code: ";
97  else //Trace Lines begin with a line number ("1,6,15...")
98  cout << "Trace Line: ";
99 
100  cout << message;
101 
102  m = 0; //Reset message index
103  }
104  else
105  {
106  m++; //Increment message index
107  }
108 
109  b++; //Increment buf index
110  }
111  }
112 
113  // If no communication has been made to gcaps for 10 minutes the connection
114  // will expire. This can be prevented by periodically sending the GUtility()
115  // Keep Alive command
116  e(GUtility(g, G_UTIL_GCAPS_KEEPALIVE, NULL, NULL));
117 
118  return GALIL_EXAMPLE_OK;
119 }
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:22
GReturn message(GCon g)
Demonstrates how to receive messages from the controller and detect differences in Trace and crashed ...
Definition: message.cpp:10