| gclib 2.0.7 Communications API for Galil controllers and PLCs |

Form1.cs
Go to the documentation of this file.
1
7 using System;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Data;
11 using System.Drawing;
12 using System.Linq;
13 using System.Text;
14 using System.Threading.Tasks;
15 using System.Windows.Forms;
16
17 namespace gclib_example
18 {
22 public partial class MainForm : Form
23 {
24
25 #region "UI"
26
27 //form's ctor
28 public MainForm()
29 {
30 InitializeComponent();
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 {
82 Color color;
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;
107 Output.SelectionColor = color;
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);
156 MainToolStrip.Enabled = true;
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);
199 gclib.GOpen(address);
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");
271 gclib.GDataRecord4000 DataRecord;
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 }
290 gclib.GRecordRate(0);
291 //turn off data records
292 PrintOutput("");
293
294 PrintOutput("Downloading an array... ", PrintStyle.Normal, true);
295 List<double> array = new List<double>();
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);
328 gclib.GMotionComplete("A");
329 PrintOutput("done");
330 PrintOutput("Going back... ", PrintStyle.Normal, true);
331 gclib.GCommand("PRA=-5000");
332 gclib.GCommand("BGA");
333 gclib.GMotionComplete("A");
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 }
gclib_example.MainForm
Demonstrates using gclib in a Windows Form, including using a second thread to free the GUI.
Definition: Form1.cs:23
gclib.GProgramUpload
string GProgramUpload()
Allows uploading of a DMC program to a string.
Definition: gclib.cs:499
gclib.GVersion
string GVersion()
Used to get the gclib version.
Definition: gclib.cs:614
gclib.GOpen
void GOpen(string address)
Used to open a connection to Galil hardware.
Definition: gclib.cs:445
gclib.GWrite
void GWrite(string buffer)
Performs a write on the connection.
Definition: gclib.cs:635
gclib.GCommand
string GCommand(string Command, bool Trim=true)
Used for command-and-response transactions.
Definition: gclib.cs:257
gclib.GArrayDownload
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
gclib.GInfo
string GInfo()
Provides a useful connection string.
Definition: gclib.cs:344
gclib.GProgramDownload
void GProgramDownload(string program, string preprocessor="")
Allows downloading of a DMC program from a string buffer.
Definition: gclib.cs:465
gclib.GAddresses
string[] GAddresses()
Return a string array of available connection addresses.
Definition: gclib.cs:102
gclib.GInterrupt
byte GInterrupt()
Provides access to PCI and UDP interrupts from the controller.
Definition: gclib.cs:364
gclib.GMessage
string GMessage()
Provides access to unsolicited messages.
Definition: gclib.cs:407
gclib.GRead
byte[] GRead()
Performs a read on the connection.
Definition: gclib.cs:536
gclib.GMotionComplete
void GMotionComplete(string axes)
Blocking call that returns once all axes specified have completed their motion.
Definition: gclib.cs:428
gclib.GTimeout
void GTimeout(Int16 timeout_ms)
Set the timeout of communication transactions. Use -1 to set the original timeout from GOpen().
Definition: gclib.cs:604
gclib.GArrayUpload
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
gclib.GClose
void GClose()
Used to close a connection to Galil hardware.
Definition: gclib.cs:239
gclib.GRecordRate
void GRecordRate(double period_ms)
Sets the asynchronous data record to a user-specified period via DR.
Definition: gclib.cs:588
gclib
Provides a class that binds to gclib's unmanaged dll. Wraps each call and provides a more user-friend...
Definition: gclib.cs:68
e
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
gclib_example
Definition: Form1.cs:18
gclib.GDataRecord4000
Data record struct for DMC-4000 controllers, including 4000, 4200, 4103, and 500x0.
Definition: gclib.cs:919