//Basic PA movement, while printing output to TextBox1: private void runAbsoluteMoveCommand(string axis, int distance_units, int speed) { try { form.printTextBox1("Preparing " + axis + " axis for PA movement. This could cause errors if the axis is not initialized..."); gclib.GCommand("AB;MO;SH" + axis); //compound commands are possible though typically not recommended form.printTextBox1("Ok"); gclib.GCommand("PA" + axis + "=" + distance_units); //might implement speed control parameter in future gclib.GCommand("SP" + axis + "=" + speed); form.printTextBox1("Profiling a move on axis" + axis + "... "); gclib.GCommand("BG" + axis); form.printTextBox1("Waiting for motion to complete... "); gclib.GMotionComplete(axis); form.printTextBox1("done"); //update absolute position and display in textBox2: //cur_abs_pos(abs_position); // as stated in Master, this is commented as it has been identified as the source of neg move problem } catch (Exception ex) { form.printTextBox1("ERROR in runAbsoluteMoveCommand on axis " + axis + ": " + ex.Message, Form1.PrintStyle.Instruction); } }
//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 bool InitPowerLand(string address) //初始化powerland { gclib gclib = null; try { gclib = new gclib(); PrintOutput("gclib version: ", PrintStyle.Normal, true); PrintOutput(gclib.GVersion(), PrintStyle.GclibData); PrintOutput("Opening connection to \"" + address + "\"... ", PrintStyle.Normal, true); gclib.GOpen(address); PrintOutput("Connected.", PrintStyle.Normal); PrintOutput("Setting AZ...", PrintStyle.Normal); gclib.GCommand(CommandCreate.AZCommand("")); PrintOutput("Complete.", PrintStyle.Normal); Commondata.GalilControler = new gclib(); PrintOutput("Connectting to Controler...", PrintStyle.Normal); Commondata.GalilControler.GOpen(Commondata.DmcC640Ip); PrintOutput("Connected.Version is " + Commondata.GalilControler.GInfo(), PrintStyle.Normal); PrintOutput("Now TP position is " + Commondata.GalilControler.GCommand("TP"), PrintStyle.Normal); Commondata.IsInit = true; return(true); } catch (Exception ex) { MessageBox.Show("PowerLand初始化失败!" + ex.Message); //MessageBox.Show(e.Message); return(false); } }
//Retrieve absolute position of gantry (Axes a,b, c): //Note that retrieval can only be done when the gantry is no longer moving. // 19/12/19: this is a function I did not modify. private void cur_abs_pos(int[] abs_position) { try { PrintOutput(textBox1, "Updating absolute position.. ", PrintStyle.Normal, true); PrintOutput(textBox2, "Updating absolute position.. ", PrintStyle.Normal, true); /* * * // Apparently GCommand would not allow me to display the TD returned result in GMessage * // So I used these 2 lines instead: * // Flush out outdated value: * gclib.GProgramDownload("TD", ""); * gclib.GCommand("XQ"); * string td_value = gclib.GMessage(); * * // Get updated value: * gclib.GProgramDownload("TD", ""); * gclib.GCommand("XQ"); * td_value = gclib.GMessage(); * * var watch = Stopwatch.StartNew(); * while (!Regex.IsMatch(td_value, @"[0-9]+,\s[0-9]+,\s[0-9]+")) * { * gclib.GProgramDownload("TD", ""); * gclib.GCommand("XQ"); * td_value = gclib.GMessage(); * * if(watch.ElapsedMilliseconds > 3000) * { * watch.Stop(); * throw new TimeoutException("TD command output result format incorrect after 3sec conversion!"); * } * } * watch.Stop(); * //throw new System.IndexOutOfRangeException("TD command invalid return..Ignore"); * * // Needed to extract substring because for some reason there is another string being outputted: * td_value = td_value.Substring(0, td_value.IndexOf(Environment.NewLine)); */ // V2: found a better way to do the above, avoiding all the bugs... string td_value = gclib.GCommand("PA?,?,?"); PrintOutput(textBox1, "Converting..", PrintStyle.Normal, true); // 231019: Note that the error messages (tagged with "TD" and "text to int") are caused by this section: // I suspect that it is because the line td_value = gclib.GMessage returns messages not intended, i.e. commas disappeared? // Might be benign: can ignore for now. // Here onwards we update the variable abs_position: // this function only updates X, Y coordinates coor_string_to_intArr(td_value, abs_position); // To update Z coordinate (axis-c), we do so manually: // Note that IndexOf in this case has 2 args and works like such: // 1st arg = char to look for in str, 2nd arg = index in str to begin searching int index_2nd_comma = td_value.IndexOf(',', td_value.IndexOf(',') + 2); string temp = td_value.Substring(index_2nd_comma); int temp_abs = abs_position[2]; if (!(Int32.TryParse(temp, out abs_position[2]))) { //if conversion failed abs_position[2] = temp_abs; } PrintOutput(textBox1, "Done!", PrintStyle.Normal, true); PrintOutput(textBox2, td_value, PrintStyle.GalilData); } catch (Exception ex) { PrintOutput(textBox1, "ERROR in TD command (absolute position): " + ex.Message, PrintStyle.Instruction); } }
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 } } }