Ejemplo n.º 1
0
        /// <summary>
        /// 1 运行 2待机 3报警 4急停
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private int GetStatus(ushort handle, out string error)
        {
            error = "";
            Focas1.ODBST statbuf = new Focas1.ODBST();
            short        nRet    = Focas1.cnc_statinfo(handle, statbuf);

            if (nRet != Focas1.EW_OK)
            {
                error = $"读取错误!错误代号[{nRet}]";
                return(-1);
            }

            int iState = 0;

            if (statbuf.emergency > 0) //急停 1 : EMerGency 2 : ReSET
            {
                iState = 4;
            }
            else if (statbuf.alarm > 0) //故障 3报警  1 : ALarM
            {
                iState = 3;
            }
            //运行中 1 : STOP 2 : HOLD 3 : STaRT  4 : MSTR(during retraction and re-positioning of tool retraction and recovery, and operation of JOG MDI)
            else if (statbuf.run > 0)
            {
                iState = 1;
            }
            else //待机
            {
                iState = 2;
            }

            return(iState);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read Machine Status Data (MachineMode, MachineStatus, MachineAlarmStatus)
        /// </summary>
        private void CallStatInfo()
        {
            try
            {
                Focas1.ODBST odbSt = new Focas1.ODBST();

                focasReturn = Focas1.cnc_statinfo(fanucHandle, odbSt);

                if (focasReturn == EW_OK)
                {
                    base.MODE   = GetDescription(machineType, "MODE", odbSt.aut.ToString());
                    base.STATUS = GetDescription(machineType, "STATUS", odbSt.run.ToString());
                    base.ALARM  = GetDescription(machineType, "ALARM", odbSt.alarm.ToString());

                    LogHandler.WriteLog(base.division, string.Format("{0} :: CallStatInfo() Success :: MODE = {1}", this.ToString(), base.MODE));
                    LogHandler.WriteLog(base.division, string.Format("{0} :: CallStatInfo() Success :: STATUS = {1}", this.ToString(), base.STATUS));
                    LogHandler.WriteLog(base.division, string.Format("{0} :: CallStatInfo() Success :: ALARM = {1}", this.ToString(), base.ALARM));
                }
                else if (focasReturn == EW_SOCKET)
                {
                    base.isConnect = false;
                    LogHandler.WriteLog(base.division, string.Format("{0} :: CallStatInfo() Fail :: SocketError", this.ToString()));
                }
                else
                {
                    LogHandler.WriteLog(base.division, string.Format("{0} :: CallStatInfo() Fail :: ReturnCode = {1}", this.ToString(), focasReturn));
                }
            }
            catch (Exception ex)
            {
                LogHandler.WriteLog(base.division, string.Format("{0} :: CallStatInfo() Exception :: Message = {1}", this.ToString(), ex.Message));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获得机床相对坐标
        /// </summary>
        public double GetRelativeCorX(ushort Flibhndl)                  //获得相对X轴
        {
            Focas1.ODBST   odbst   = new Focas1.ODBST();
            Focas1.ODBAXIS odbaxis = new Focas1.ODBAXIS();

            short  ret2       = Focas1.cnc_relative2(Flibhndl, 1, 16, odbaxis);
            string relative2X = odbaxis.data[0].ToString();
            double x          = Convert.ToDouble(relative2X) / 1000;

            return(x);
        }
Ejemplo n.º 4
0
        public static void connectToFanucDevice(string ip, string port, int timeout)
        {
            int ret;
            int status;

            Focas1.ODBST fanbuf; // CNC Status

            ret = Focas1.cnc_allclibhndl3(ip, Convert.ToUInt16(port), timeout, out handleFanuc);
            if (ret != Focas1.EW_OK)
            {
                Console.WriteLine("Fanuc lathe connection error£¡");
                return;
            }

            //coordination of the arm
            Focas1.ODBAXIS odbaxis = new Focas1.ODBAXIS();
            for (short i = 0; i < 3; i++)
            {
                ret = Focas1.cnc_machine(handleFanuc, (short)(i + 1), 8, odbaxis);
                Console.WriteLine(odbaxis.data[0] * Math.Pow(10, -4));
            }

            fanbuf = new Focas1.ODBST();
            ret    = Focas1.cnc_statinfo(handleFanuc, fanbuf);
            if (ret == Focas1.EW_OK)
            {
                status = fanbuf.run;

                switch (status)
                {
                case 0:
                    Console.WriteLine("Fanuc lathe in shutdown mode£¡");
                    break;

                case 1:
                    Console.WriteLine("Fanuc lathe in standby mode£¡");
                    break;

                case 2:
                    Console.WriteLine("Fanuc lathe in hold mode£¡");
                    break;

                case 3:
                    Console.WriteLine("Fanuc lathe in start mode£¡");
                    break;

                case 4:
                    Console.WriteLine("Fanuc lathe in manual mode, retraction and re-positioning of tools");
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        private void button1_Click(object sender, EventArgs e)
        {
            Fanuc.ODBST odbst = new Focas1.ODBST();
            int         ret   = Fanuc.cnc_statinfo(Fanuc.h, odbst);

            if (ret == Fanuc.EW_OK)
            {
            }
            else
            {
                MessageBox.Show(ret + "");
            }
        }
Ejemplo n.º 6
0
        //获取状态信息
        #region cnc_statinfo
        short IMachineFanuc.GetMachineStateInfo(short[] stateInfo)
        {
            Focas1.ODBST odbst_StateInfo = new Focas1.ODBST();             //状态信息
            short        ret             = Focas1.cnc_statinfo(mFlibhndl, odbst_StateInfo);

            stateInfo[0] = odbst_StateInfo.tmmode;     //* T/M mode
            stateInfo[1] = odbst_StateInfo.aut;        //selected automatic mode
            stateInfo[2] = odbst_StateInfo.run;        //running status
            stateInfo[3] = odbst_StateInfo.motion;     //axis, dwell status
            stateInfo[4] = odbst_StateInfo.mstb;       //m, s, t, b status
            stateInfo[5] = odbst_StateInfo.emergency;  //emergency stop status
            stateInfo[6] = odbst_StateInfo.alarm;      //alarm status
            stateInfo[7] = odbst_StateInfo.edit;       //editting status
            return(ret);
        }
Ejemplo n.º 7
0
        public Status GetStatusInfo()
        {
            Focas1.ODBST odbst      = new Focas1.ODBST();
            Status       statusInfo = new Status();

            var statusReturnCode = (ReturnCodes.Code)Focas1.cnc_statinfo(focasHandle, odbst);

            statusInfo.returnCode = statusReturnCode;
            statusInfo.Auto       = odbst.aut;
            statusInfo.Emergency  = odbst.emergency;
            statusInfo.Run        = odbst.run;
            statusInfo.Motion     = odbst.motion;
            statusInfo.Edit       = odbst.edit;

            return(statusInfo);
        }
Ejemplo n.º 8
0
        public string GetStatus()
        {
            if (_handle == 0)
            {
                return("UNAVAILABLE");
            }

            Focas1.ODBST status = new Focas1.ODBST();

            _ret = Focas1.cnc_statinfo(_handle, status);

            if (_ret == Focas1.EW_OK)
            {
                return(GetModeString(status.aut));
            }
            return("UNAVAILABLE");
        }
Ejemplo n.º 9
0
 public void TestConnection(string cncAdress, ushort portNumber)
 {
     Focas1.ODBST buf = new Focas1.ODBST();
     ComReturnValue = Focas1.cnc_allclibhndl3(cncAdress, portNumber, TimeOut, out Handle);
     if (ComReturnValue == Focas1.EW_OK)
     {
         ErrorCode = "None";
         // Get CNC info
         Focas1.cnc_statinfo(Handle, buf);
         // Close COM handle
         Focas1.cnc_freelibhndl(Handle);
     }
     else
     {
         Console.WriteLine("ERROR!({0})", ComReturnValue);
         ErrorCode = $"({ComReturnValue})";
     }
 }
Ejemplo n.º 10
0
        //获得状态
        /// <summary>
        /// 获取CNC运行状态
        /// </summary>
        /// <param name="deviceAddress"></param>
        /// <param name="error"></param>
        /// <returns>1 运行 2报警 3急停 4 待机</returns>
        private static int GetStatus(string deviceAddress, out string error)
        {
            error = "";
            Focas1.ODBST statbuf = new Focas1.ODBST();
            ushort       m_handle;
            short        nRet = Focas1.cnc_allclibhndl3(deviceAddress, 8193, 2, out m_handle);

            if (nRet == Focas1.EW_OK)
            {
                nRet = Focas1.cnc_statinfo(m_handle, statbuf);
            }

            if (nRet == Focas1.EW_OK)
            {
                int iState = 0;

                if (statbuf.run > 0) //运行中
                {
                    iState = 1;
                }
                else if (statbuf.alarm > 0) //故障
                {
                    iState = 2;
                }
                else if (statbuf.emergency > 0) //急停
                {
                    iState = 3;
                }
                else //待机
                {
                    iState = 4;
                }

                Focas1.cnc_freelibhndl(m_handle); //释放连接
                return(iState);
            }

            error = "连接失败";
            return(-1);
        }
Ejemplo n.º 11
0
        public string GetMode()
        {
            // Check we have a valid handle
            if (_handle == 0)
            {
                return("UNAVAILABLE");
            }

            // Creat an instance of our stucture
            Focas1.ODBST mode = new Focas1.ODBST();

            // Ask Fanuc for the status information
            _ret = Focas1.cnc_statinfo(_handle, mode);

            // Check to make sure the call was successfull
            // and convert the mode to a string and return it.
            if (_ret == Focas1.EW_OK)
            {
                return(GetModeString(mode.aut));
            }
            return("UNAVAILABLE");
        }
Ejemplo n.º 12
0
        //-------------------------------------------------------------------------------------------------------------------//
        //----------------------------- CNC Connect & Test ------------------------------------------------------------------//
        //-------------------------------------------------------------------------------------------------------------------//


        public void cncConnect()
        {
            // Open a Cndex Session
            int result = Focas1.cnc_allclibhndl3(fanucIP, 8193, 6, out hFanucConn);

            //int result = Focas1.cnc_allclibhndl(out hFanucLib);
            fanucStatus = new Focas1.ODBST();

            // Check to see if the session was successful. If it was, enter a loop to continually test for connection.
            if (result == 0)
            {
                while (Focas1.cnc_statinfo(hFanucConn, fanucStatus) == Focas1.EW_OK && halt == false)
                {
                    connected = true;
                    Thread.Sleep(1000);
                }

                // If the loop was not halted on purpose (Stop Button was not clicked), then display tbe error
                if (halt == false)
                {
                    //MessageBox.Show("Cndex Disconnected!");
                    Focas1.cnc_freelibhndl(hFanucConn);
                    connected = false;
                    return;
                }
                //Focas1.cnc_freelibhndl(hFanucLib);
                return;
            }
            else
            {
                // If we did not enter the loop at all then the connection was not successful, so display an error
                Focas1.cnc_freelibhndl(hFanucConn);
                MessageBox.Show("Error connecting to CNC at: " + fanucIP + ":8193");
                return;
            }
        }
Ejemplo n.º 13
0
        public string read()
        {
            if (!isOpened)
            {
                throw new Exception("Must open cnc connection first!");
            }
            // logger.Info("Target ip: " + host + "; port: " + port);
            JObject resp = new JObject();

            resp["systemtime"]               = DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString();
            resp["centername"]               = "加工中心";
            resp["txtip"]                    = host;
            resp["machinename"]              = "FANUC Series 32i";
            resp["spec"]                     = "5505";
            resp["x_relative"]               = null;
            resp["z_relative"]               = null;
            resp["c_relative"]               = null;
            resp["v_relative"]               = null;
            resp["x_absolute"]               = null;
            resp["z_absolute"]               = null;
            resp["c_absolute"]               = null;
            resp["v_absolute"]               = null;
            resp["x_machine"]                = null;
            resp["z_machine"]                = null;
            resp["c_machine"]                = null;
            resp["v_machine"]                = null;
            resp["x_distancetogo"]           = null;
            resp["z_distancetogo"]           = null;
            resp["c_distancetogo"]           = null;
            resp["v_distancetogo"]           = null;
            resp["spindle_name"]             = null;
            resp["spindlenumber"]            = 1;
            resp["spindleload"]              = null;
            resp["spindlespeed"]             = null;
            resp["sv1_load"]                 = null;
            resp["sv2_load"]                 = null;
            resp["sv3_load"]                 = null;
            resp["sv4_load"]                 = null;
            resp["cnc_status"]               = null;
            resp["runningmode"]              = null;
            resp["currentrunningprogramnum"] = null;
            resp["mainprogramnumber"]        = null;
            resp["currentrunningncprogram"]  = null;
            resp["currenttoolgroupnum"]      = null;
            resp["currenttoolnum"]           = null;
            resp["svnum"]                    = null;
            resp["cnc_versionnum"]           = "3C7B5D01";
            resp["relfeedrate"]              = null;
            resp["alarm_status"]             = null;
            resp["alarmnum"]                 = null;
            resp["alarmtype"]                = null;
            resp["alarmmessage"]             = null;
            resp["operatingtime"]            = null;
            resp["circlingtime"]             = null;
            resp["cuttingtime"]              = null;
            resp["powerontime"]              = null;
            resp["totalparts"]               = null;
            resp["cnc_turn_on"]              = "开机";
            short code;

            {
                short         number = Focas1.MAX_AXIS;
                Focas1.ODBPOS pos    = new Focas1.ODBPOS();
                code = Focas1.cnc_rdposition(pHdl, -1, ref number, pos);
                if (code == 0)
                {
                    resp["x_relative"]     = pos.p1.rel.data * Math.Pow(10, -pos.p1.rel.dec);
                    resp["z_relative"]     = pos.p2.rel.data * Math.Pow(10, -pos.p2.rel.dec);
                    resp["c_relative"]     = pos.p4.rel.data * Math.Pow(10, -pos.p4.rel.dec);
                    resp["v_relative"]     = pos.p5.rel.data * Math.Pow(10, -pos.p5.rel.dec);
                    resp["x_absolute"]     = pos.p1.abs.data * Math.Pow(10, -pos.p1.abs.dec);
                    resp["z_absolute"]     = pos.p2.abs.data * Math.Pow(10, -pos.p2.abs.dec);
                    resp["c_absolute"]     = pos.p4.abs.data * Math.Pow(10, -pos.p4.abs.dec);
                    resp["v_absolute"]     = pos.p5.abs.data * Math.Pow(10, -pos.p5.abs.dec);
                    resp["x_machine"]      = pos.p1.mach.data * Math.Pow(10, -pos.p1.mach.dec);
                    resp["z_machine"]      = pos.p2.mach.data * Math.Pow(10, -pos.p2.mach.dec);
                    resp["c_machine"]      = pos.p4.mach.data * Math.Pow(10, -pos.p4.mach.dec);
                    resp["v_machine"]      = pos.p5.mach.data * Math.Pow(10, -pos.p5.mach.dec);
                    resp["x_distancetogo"] = pos.p1.dist.data * Math.Pow(10, -pos.p1.dist.dec);
                    resp["z_distancetogo"] = pos.p2.dist.data * Math.Pow(10, -pos.p2.dist.dec);
                    resp["c_distancetogo"] = pos.p4.dist.data * Math.Pow(10, -pos.p4.dist.dec);
                    resp["v_distancetogo"] = pos.p5.dist.data * Math.Pow(10, -pos.p5.dist.dec);
                }
            }
            {
                short number = 32;
                Focas1.ODBEXAXISNAME spindleName = new Focas1.ODBEXAXISNAME();
                code = Focas1.cnc_exaxisname(pHdl, 1, ref number, spindleName);
                if (code == 0)
                {
                    resp["spindle_name"] = spindleName.axname1;
                }
            }
            {
                Focas1.ODBSVLOAD sv    = new Focas1.ODBSVLOAD();
                Focas1.ODBSPLOAD sp    = new Focas1.ODBSPLOAD();
                short            a     = 6;
                short            code0 = Focas1.cnc_rdsvmeter(pHdl, ref a, sv);
                short            code1 = Focas1.cnc_rdspmeter(pHdl, 1, ref a, sp);
                if (code0 == 0)
                {
                    resp["sv1_load"] = sv.svload1.data;
                    resp["sv2_load"] = sv.svload2.data;
                    resp["sv3_load"] = sv.svload3.data;
                    resp["sv4_load"] = sv.svload4.data;
                }
                if (code1 == 0)
                {
                    resp["spindleload"] = sp.spload1.spload.data;
                }
            }
            {
                Focas1.ODBACT pindle = new Focas1.ODBACT();
                code = Focas1.cnc_acts(pHdl, pindle);
                if (code == 0)
                {
                    resp["spindlespeed"] = pindle.data;
                }
            }
            {
                Focas1.ODBST obst = new Focas1.ODBST();
                code = Focas1.cnc_statinfo(pHdl, obst);
                if (code == 0)
                {
                    resp["cnc_status"]   = obst.run;
                    resp["runningmode"]  = obst.tmmode;
                    resp["alarm_status"] = obst.alarm;
                }
            }
            {
                Focas1.ODBDY2_1 dynadata = new Focas1.ODBDY2_1();
                Focas1.ODBPRO   pro      = new Focas1.ODBPRO();
                Focas1.ODBSEQ   seqnum   = new Focas1.ODBSEQ();
                code = Focas1.cnc_rdprgnum(pHdl, pro);
                if (code == 0)
                {
                    resp["currentrunningprogramnum"] = pro.data;
                    resp["mainprogramnumber"]        = pro.mdata;
                }
                code = Focas1.cnc_rdseqnum(pHdl, seqnum);
                if (code == 0)
                {
                    resp["currentrunningncprogram"] = seqnum.data;
                }
            }
            {
                Focas1.ODBTG     btg = new Focas1.ODBTG();
                Focas1.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
                code = Focas1.cnc_rdtlusegrp(pHdl, grp);
                short a = Convert.ToInt16(grp.use);
                code = Focas1.cnc_rdtoolgrp(pHdl, a, 20 + 20 * 1, btg);
                resp["currenttoolgroupnum"] = a;
                resp["currenttoolnum"]      = btg.data.data1.tool_num;
            }
            {
                Focas1.ODBSYS odbsys = new Focas1.ODBSYS();
                code = Focas1.cnc_sysinfo(pHdl, odbsys);
                if (code == 0)
                {
                    resp["svnum"] = odbsys.axes[1];
                }
            }
            {
                Focas1.ODBACT odbact = new Focas1.ODBACT();
                code = Focas1.cnc_actf(pHdl, odbact);
                if (code == 0)
                {
                    resp["relfeedrate"] = odbact.data;
                }
            }
            {
                short             b   = 8;
                Focas1.ODBALMMSG2 msg = new Focas1.ODBALMMSG2();
                code = Focas1.cnc_rdalmmsg2(pHdl, -1, ref b, msg);
                if (code == 0)
                {
                    resp["alarmnum"]     = msg.msg2.alm_no;
                    resp["alarmmessage"] = msg.msg2.alm_msg;
                }
            }
            {
                int alarm = 0;
                code = Focas1.cnc_alarm2(pHdl, out alarm);
                if (code == 0)
                {
                    resp["alarmtype"] = alarmTypeMapper.ContainsKey(alarm) ? alarmTypeMapper[alarm] : null;
                }
            }
            {
                Focas1.IODBPSD_1 param6751 = new Focas1.IODBPSD_1();
                Focas1.IODBPSD_1 param6752 = new Focas1.IODBPSD_1();
                code = Focas1.cnc_rdparam(pHdl, 6751, 0, 8, param6751);
                if (code == 0)
                {
                    int workingTimeSec = param6751.ldata / 1000;
                    code = Focas1.cnc_rdparam(pHdl, 6752, 0, 8, param6752);
                    if (code == 0)
                    {
                        int workingTimeMin = param6752.ldata;
                        int workingTime    = workingTimeMin * 60 + workingTimeSec;
                        resp["operatingtime"] = workingTime;
                    }
                }
            }
            {
                Focas1.IODBPSD_1 param6757 = new Focas1.IODBPSD_1();
                Focas1.IODBPSD_1 param6758 = new Focas1.IODBPSD_1();
                code = Focas1.cnc_rdparam(pHdl, 6757, 0, 8, param6757);
                if (code == 0)
                {
                    int circlingTimeSec = param6757.ldata / 1000;
                    code = Focas1.cnc_rdparam(pHdl, 6758, 0, 8, param6758);
                    if (code == 0)
                    {
                        int circlingTimeMin = param6758.ldata;
                        int workingTime     = circlingTimeMin * 60 + circlingTimeSec;
                        resp["circlingtime"] = workingTime;
                    }
                }
            }
            {
                Focas1.IODBPSD_1 param6753 = new Focas1.IODBPSD_1();
                Focas1.IODBPSD_1 param6754 = new Focas1.IODBPSD_1();
                code = Focas1.cnc_rdparam(pHdl, 6753, 0, 8 + 32, param6753);
                if (code == 0)
                {
                    int cuttingTimeSec = param6753.ldata / 1000;
                    code = Focas1.cnc_rdparam(pHdl, 6754, 0, 8 + 32, param6754);
                    if (code == 0)
                    {
                        int cuttingTimeMin = param6754.ldata;
                        int workingTime    = cuttingTimeMin * 60 + cuttingTimeSec;
                        resp["cuttingtime"] = workingTime;
                    }
                }
            }
            {
                Focas1.IODBPSD_1 param6750 = new Focas1.IODBPSD_1();
                code = Focas1.cnc_rdparam(pHdl, 6750, 0, 8 + 32, param6750);
                if (code == 0)
                {
                    int powerOnTime = param6750.ldata * 60;
                    resp["powerontime"] = powerOnTime;
                }
            }
            {
                Focas1.IODBPSD_1 param6712 = new Focas1.IODBPSD_1();
                code = Focas1.cnc_rdparam(pHdl, 6712, 0, 8, param6712);
                if (code == 0)
                {
                    resp["totalparts"] = param6712.ldata;
                }
            }
            return(resp.ToString());
        }
Ejemplo n.º 14
0
        //-------------------------------------------------------------------------------------------------------------------//
        //----------------------------- CNC Connect & Test ------------------------------------------------------------------//
        //-------------------------------------------------------------------------------------------------------------------//


        public void cncConnect()
        {
            // Open a Cndex Session
            int result = Focas1.cnc_allclibhndl3(fanucIP, 8193, 6, out hFanucConn);
            //int result = Focas1.cnc_allclibhndl(out hFanucLib);
            fanucStatus = new Focas1.ODBST();

            // Check to see if the session was successful. If it was, enter a loop to continually test for connection.
            if (result == 0)
            {
                while (Focas1.cnc_statinfo(hFanucConn, fanucStatus) == Focas1.EW_OK && halt == false)
                {
                    connected = true;
                    Thread.Sleep(1000);
                }

                // If the loop was not halted on purpose (Stop Button was not clicked), then display tbe error
                if (halt == false)
                {
                    //MessageBox.Show("Cndex Disconnected!");
                    Focas1.cnc_freelibhndl(hFanucConn);
                    connected = false;
                    return;
                }
                //Focas1.cnc_freelibhndl(hFanucLib);
                return;
            }
            else
            {
                // If we did not enter the loop at all then the connection was not successful, so display an error
                Focas1.cnc_freelibhndl(hFanucConn);
                MessageBox.Show("Error connecting to CNC at: " + fanucIP + ":8193");
                return;
            }
        }