//Note to self: currently not working public void Main(string address) { gclib gclib = new gclib(); try { //execute any program as dictated by all buttons and given script //i plan to change the way we do this abit. i want to establish a connection. check for it // then just use this Main function to execute the intention of the comment 2 lines above this //calls to gclib should be in a try-catch textBox1.AppendText("GVersion: " + gclib.GVersion() + "\n"); gclib.GOpen(address + " -d"); //Set an appropriate IP address here textBox1.AppendText("GInfo: " + gclib.GInfo() + "\n"); textBox1.AppendText("GCommand: " + gclib.GCommand("MG TIME") + "\n"); } catch (Exception ex) { textBox1.AppendText("ERROR: " + ex.Message); } finally { gclib.GClose(); //Don't forget to close! } }
private void ConnectStripButton_Click(object sender, EventArgs e) { //only checking for empty string, otherwise we enter the Main program. //because invalid address will itself trigger an exception, exiting the Main program. if (AddressTextBox.Text.Length == 0) { PrintOutput(textBox1, "Cannot be Null. Enter a FULL GOpen() address above and click Go", PrintStyle.Instruction); return; } try { string address = AddressTextBox.Text; PrintOutput(textBox1, "Opening connection to \"" + address + "\"... ", PrintStyle.Normal, true); gclib.GOpen(address); PrintOutput(textBox1, "CONNECTED!", PrintStyle.Normal); PrintOutput(textBox1, gclib.GInfo(), PrintStyle.GalilData); //Now that we've successfully connected, here we modify how user //can interact with the GUI: DisconnectStripButton.Enabled = true; ConnectStripButton.Enabled = false; AddressTextBox.Enabled = false; configBox.Enabled = true; this.analyzer.Open(); // meaning, we activate analyzer as soon as connect button is pressed. // Call this multiple times, as a warm up. // Previous problem: there would be nothing (or problems) to read from Gclib.message() if we don't call the preceding Gclib commands multiple times cur_abs_pos(abs_position); cur_abs_pos(abs_position); cur_abs_pos(abs_position); return; } catch (Exception ex) { PrintOutput(textBox1, "ERROR: " + ex.Message, PrintStyle.Instruction); PrintOutput(textBox1, "Invalid address. Re-enter a FULL GOpen() address above and click Go", PrintStyle.Instruction); return; } //Given previously, because it was meant to execute demo only ONCE, when the connect button is clicked. //RunDemo(AddressTextBox.Text); }
private void TheDemo(string address) { gclib gclib = null; try { gclib = new gclib(); //constructor can throw, so keep it in a Try block PrintOutput("gclib version: ", PrintStyle.Normal, true); PrintOutput(gclib.GVersion(), PrintStyle.GclibData); //*** Uncomment below for network utilities *** //PrintOutput("Controllers requesting IP addresses..."); //string[] macs = gclib.GIpRequests(); //if (macs.Length == 0) // PrintOutput("None"); //else // foreach (string m in macs) // PrintOutput(m); //gclib.GAssign("192.168.0.42", "00:50:4c:20:01:23"); //Assign an IP to an unassigned controller PrintOutput("Available connections:"); string[] addrs = gclib.GAddresses(); if (addrs.Length == 0) { PrintOutput("None"); } else { foreach (string a in addrs) { PrintOutput(a, PrintStyle.GclibData); } } PrintOutput("Opening connection to \"" + address + "\"... ", PrintStyle.Normal, true); gclib.GOpen(address); PrintOutput("Connected.", PrintStyle.Normal); PrintOutput(gclib.GInfo(), PrintStyle.GalilData); // gclib.GCommand("BN"); //send BN if IP address was assigned above PrintOutput("Sending \"MG TIME\"", PrintStyle.Normal); PrintOutput(gclib.GCommand("MG TIME", false), PrintStyle.GalilData); PrintOutput("Downloading Program... ", PrintStyle.Normal, true); gclib.GProgramDownload("i=0\r#A;MG i{N};i=i+1;WT10;JP#A,i<10;EN", ""); PrintOutput("Uploading Program"); PrintOutput(gclib.GProgramUpload(), PrintStyle.GalilData); PrintOutput("Blocking GMessage call"); gclib.GCommand("XQ"); System.Threading.Thread.Sleep(200); //wait a bit to queue up some messages PrintOutput(gclib.GMessage(), PrintStyle.GalilData); //get them all in one blocking read PrintOutput("Downloading Program... ", PrintStyle.Normal, true); gclib.GProgramDownload("WT 1000; MG TIME; EN", ""); //prints a messsage after 1 second PrintOutput("Uploading Program"); PrintOutput(gclib.GProgramUpload(), PrintStyle.GalilData); PrintOutput("Non-blocking GMessage call", PrintStyle.Normal, true); gclib.GCommand("XQ"); gclib.GTimeout(0); //set a zero timeout for a non-blocking read string msg = ""; while ((string.IsNullOrEmpty(msg))) { msg = gclib.GMessage(); PrintOutput(".", PrintStyle.Normal, true); System.Threading.Thread.Sleep(20); //do something useful here... } PrintOutput("Message: ", PrintStyle.Normal, true); PrintOutput(msg.Trim(), PrintStyle.GalilData); gclib.GTimeout(-1); //put the timeout back //NOTE: Both GRecord and GInterrupt also have non-blocking mode with 0 timeout. PrintOutput("Downloading Program... ", PrintStyle.Normal, true); gclib.GProgramDownload("WT 1000; UI 8; EN", ""); //fires an interrupt after 1 second PrintOutput("Uploading Program"); PrintOutput(gclib.GProgramUpload(), PrintStyle.GalilData); PrintOutput("Non-blocking GInterrupt call", PrintStyle.Normal, true); gclib.GCommand("XQ"); gclib.GTimeout(0); //set a zero timeout for a non-blocking read byte b = 0; while ((b == 0)) { b = gclib.GInterrupt(); PrintOutput(".", PrintStyle.Normal, true); System.Threading.Thread.Sleep(20); //do something useful here... } PrintOutput("Byte: ", PrintStyle.Normal, true); PrintOutput(b.ToString("X02"), PrintStyle.GalilData); gclib.GTimeout(-1); //put the timeout back PrintOutput("Getting some synchronous data records"); byte[] DataRecord = null; for (int i = 0; i <= 10; i++) { DataRecord = gclib.GRecord(false); PrintOutput(BitConverter.ToUInt16(DataRecord, 4).ToString() + " ", PrintStyle.GalilData, true); //byte 4 and 5 are typically TIME counter //need help accessing the data record? Contact [email protected] System.Threading.Thread.Sleep(10); } PrintOutput(""); PrintOutput("Getting some asynchronous data records"); gclib.GRecordRate(10); //set up data records every 10 ms for (int i = 0; i <= 10; i++) { DataRecord = gclib.GRecord(true); PrintOutput(BitConverter.ToUInt16(DataRecord, 4).ToString() + " ", PrintStyle.GalilData, true); //byte 4 and 5 are typically TIME counter //no need to delay, asynchronous mode is dispatched by the Galil's RTOS. } gclib.GRecordRate(0); //turn off data records PrintOutput(""); PrintOutput("Downloading an array... ", PrintStyle.Normal, true); List <double> array = new List <double>(); for (double i = 0; i <= 9; i++) { array.Add(i * 2); } gclib.GCommand("DA *[];DM array[10]"); //arrays must be dimensioned prior to download gclib.GArrayDownload("array", ref array); PrintOutput("Ok. Uploading array"); array = gclib.GArrayUpload("array"); foreach (double d in array) { PrintOutput(d.ToString("F4") + " ", PrintStyle.GalilData, true); } PrintOutput(""); PrintOutput("Performing a write... ", PrintStyle.Normal, true); gclib.GWrite("QR\r"); //QR returns the binary data record PrintOutput("Ok. Reading binary data... ", PrintStyle.Normal, true); byte[] data = gclib.GRead(); PrintOutput("Ok. Read " + data.Length + " bytes."); PrintOutput("Preparing A axis. This could cause errors if the axis is not initialized...", PrintStyle.Normal, true); gclib.GCommand("AB;MO;SHA"); //compound commands are possible though typically not recommended PrintOutput("Ok"); gclib.GCommand("PRA=5000"); gclib.GCommand("SPA=5000"); PrintOutput("Profiling a move on axis A... ", PrintStyle.Normal, true); gclib.GCommand("BGA"); PrintOutput("Waiting for motion to complete... ", PrintStyle.Normal, true); gclib.GMotionComplete("A"); PrintOutput("done"); PrintOutput("Going back... ", PrintStyle.Normal, true); gclib.GCommand("PRA=-5000"); gclib.GCommand("BGA"); gclib.GMotionComplete("A"); PrintOutput("done"); } catch (Exception ex) { PrintOutput("Error: " + ex.Message, PrintStyle.Err); } finally { if (gclib != null) { gclib.GClose(); //don't forget to close the connection } } }