| gclib 1.32.6 Communications API for Galil controllers and PLCs |

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