2 ''' Demonstrates
using gclib in a Windows Form, including
using a second thread to free the GUI.
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)
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:
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)
26 RunDemo(AddressTextBox.Text)
29 'Various print styles.
30 Private Enum PrintStyle
39 ''' Thread safe printing call.
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
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
57 color = Drawing.Color.Red
58 Case PrintStyle.GclibData
59 color = Drawing.Color.Magenta
61 color = Drawing.Color.Blue
63 Output.SelectionStart = Output.Text.Length
64 Output.SelectionColor = color
65 Output.AppendText(Message)
66 If Not SuppressCrLf Then
67 Output.AppendText(vbCrLf)
75 ''' Delegate used to print status when the status is generated in a thread other than the UI thread.
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)
83 ''' Fires up the demo via the background worker thread
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
90 GclibBackgroundWorker.RunWorkerAsync(address)
94 ''' Runs in second thread to call the demo.
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
102 ''' Runs in the
main thread after the second thread returns.
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
113 ''' Runs in a different thread than the UI, allowing the UI to stay active
115 ''' <param name=
"address">The full
GOpen() addresss</param>
116 Private Sub TheDemo(address As String)
118 Dim
gclib As Gclib = Nothing 'keep
gclib calls all in one thread.
120 gclib = New Gclib 'constructor can throw, so keep it in a Try block
121 Print("
gclib version: ", PrintStyle.Normal, True)
124 '*** Uncomment below for network utilities ***
125 'Print("Controllers requesting IP addresses...")
127 'If macs.Length = 0 Then
130 ' For Each m As String In macs
131 ' Print(m, PrintStyle.GclibData)
135 '
gclib.
GAssign("192.168.0.42", "00:50:4c:20:01:23") 'Assign an IP to an unassigned controller
137 Print("Available connections:")
139 If addrs.Length = 0 Then
142 For Each a As String In addrs
143 Print(a, PrintStyle.GclibData)
147 Print("Opening connection to """ & address & """... ", PrintStyle.Normal, True)
149 Print("Connected.", PrintStyle.Normal)
152 '
gclib.
GCommand("BN") 'send BN if IP address was assigned above
154 Print("Sending ""MG TIME""", PrintStyle.Normal)
155 Print(
gclib.
GCommand("MG TIME", False), PrintStyle.GalilData)
157 Print("Downloading Program... ", , True)
160 Print("Uploading Program")
161 Print(gclib.GProgramUpload(), PrintStyle.GalilData)
163 Print("Blocking GMessage call")
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
168 Print("Downloading Program... ", , True)
169 gclib.GProgramDownload("WT 1000; MG TIME; EN", "") 'prints a messsage after 1 second
171 Print("Uploading Program")
172 Print(gclib.GProgramUpload(), PrintStyle.GalilData)
174 Print("Non-blocking GMessage call", , True)
176 gclib.GTimeout(0) 'set a zero timeout for a non-blocking read
177 Dim msg As String = ""
179 msg = gclib.GMessage()
180 Print(".", PrintStyle.Normal, True)
181 System.Threading.Thread.Sleep(20) 'do something useful here...
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.
188 Print("Downloading Program... ", , True)
189 gclib.GProgramDownload("WT 1000; UI 8; EN", "") 'fires an interrupt after 1 second
191 Print("Uploading Program")
192 Print(gclib.GProgramUpload(), PrintStyle.GalilData)
194 Print("Non-blocking GInterrupt call", , True)
196 gclib.GTimeout(0) 'set a zero timeout for a non-blocking read
199 b = gclib.GInterrupt()
200 Print(".", PrintStyle.Normal, True)
201 System.Threading.Thread.Sleep(20) 'do something useful here...
203 Print("Byte: ", PrintStyle.Normal, True)
204 Print(b.ToString("X02"), PrintStyle.GalilData)
205 gclib.GTimeout(-1) 'put the timeout back
207 Print("Getting some synchronous data records")
208 Dim DataRecord As Gclib.GDataRecord4000
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)
217 Print("Getting some asynchronous data records")
218 gclib.GRecordRate(10) 'set up data records every 10 ms
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.
224 gclib.GRecordRate(0) 'turn off data records
227 Print("Downloading an array... ", , True)
228 Dim array As New List(Of Double)
229 For i As Double = 0 To 9
232 gclib.GCommand("DA *[];DM array[10]") 'arrays must be dimensioned prior to download
233 gclib.GArrayDownload("array", array)
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)
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.")
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
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")
258 Print("Going back... ", , True)
259 gclib.GCommand("PRA=-5000")
260 gclib.GCommand("BGA")
261 gclib.GMotionComplete("A")
263 Catch ex As Exception
264 Print("Error: " & ex.Message, PrintStyle.Err)
266 If Not gclib Is Nothing Then
267 gclib.GClose() 'don't forget to close connections!
Provides a class that binds to gclib's unmanaged dll. Wraps each call and provides a more user-friend...
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 ...
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...
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...
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.
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...
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.
GReturn message(GCon g)
Demonstrates how to receive messages from the controller and detect differences in Trace and crashed ...