| gclib 1.34.15 Communications API for Galil controllers and PLCs |

Message.vb
1 Partial Public Module Examples
2 Public Function Message(gclib As Gclib)
3 Console.WriteLine("***************************************************************")
4 Console.WriteLine("Example GMessage() usage")
5 Console.WriteLine("***************************************************************")
6
7 gclib.GCommand("TR0") 'Turn off trace
8
9 'This program will force one message to appear as two separate packets.
10 gclib.GProgramDownload("MG ""HELLO "" {N}" + vbCr +
11 "MG ""WORLD """ + vbCr +
12 "EN")
13
14 gclib.GCommand("XQ") 'Begins execution Of program On controller
15
16 Dim buf = ""
17 Dim msg = ""
18
19 'It Is important to note that a message can be too large to read in one
20 'GMessage() call. Keep calling GMessage() while there are no errors to
21 'get the full message.
22
23 'While still receiving messages
24 buf = gclib.GMessage()
25 While buf <> ""
26 For b = 0 To buf.Length - 1 'While message characters are in the buffer
27
28 msg += buf(b) 'Copy chars from buffer To message
29
30 'If the message ends in "\r\n" it Is ready to be terminated
31 If (msg.Length > 2) AndAlso (msg(msg.Length - 1) = vbLf) AndAlso (msg(msg.Length - 2) = vbCr) Then
32 Console.WriteLine(msg)
33 msg = "" 'Reset message index
34 Exit While
35 End If
36 Next
37 buf = gclib.GMessage()
38 End While
39
40
41 'Downloads program to the controller
42 gclib.GCommand("TR1") 'Turn On trace
43 gclib.GProgramDownload(
44 "i=0" + vbCr +
45 "#A" + vbCr +
46 "MGi" + vbCr +
47 "i=i+1" + vbCr +
48 "WT100" + vbCr +
49 "JP#A,i<1" + vbCr +
50 "i=i/0" + vbCr +
51 "EN")
52
53 gclib.GCommand("XQ") 'Begins execution Of program On controller
54
55 'Lines returned by GMessage() can be one of three types
56 '1) Standard Lines begin with a space (" ")
57 '2) Crashed code begins with a question mark ("?")
58 '3) Trace Lines begin with a line number ("1,6,15...")
59
60 'While still receiving messages
61 buf = gclib.GMessage()
62 While buf <> ""
63 For b = 0 To buf.Length - 1 'While message characters are in the buffer
64
65 msg += buf(b) 'Copy chars from buffer To message
66
67 'If the message ends in "\r\n" its ready to be terminated
68 If (msg.Length > 2) AndAlso (msg(msg.Length - 1) = vbLf) AndAlso (msg(msg.Length - 2) = vbCr) Then
69
70 If (msg(0) = " ") Then 'Standard Lines begin with a space (" ")
71 Console.Write("Standard Line: ")
72 ElseIf (msg(0) = "?") Then 'Crashed code begins with a question mark ("?")
73 Console.Write("Crashed Code: ")
74 Else 'Trace Lines begin with a line number ("1,6,15...")
75 Console.Write("Trace Line: ")
76 End If
77 Console.WriteLine(msg)
78 msg = ""
79 End If
80 Next
81 buf = gclib.GMessage()
82 End While
83
84 Return Examples.GALIL_EXAMPLE_OK
85 End Function
86 End Module