gclib  2.0.8
Communications API for Galil controllers and PLCs
Form1.vb
Go to the documentation of this file.
1 ''' <summary>
2 ''' Demonstrates using gclib in a Windows Form, including using a second thread to free the GUI.
3 ''' </summary>
4 Public Class MainForm
5 
6 #Region "UI"
7 
8  'Runs when form loads
9  Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
10  Print("Enter a FULL GOpen() address above and click Go", PrintStyle.Instruction)
11  Print("NOTE: This demo will attempt to move Axis A", PrintStyle.Instruction)
12  End Sub
13 
14  'Opens Galil's help to show GOpen() options
15  Private Sub HelpLabel_Click(sender As Object, e As EventArgs) Handles HelpLabel.Click
16  'link to GOpen() documentation.
17  System.Diagnostics.Process.Start("http://www.galil.com/sw/pub/all/doc/gclib/html/gclib_8h_aef4aec8a85630eed029b7a46aea7db54.html#aef4aec8a85630eed029b7a46aea7db54")
18  End Sub
19 
20  'Runs when user clicks Go button
21  Private Sub GoButton_Click(sender As Object, e As EventArgs) Handles GoButton.Click
22  If AddressTextBox.Text.Length = 0 Then
23  Print("Enter a FULL GOpen() address above and click Go", PrintStyle.Instruction)
24  Return
25  End If
26  RunDemo(AddressTextBox.Text)
27  End Sub
28 
29  'Various print styles.
30  Private Enum PrintStyle
31  Instruction
32  Normal
33  GalilData
34  GclibData
35  Err
36  End Enum
37 
38  ''' <summary>
39  ''' Thread safe printing call.
40  ''' </summary>
41  ''' <param name="Message">The message to print</param>
42  ''' <param name="Style">The style enum to indicate how to print</param>
43  ''' <param name="SuppressCrLf">If true, the string will be printed without a trailing cr+lf</param>
44  Private Sub Print(Message As String, Optional Style As PrintStyle = PrintStyle.Normal, Optional SuppressCrLf As Boolean = False)
45  If Output.InvokeRequired Then 'tests if call is coming from another thread
46  Output.Invoke(New Printer(AddressOf Print), Message, Style, SuppressCrLf) 'invoke the call on the correct thread
47  Else 'we're on the right thread, just print
48  Dim color As Color
49  Select Case Style
50  Case PrintStyle.Instruction
51  color = Drawing.Color.Black
52  Case PrintStyle.GalilData
53  color = Drawing.Color.Green
54  Case PrintStyle.Normal
55  color = Drawing.Color.Blue
56  Case PrintStyle.Err
57  color = Drawing.Color.Red
58  Case PrintStyle.GclibData
59  color = Drawing.Color.Magenta
60  Case Else
61  color = Drawing.Color.Blue
62  End Select
63  Output.SelectionStart = Output.Text.Length
64  Output.SelectionColor = color
65  Output.AppendText(Message)
66  If Not SuppressCrLf Then
67  Output.AppendText(vbCrLf)
68  End If
69  End If 'invoke check
70  End Sub
71 #End Region
72 
73 #Region "Threading"
74  ''' <summary>
75  ''' Delegate used to print status when the status is generated in a thread other than the UI thread.
76  ''' </summary>
77  ''' <param name="Message">Message to print</param>
78  ''' <param name="Style">Print Style</param>
79  ''' <param name="SuppressCrLf">If true, the string will be printed without a trailing cr+lf</param>
80  Private Delegate Sub Printer(Message As String, Style As PrintStyle, SuppressCrLf As Boolean)
81 
82  ''' <summary>
83  ''' Fires up the demo via the background worker thread
84  ''' </summary>
85  ''' <param name="address">The full GOpen() addresss</param>
86  ''' <remarks>Runs in UI thread</remarks>
87  Private Sub RunDemo(address As String)
88  MainToolStrip.Enabled = False
89  Output.Clear()
90  GclibBackgroundWorker.RunWorkerAsync(address)
91  End Sub
92 
93  ''' <summary>
94  ''' Runs in second thread to call the demo.
95  ''' </summary>
96  Private Sub GclibBackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles GclibBackgroundWorker.DoWork
97  Print("Running Demo with address " & e.Argument, PrintStyle.Normal)
98  TheDemo(e.Argument) 'call the actual demo code
99  End Sub
100 
101  ''' <summary>
102  ''' Runs in the main thread after the second thread returns.
103  ''' </summary>
104  Private Sub GclibBackgroundWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles GclibBackgroundWorker.RunWorkerCompleted
105  Print("Demo thread done.", PrintStyle.Normal)
106  MainToolStrip.Enabled = True
107  End Sub
108 #End Region
109 
110 #Region "Demo Code"
111 
112  ''' <summary>
113  ''' Runs in a different thread than the UI, allowing the UI to stay active
114  ''' </summary>
115  ''' <param name="address">The full GOpen() addresss</param>
116  Private Sub TheDemo(address As String)
117 
118  Dim gclib As Gclib = Nothing 'keep gclib calls all in one thread.
119  Try
120  gclib = New Gclib 'constructor can throw, so keep it in a Try block
121  Print("gclib version: ", PrintStyle.Normal, True)
122  Print(gclib.GVersion, PrintStyle.GclibData)
123 
124  '*** Uncomment below for network utilities ***
125  'Print("Controllers requesting IP addresses...")
126  'Dim macs As String() = gclib.GIpRequests()
127  'If macs.Length = 0 Then
128  ' Print("None")
129  'Else
130  ' For Each m As String In macs
131  ' Print(m, PrintStyle.GclibData)
132  ' Next
133  'End If
134 
135  'gclib.GAssign("192.168.0.42", "00:50:4c:20:01:23") 'Assign an IP to an unassigned controller
136 
137  Print("Available connections:")
138  Dim addrs As String() = gclib.GAddresses()
139  If addrs.Length = 0 Then
140  Print("None")
141  Else
142  For Each a As String In addrs
143  Print(a, PrintStyle.GclibData)
144  Next
145  End If
146 
147  Print("Opening connection to """ & address & """... ", PrintStyle.Normal, True)
148  gclib.GOpen(address)
149  Print("Connected.", PrintStyle.Normal)
150  Print(gclib.GInfo(), PrintStyle.GalilData)
151 
152  'gclib.GCommand("BN") 'send BN if IP address was assigned above
153 
154  Print("Sending ""MG TIME""", PrintStyle.Normal)
155  Print(gclib.GCommand("MG TIME", False), PrintStyle.GalilData)
156 
157  Print("Downloading Program... ", , True)
158  gclib.GProgramDownload("i=0" & vbCr & "#A;MG i{N};i=i+1;WT10;JP#A,i<10;EN", "")
159 
160  Print("Uploading Program")
161  Print(gclib.GProgramUpload(), PrintStyle.GalilData)
162 
163  Print("Blocking GMessage call")
164  gclib.GCommand("XQ")
165  System.Threading.Thread.Sleep(200) 'wait a bit to queue up some messages
166  Print(gclib.GMessage(), PrintStyle.GalilData) 'get them all in one blocking read
167 
168  Print("Downloading Program... ", , True)
169  gclib.GProgramDownload("WT 1000; MG TIME; EN", "") 'prints a messsage after 1 second
170 
171  Print("Uploading Program")
172  Print(gclib.GProgramUpload(), PrintStyle.GalilData)
173 
174  Print("Non-blocking GMessage call", , True)
175  gclib.GCommand("XQ")
176  gclib.GTimeout(0) 'set a zero timeout for a non-blocking read
177  Dim msg As String = ""
178  While (msg = "")
179  msg = gclib.GMessage()
180  Print(".", PrintStyle.Normal, True)
181  System.Threading.Thread.Sleep(20) 'do something useful here...
182  End While
183  Print("Message: ", PrintStyle.Normal, True)
184  Print(msg.Trim(), PrintStyle.GalilData)
185  gclib.GTimeout(-1) 'put the timeout back
186  'NOTE: Both GRecord and GInterrupt also have non-blocking mode with 0 timeout.
187 
188  Print("Downloading Program... ", , True)
189  gclib.GProgramDownload("WT 1000; UI 8; EN", "") 'fires an interrupt after 1 second
190 
191  Print("Uploading Program")
192  Print(gclib.GProgramUpload(), PrintStyle.GalilData)
193 
194  Print("Non-blocking GInterrupt call", , True)
195  gclib.GCommand("XQ")
196  gclib.GTimeout(0) 'set a zero timeout for a non-blocking read
197  Dim b As Byte = 0
198  While (b = 0)
199  b = gclib.GInterrupt()
200  Print(".", PrintStyle.Normal, True)
201  System.Threading.Thread.Sleep(20) 'do something useful here...
202  End While
203  Print("Byte: ", PrintStyle.Normal, True)
204  Print(b.ToString("X02"), PrintStyle.GalilData)
205  gclib.GTimeout(-1) 'put the timeout back
206 
207  Print("Getting some synchronous data records")
208  Dim DataRecord As Gclib.GDataRecord4000
209  For i = 0 To 10
210  DataRecord = gclib.GRecord(Of Gclib.GDataRecord4000)(False)
211  Print(DataRecord.sample_number.ToString() & " ", PrintStyle.GalilData, True) 'byte 4 and 5 are typically TIME counter
212  'need help accessing the data record? Contact softwaresupport@galil.com
213  System.Threading.Thread.Sleep(10)
214  Next
215  Print("")
216 
217  Print("Getting some asynchronous data records")
218  gclib.GRecordRate(10) 'set up data records every 10 ms
219  For i = 0 To 10
220  DataRecord = gclib.GRecord(Of Gclib.GDataRecord4000)(True)
221  Print(DataRecord.sample_number.ToString() & " ", PrintStyle.GalilData, True) 'byte 4 and 5 are typically TIME counter
222  'no need to delay, asynchronous mode is dispatched by the Galil's RTOS.
223  Next
224  gclib.GRecordRate(0) 'turn off data records
225  Print("")
226 
227  Print("Downloading an array... ", , True)
228  Dim array As New List(Of Double)
229  For i As Double = 0 To 9
230  array.Add(i * 2)
231  Next
232  gclib.GCommand("DA *[];DM array[10]") 'arrays must be dimensioned prior to download
233  gclib.GArrayDownload("array", array)
234 
235  Print("Ok. Uploading array")
236  array = gclib.GArrayUpload("array")
237  For Each d As Double In array
238  Print(d.ToString("F4") & " ", PrintStyle.GalilData, True)
239  Next
240  Print("")
241 
242  Print("Performing a write... ", , True)
243  gclib.GWrite("QR" & vbCr) 'QR returns the binary data record
244  Print("Ok. Reading binary data... ", , True)
245  Dim data As Byte() = gclib.GRead()
246  Print("Ok. Read " & data.Length() & " bytes.")
247 
248  Print("Preparing A axis. This could cause errors if the axis is not initialized...", , True)
249  gclib.GCommand("AB;MO;SHA") 'compound commands are possible though typically not recommended
250  Print("Ok")
251  gclib.GCommand("PRA=5000")
252  gclib.GCommand("SPA=5000")
253  Print("Profiling a move on axis A... ", , True)
254  gclib.GCommand("BGA")
255  Print("Waiting for motion to complete... ", , True)
256  gclib.GMotionComplete("A")
257  Print("done")
258  Print("Going back... ", , True)
259  gclib.GCommand("PRA=-5000")
260  gclib.GCommand("BGA")
261  gclib.GMotionComplete("A")
262  Print("done")
263  Catch ex As Exception
264  Print("Error: " & ex.Message, PrintStyle.Err)
265  Finally
266  If Not gclib Is Nothing Then
267  gclib.GClose() 'don't forget to close connections!
268  End If
269  End Try
270  End Sub
271 #End Region
272 
273 End Class
Definition: Galil.h:26
Provides a class that binds to gclib's unmanaged dll. Wraps each call and provides a more user-friend...
Definition: gclib.cs:68
GCLIB_DLL_EXPORTED GReturn GCALL GAssign(GCStringIn ip, GCStringIn mac)
Uses GUtility(), G_UTIL_GCAPS_ASSIGN or G_UTIL_ASSIGN to assign an IP address over the Ethernet to a ...
Definition: gclibo.c:70
GCLIB_DLL_EXPORTED GReturn GCALL GIpRequests(GCStringOut requests, GSize requests_len)
Uses GUtility(), G_UTIL_GCAPS_IPREQUEST or G_UTIL_IPREQUEST to provide a list of all Galil controller...
Definition: gclibo.c:106
GCLIB_DLL_EXPORTED GReturn GCALL GVersion(GCStringOut ver, GSize ver_len)
Uses GUtility(), G_UTIL_VERSION and G_UTIL_GCAPS_VERSION to provide the library and gcaps version num...
Definition: gclibo.c:29
GCLIB_DLL_EXPORTED GReturn GCALL GCommand(GCon g, GCStringIn command, GBufOut buffer, GSize buffer_len, GSize *bytes_returned)
Performs a command-and-response transaction on the connection.
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 GInfo(GCon g, GCStringOut info, GSize info_len)
Uses GUtility() and G_UTIL_INFO to provide a useful connection string.
Definition: gclibo.c:49
GCLIB_DLL_EXPORTED GReturn GCALL GAddresses(GCStringOut addresses, GSize addresses_len)
Uses GUtility(), G_UTIL_GCAPS_ADDRESSES or G_UTIL_ADDRESSES to provide a listing of all available con...
Definition: gclibo.c:54
GCLIB_DLL_EXPORTED GReturn GCALL GOpen(GCStringIn address, GCon *g)
Open a connection to a Galil Controller.
int main(int argc, char *argv[])
Main function for Commands Example.
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
GReturn message(GCon g)
Demonstrates how to receive messages from the controller and detect differences in Trace and crashed ...
Definition: message.cpp:14