コード例 #1
0
        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
                }
            }
        }