| gclib 1.35.5 Communications API for Galil controllers and PLCs |

message.cs
Go to the documentation of this file.
1
10 using System;
11
12 namespace examples
13 {
14 public static partial class Examples
15 {
26
27 public static int Message(gclib gclib)
28 {
29 Console.WriteLine("***************************************************************");
30 Console.WriteLine("Example GMessage() usage");
31 Console.WriteLine("***************************************************************");
32
33 gclib.GCommand("TR0"); //Turn off trace
34
35 //This program will force one message to appear as two separate packets.
36 gclib.GProgramDownload("MG \"HELLO \" {N}\r" +
37 "MG \"WORLD \"\r" +
38 "EN");
39
40 gclib.GCommand("XQ"); //Begins execution of program on controller
41
42 string buf = "";
43 string message = "";
44
45 // It is important to note that a message can be too large to read in one
46 // GMessage() call. Keep calling GMessage() while there are no errors to
47 // get the full message.
48
49 //While still receiving messages
50 while ((buf = gclib.GMessage()) != "")
51 {
52 for (int b = 0; b < buf.Length; b++) //While message characters are in the buffer
53 {
54 message += buf[b]; //Copy chars from buffer to message
55
56 //If the message ends in "\r\n" it is ready to be terminated
57 if (message.Length > 2 && message[message.Length - 1] == '\n' && message[message.Length - 2] == '\r')
58 {
59 Console.WriteLine(message);
60 message = ""; //Reset message index
61 }
62 }
63 }
64
65 //Downloads program to the controller
66 gclib.GCommand("TR1"); //Turn on trace
67 gclib.GProgramDownload(
68 "i=0\r" +
69 "#A\r" +
70 "MGi\r" +
71 "i=i+1\r" +
72 "WT100\r" +
73 "JP#A,i<1\r" +
74 "i=i/0\r" +
75 "EN");
76 gclib.GCommand("XQ"); //Begins execution of program on controller
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 ((buf = gclib.GMessage()) != "")
85 {
86 for (int b = 0; b < buf.Length; b++) //While message characters are in the buffer
87 {
88 message += buf[b]; //Copy chars from buffer to message
89
90 //If the message ends in "\r\n" its ready to be terminated
91 if (message.Length > 2 && message[message.Length - 1] == '\n' && message[message.Length - 2] == '\r')
92 {
93 if (message[0] == ' ') //Standard Lines begin with a space (" ")
94 Console.Write("Standard Line: ");
95 else if (message[0] == '?') //Crashed code begins with a question mark ("?")
96 Console.Write("Crashed Code: ");
97 else //Trace Lines begin with a line number ("1,6,15...")
98 Console.Write("Trace Line: ");
99
100 Console.WriteLine(message);
101 message = "";
102 }
103 }
104 }
105
106 return Examples.GALIL_EXAMPLE_OK;
107 }
109 }
110 }
examples.Examples
Provides a class of shared constants and methods for gclib's example projects.
Definition: commands.cs:16
examples.Examples.GALIL_EXAMPLE_OK
const int GALIL_EXAMPLE_OK
Examples success code.
Definition: examples.cs:29
message
GReturn message(GCon g)
Demonstrates how to receive messages from the controller and detect differences in Trace and crashed ...
Definition: message.cpp:14
examples.Examples.Message
static int Message(gclib gclib)
Demonstrates how to receive messages from the controller and detect differences in Trace and crashed ...
Definition: message.cs:27
examples
Definition: commands.cs:14