gclib 2.0.9
Communications API for Galil controllers and PLCs
 
Loading...
Searching...
No Matches
Form1.cs
Go to the documentation of this file.
1
7using System;
10using System.Data;
11using System.Drawing;
12using System.Linq;
13using System.Text;
16
18{
22 public partial class MainForm : Form
23 {
24
25 #region "UI"
26
27 //form's ctor
28 public MainForm()
29 {
31 }
32
33 //Runs when form loads
34 private void MainForm_Load(object sender, EventArgs e)
35 {
36 PrintOutput("Enter a FULL GOpen() address above and click Go", PrintStyle.Instruction);
37 PrintOutput("NOTE: This demo will attempt to move Axis A", PrintStyle.Instruction);
38 }
39
40 // Opens Galil's help to show GOpen() options
41 private void HelpLabel_Click(object sender, EventArgs e)
42 {
43 //link to GOpen() documentation.
44 System.Diagnostics.Process.Start("http://www.galil.com/sw/pub/all/doc/gclib/html/gclib_8h_aef4aec8a85630eed029b7a46aea7db54.html#aef4aec8a85630eed029b7a46aea7db54");
45 }
46
47 //Runs when user clicks Go button
48 private void GoButton_Click(object sender, EventArgs e)
49 {
50 if (AddressTextBox.Text.Length == 0)
51 {
52 PrintOutput("Enter a FULL GOpen() address above and click Go", PrintStyle.Instruction);
53 return;
54 }
55 RunDemo(AddressTextBox.Text);
56 }
57
58 //Various print styles.
59 private enum PrintStyle
60 {
61 Instruction,
62 Normal,
63 GalilData,
64 GclibData,
65 Err,
66 }
67
74 private void PrintOutput(string Message, PrintStyle Style = PrintStyle.Normal, bool SuppressCrLf = false)
75 {
76 if (Output.InvokeRequired)
77 {
78 Output.Invoke(new Printer(PrintOutput), new object[] { Message, Style, SuppressCrLf });
79 }
80 else
81 {
83
84 switch (Style)
85 {
86 case PrintStyle.Instruction:
87 color = Color.Black;
88 break;
89 case PrintStyle.GalilData:
90 color = Color.Green;
91 break;
92 case PrintStyle.Normal:
93 color = Color.Blue;
94 break;
95 case PrintStyle.Err:
96 color = Color.Red;
97 break;
98 case PrintStyle.GclibData:
99 color = Color.Magenta;
100 break;
101 default:
102 color = Color.Blue;
103 break;
104 }//switch
105
106 Output.SelectionStart = Output.Text.Length;
108 Output.AppendText(Message);
109
110 if (!SuppressCrLf)
111 Output.AppendText("\r\n");
112
113 }//invoke check
114 }
115
116 #endregion
117
118 #region "Threading"
119
126 private delegate void Printer(string Message, PrintStyle Style, bool SuppressCrLf);
127
133 private void RunDemo(string address)
134 {
135 MainToolStrip.Enabled = false;
136 Output.Clear();
137 GclibBackgroundWorker.RunWorkerAsync(address);
138 }
139
140
144 private void GclibBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
145 {
146 PrintOutput("Running Demo with address " + e.Argument, PrintStyle.Normal);
147 TheDemo((string)e.Argument); //call the actual demo code
148 }
149
153 private void GclibBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
154 {
155 PrintOutput("Demo thread done.", PrintStyle.Normal);
157 }
158
159 #endregion
160
161 #region "Demo Code"
162
163 private void TheDemo(string address)
164 {
165 gclib gclib = null;
166 try
167 {
168 gclib = new gclib(); //constructor can throw, so keep it in a Try block
169
170 PrintOutput("gclib version: ", PrintStyle.Normal, true);
171 PrintOutput(gclib.GVersion(), PrintStyle.GclibData);
172
173 //*** Uncomment below for network utilities ***
174 //PrintOutput("Controllers requesting IP addresses...");
175 //string[] macs = gclib.GIpRequests();
176 //if (macs.Length == 0)
177 // PrintOutput("None");
178 //else
179 // foreach (string m in macs)
180 // PrintOutput(m);
181
182 //gclib.GAssign("192.168.0.42", "00:50:4c:20:01:23"); //Assign an IP to an unassigned controller
183
184 PrintOutput("Available connections:");
185 string[] addrs = gclib.GAddresses();
186 if (addrs.Length == 0)
187 {
188 PrintOutput("None");
189 }
190 else
191 {
192 foreach (string a in addrs)
193 {
194 PrintOutput(a, PrintStyle.GclibData);
195 }
196 }
197
198 PrintOutput("Opening connection to \"" + address + "\"... ", PrintStyle.Normal, true);
200 PrintOutput("Connected.", PrintStyle.Normal);
201 PrintOutput(gclib.GInfo(), PrintStyle.GalilData);
202
203 // gclib.GCommand("BN"); //send BN if IP address was assigned above
204
205 PrintOutput("Sending \"MG TIME\"", PrintStyle.Normal);
206 PrintOutput(gclib.GCommand("MG TIME", false), PrintStyle.GalilData);
207
208 PrintOutput("Downloading Program... ", PrintStyle.Normal, true);
209 gclib.GProgramDownload("i=0\r#A;MG i{N};i=i+1;WT10;JP#A,i<10;EN", "");
210
211 PrintOutput("Uploading Program");
212 PrintOutput(gclib.GProgramUpload(), PrintStyle.GalilData);
213
214 PrintOutput("Blocking GMessage call");
215 gclib.GCommand("XQ");
216 System.Threading.Thread.Sleep(200);
217 //wait a bit to queue up some messages
218 PrintOutput(gclib.GMessage(), PrintStyle.GalilData);
219 //get them all in one blocking read
220
221 PrintOutput("Downloading Program... ", PrintStyle.Normal, true);
222 gclib.GProgramDownload("WT 1000; MG TIME; EN", "");
223 //prints a messsage after 1 second
224
225 PrintOutput("Uploading Program");
226 PrintOutput(gclib.GProgramUpload(), PrintStyle.GalilData);
227
228 PrintOutput("Non-blocking GMessage call", PrintStyle.Normal, true);
229 gclib.GCommand("XQ");
230 gclib.GTimeout(0);
231 //set a zero timeout for a non-blocking read
232 string msg = "";
233 while ((string.IsNullOrEmpty(msg)))
234 {
235 msg = gclib.GMessage();
236 PrintOutput(".", PrintStyle.Normal, true);
237 System.Threading.Thread.Sleep(20);
238 //do something useful here...
239 }
240 PrintOutput("Message: ", PrintStyle.Normal, true);
241 PrintOutput(msg.Trim(), PrintStyle.GalilData);
242 gclib.GTimeout(-1);
243 //put the timeout back
244 //NOTE: Both GRecord and GInterrupt also have non-blocking mode with 0 timeout.
245
246 PrintOutput("Downloading Program... ", PrintStyle.Normal, true);
247 gclib.GProgramDownload("WT 1000; UI 8; EN", "");
248 //fires an interrupt after 1 second
249
250 PrintOutput("Uploading Program");
251 PrintOutput(gclib.GProgramUpload(), PrintStyle.GalilData);
252
253 PrintOutput("Non-blocking GInterrupt call", PrintStyle.Normal, true);
254 gclib.GCommand("XQ");
255 gclib.GTimeout(0);
256 //set a zero timeout for a non-blocking read
257 byte b = 0;
258 while ((b == 0))
259 {
260 b = gclib.GInterrupt();
261 PrintOutput(".", PrintStyle.Normal, true);
262 System.Threading.Thread.Sleep(20);
263 //do something useful here...
264 }
265 PrintOutput("Byte: ", PrintStyle.Normal, true);
266 PrintOutput(b.ToString("X02"), PrintStyle.GalilData);
267 gclib.GTimeout(-1);
268 //put the timeout back
269
270 PrintOutput("Getting some synchronous data records");
272 for (int i = 0; i <= 10; i++)
273 {
274 DataRecord = gclib.GRecord<gclib.GDataRecord4000>(false);
275 PrintOutput(DataRecord.sample_number + " ", PrintStyle.GalilData, true);
276 //need help accessing the data record? Contact softwaresupport@galil.com
277 System.Threading.Thread.Sleep(10);
278 }
279 PrintOutput("");
280
281 PrintOutput("Getting some asynchronous data records");
282 gclib.GRecordRate(10);
283 //set up data records every 10 ms
284 for (int i = 0; i <= 10; i++)
285 {
286 DataRecord = gclib.GRecord<gclib.GDataRecord4000>(true);
287 PrintOutput(DataRecord.sample_number + " ", PrintStyle.GalilData, true);
288 //no need to delay, asynchronous mode is dispatched by the Galil's RTOS.
289 }
291 //turn off data records
292 PrintOutput("");
293
294 PrintOutput("Downloading an array... ", PrintStyle.Normal, true);
296 for (double i = 0; i <= 9; i++)
297 {
298 array.Add(i * 2);
299 }
300 gclib.GCommand("DA *[];DM array[10]");
301 //arrays must be dimensioned prior to download
302 gclib.GArrayDownload("array", ref array);
303
304 PrintOutput("Ok. Uploading array");
305 array = gclib.GArrayUpload("array");
306 foreach (double d in array)
307 {
308 PrintOutput(d.ToString("F4") + " ", PrintStyle.GalilData, true);
309 }
310 PrintOutput("");
311
312 PrintOutput("Performing a write... ", PrintStyle.Normal, true);
313 gclib.GWrite("QR\r");
314 //QR returns the binary data record
315 PrintOutput("Ok. Reading binary data... ", PrintStyle.Normal, true);
316 byte[] data = gclib.GRead();
317 PrintOutput("Ok. Read " + data.Length + " bytes.");
318
319 PrintOutput("Preparing A axis. This could cause errors if the axis is not initialized...", PrintStyle.Normal, true);
320 gclib.GCommand("AB;MO;SHA");
321 //compound commands are possible though typically not recommended
322 PrintOutput("Ok");
323 gclib.GCommand("PRA=5000");
324 gclib.GCommand("SPA=5000");
325 PrintOutput("Profiling a move on axis A... ", PrintStyle.Normal, true);
326 gclib.GCommand("BGA");
327 PrintOutput("Waiting for motion to complete... ", PrintStyle.Normal, true);
329 PrintOutput("done");
330 PrintOutput("Going back... ", PrintStyle.Normal, true);
331 gclib.GCommand("PRA=-5000");
332 gclib.GCommand("BGA");
334 PrintOutput("done");
335 }
336 catch (Exception ex)
337 {
338 PrintOutput("Error: " + ex.Message, PrintStyle.Err);
339 }
340 finally
341 {
342 if (gclib != null)
343 gclib.GClose(); //don't forget to close the connection
344 }
345 }
346 #endregion
347 }
348}
Demonstrates using gclib in a Windows Form, including using a second thread to free the GUI.
Definition Form1.cs:23
string GProgramUpload()
Allows uploading of a DMC program to a string.
Definition gclib.cs:499
string GVersion()
Used to get the gclib version.
Definition gclib.cs:614
void GOpen(string address)
Used to open a connection to Galil hardware.
Definition gclib.cs:445
void GWrite(string buffer)
Performs a write on the connection.
Definition gclib.cs:635
List< double > GArrayUpload(string array_name, Int16 first=-1, Int16 last=-1)
Uploads array data from the controller's array table.
Definition gclib.cs:173
string GCommand(string Command, bool Trim=true)
Used for command-and-response transactions.
Definition gclib.cs:257
void GArrayDownload(string array_name, ref List< double > data, Int16 first=-1, Int16 last=-1)
Downloads array data to a pre-dimensioned array in the controller's array table.
Definition gclib.cs:126
string GInfo()
Provides a useful connection string.
Definition gclib.cs:344
void GProgramDownload(string program, string preprocessor="")
Allows downloading of a DMC program from a string buffer.
Definition gclib.cs:465
byte GInterrupt()
Provides access to PCI and UDP interrupts from the controller.
Definition gclib.cs:364
string GMessage()
Provides access to unsolicited messages.
Definition gclib.cs:407
void GMotionComplete(string axes)
Blocking call that returns once all axes specified have completed their motion.
Definition gclib.cs:428
void GTimeout(Int16 timeout_ms)
Set the timeout of communication transactions. Use -1 to set the original timeout from GOpen().
Definition gclib.cs:604
void GClose()
Used to close a connection to Galil hardware.
Definition gclib.cs:239
void GRecordRate(double period_ms)
Sets the asynchronous data record to a user-specified period via DR.
Definition gclib.cs:588
string[] GAddresses()
Return a string array of available connection addresses.
Definition gclib.cs:102
byte[] GRead()
Performs a read on the connection.
Definition gclib.cs:536
Provides a class that binds to gclib's unmanaged dll. Wraps each call and provides a more user-friend...
Definition gclib.cs:68
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition examples.h:33
GReturn vector(GCon g, char *file)
Puts controller into Vector Mode and accepts a file defining vector points.
Definition vector.cpp:36
Data record struct for DMC-4000 controllers, including 4000, 4200, 4103, and 500x0.
Definition gclib.cs:919