123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace IMCS_CCS.Utils.DeviceProtocol
- {
- public class DeviceFanuc
- {
- public ushort h = 0;
- public DeviceFanuc(string ip, string port)
- {
- Fanuc.cnc_allclibhndl3(ip, Convert.ToUInt16(port), 1, out h);
- }
- public void freehand()
- {
- Focas1.cnc_freelibhndl(h);
- }
- public string MainProg()
- {
- Focas1.ODBPRO pnum = new Focas1.ODBPRO();
- int ret = Focas1.cnc_rdprgnum(h, pnum);
- if (ret == Fanuc.EW_OK)
- {
- return pnum.data.ToString().PadLeft(4, '0');
- }
- else
- {
- return "error";
- }
- }
- public string Status()
- {
- Focas1.ODBST cncStatus = new Focas1.ODBST();
- string[] str = new string[3];
- int ret = Focas1.cnc_statinfo(h, cncStatus);
- if (ret == Fanuc.EW_OK)
- {
- return cncStatus.run.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string Mode()
- {
- Focas1.ODBST cncStatus = new Focas1.ODBST();
- string[] str = new string[3];
- int ret = Focas1.cnc_statinfo(h, cncStatus);
- if (ret == Fanuc.EW_OK)
- {
- return cncStatus.aut.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string EMG()
- {
- Focas1.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
- int ret = Focas1.pmc_rdpmcrng(h, 0, 0, 7, 12, 14, iodbpmc0);
- if (ret == Fanuc.EW_OK)
- {
- byte[] data = iodbpmc0.cdata;
- string value = Convert.ToString(data[1], 16).ToString().PadLeft(2, '0').ToUpper();
- string str = Convert.ToString(Convert.ToInt32(value.ToString().Substring(0, 1)), 2).PadLeft(4, '0').Substring(3, 1);
- if (str == "1")
- return "1";
- else
- return "0";
- }
- else
- {
- return "error";
- }
- }
- public string ActFeed()
- {
- Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
- int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 12, 13, 10, iodbpmco);
- if (ret == Fanuc.EW_OK)
- {
- return iodbpmco.cdata[0].ToString();
- }
- else
- {
- return "error";
- }
- }
- public string ActSpindle()
- {
- Focas1.ODBACT pindle = new Focas1.ODBACT();
- int ret = Focas1.cnc_acts(h, pindle);
- if (ret == Fanuc.EW_OK)
- {
- return pindle.data.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string ServoLoadX()
- {
- Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
- short a = 6;//伺服轴的数量
- int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
- if (ret == Fanuc.EW_OK)
- {
- return sv.svload1.data.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string ServoLoadY()
- {
- Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
- short a = 6;//伺服轴的数量
- int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
- if (ret == Fanuc.EW_OK)
- {
- return sv.svload2.data.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string ServoLoadZ()
- {
- Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
- short a = 6;//伺服轴的数量
- int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
- if (ret == Fanuc.EW_OK)
- {
- return sv.svload3.data.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string PowerOnTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(h, 6750, 0, 8, iodbpsd);
- if (ret == Fanuc.EW_OK)
- {
- return iodbpsd.ldata.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string AccumulateCuttingTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(h, 6754, 0, 8, iodbpsd);
- if (ret == Fanuc.EW_OK)
- {
- return iodbpsd.ldata.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string CuttingTimePerCycle()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int min = 0;
- int sec = 0;
- int ret = Focas1.cnc_rdparam(h, 6757, 0, 8, iodbpsd);//秒
- if (ret == Focas1.EW_OK)
- {
- sec = iodbpsd.ldata;
- ret = Focas1.cnc_rdparam(h, 6758, 0, 8, iodbpsd);//分
- if (ret == Focas1.EW_OK)
- {
- min = iodbpsd.ldata;
- return (min * 60 * 1000 + sec).ToString();
- }
- else
- { return "error"; }
- }
- else { return "error"; }
- }
- public string WorkTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(h, 6752, -1, 8, iodbpsd);
- if (ret == Fanuc.EW_OK)
- {
- return iodbpsd.ldata.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string Part_Count()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(h, 6712, 0, 8, iodbpsd);
- if (ret == Fanuc.EW_OK)
- {
- return iodbpsd.ldata.ToString();
- }
- else
- {
- return "error";
- }
- }
- public string ToolNo()
- {
- Focas1.ODBM odb = new Focas1.ODBM();
- int ret = Focas1.cnc_rdmacro(h, 523, 10, odb);
- if (ret == Fanuc.EW_OK)
- {
- return (odb.mcr_val * Math.Pow(10, -odb.dec_val)).ToString();
- }
- else
- {
- return "error";
- }
- }
- public string ToolLife()
- {
- Fanuc.ODBTLIFE5 tool = new Focas1.ODBTLIFE5();//读取刀片组的序号
- Fanuc.ODBTLIFE2 tool1 = new Focas1.ODBTLIFE2();//读取刀片组的全部数量
- Fanuc.ODBTLIFE3 tool2 = new Focas1.ODBTLIFE3();//刀具的数量
- Fanuc.ODBTG btg = new Focas1.ODBTG();
- Fanuc.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
- int m = 2;
- Fanuc.cnc_rdgrpid2(h, m, tool);
- int group_numer = tool.data;
- int all = Fanuc.cnc_rdngrp(Fanuc.h, tool1);//刀片组的全部数量
- short b = Convert.ToInt16(group_numer);
- Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量
- Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
- Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
- Fanuc.cnc_rdtoolgrp(h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息,很重要;
- int ret = Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号;
- if (ret == Fanuc.EW_OK)
- {
- return tool2.data.ToString();
- }
- else
- {
- return "error";
- }
- }
- public List<AlmInfo> AlmMsg(out string IsAlarm)
- {
- List<AlmInfo> stateinfo = new List<AlmInfo>();
- IsAlarm = "0";
- ushort h = 0;
- short b = -1;//哪一类报警,-1所有报警
- short num = 11;//设大一些没关系
- Focas1.ODBALMMSG odbalmmsg2 = new Focas1.ODBALMMSG();
- int ret = Focas1.cnc_rdalmmsg(h, b, ref num, odbalmmsg2);
- System.Type type = odbalmmsg2.GetType();
- if (ret == 0)
- {
- if (num > 0)
- {
- IsAlarm = "1";
- for (int i = 0; i <= num - 1; i++)
- {
- string str1 = "msg";
- str1 = str1 + (i + 1);
- object obj = type.GetField(str1).GetValue(odbalmmsg2);
- //if(obj.)
- System.Type type1 = obj.GetType();
- if (type1.GetField("alm_msg").GetValue(obj).ToString() != "")
- {
- AlmInfo s = new AlmInfo();
- s.no = Convert.ToInt32(type1.GetField("alm_no").GetValue(obj).ToString()).ToString();
- s.msg = type1.GetField("alm_msg").GetValue(obj).ToString();
- stateinfo.Add(s);
- }
- }
- return stateinfo;
- }
- else
- {
- IsAlarm = "0";
- AlmInfo s2 = new AlmInfo();
- s2.no = "0";
- s2.msg = "无报警";
- stateinfo.Add(s2);
- return stateinfo;
- }
- }
- else
- {
- IsAlarm = "0";
- AlmInfo s3 = new AlmInfo();
- s3.no = "0";
- s3.msg = "无报警";
- stateinfo.Add(s3);
- return stateinfo;
- }
- }
- public string ReadSpindleSpeed()
- {
- Focas1.ODBACT pindle = new Focas1.ODBACT();
- string str = "";
- int ret = Focas1.cnc_acts(Fanuc.h, pindle);
- if (ret == 0)
- str = pindle.data.ToString();
- else str = "错误";
- return str;
- }
- public string GetSpload()
- {
- string str = "";
- Focas1.ODBSPLOAD ODBSPLOAD = new Focas1.ODBSPLOAD();
- short typel = 0;
- short bl = 4;
- int ret = Focas1.cnc_rdspmeter(Fanuc.h, typel, ref bl, ODBSPLOAD);
- if (ret == Focas1.EW_OK)
- str = ODBSPLOAD.spload1.spload.data.ToString();
- else
- str = "返回错误";
- return str;
- }
- public string GetSpload1()
- {
- string str = "";
- Focas1.ODBSPLOAD ODBSPLOAD = new Focas1.ODBSPLOAD();
- short typel = 0;
- short bl = 1;
- int ret = Focas1.cnc_rdspmeter(Fanuc.h, typel, ref bl, ODBSPLOAD);
- if (ret == Focas1.EW_OK)
- str = ODBSPLOAD.spload1.spload.data.ToString();
- else
- str = "返回错误";
- return str;
- }
- public string GetSpload2()
- {
- string str = "";
- Focas1.ODBSPLOAD ODBSPLOAD = new Focas1.ODBSPLOAD();
- short typel = 0;
- short bl = 2;
- int ret = Focas1.cnc_rdspmeter(Fanuc.h, typel, ref bl, ODBSPLOAD);
- if (ret == Focas1.EW_OK)
- str = ODBSPLOAD.spload1.spload.data.ToString();
- else
- str = "返回错误";
- return str;
- }
- public string GetSpload3()
- {
- string str = "";
- Focas1.ODBSPLOAD ODBSPLOAD = new Focas1.ODBSPLOAD();
- short typel = 0;
- short bl = 3;
- int ret = Focas1.cnc_rdspmeter(Fanuc.h, typel, ref bl, ODBSPLOAD);
- if (ret == Focas1.EW_OK)
- str = ODBSPLOAD.spload1.spload.data.ToString();
- else
- str = "返回错误";
- return str;
- }
- /// <summary>
- /// fanuc单次计数
- /// </summary>
- /// <returns></returns>
- public string GetNumber()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(Fanuc.h, 6711, 0, 50, iodbpsd);
- if (ret == 0)
- {
- return iodbpsd.idata.ToString();
- }
- else { return "0"; }
- }
- /// <summary>
- /// fanuc累计计数
- /// </summary>
- /// <returns></returns>
- public string GetTotalNumber()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(Fanuc.h, 6712, 0, 8, iodbpsd);
- if (ret == 0)
- {
- return iodbpsd.ldata.ToString();
- }
- else { return "0"; }
- }
- static Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
- public string GetSploadRatio()
- {
- int ret = Focas1.pmc_rdpmcrng(Fanuc.h, 0, 1, 30, 31, 10, iodbpmco);
- if (ret == 0)
- {
- return iodbpmco.cdata[0].ToString();
- }
- else { return "0"; }
- }
- public string GetFeedrate()
- {
- int ret = Focas1.pmc_rdpmcrng(Fanuc.h, 0, 1, 12, 13, 10, iodbpmco);
- if (ret == 0)
- {
- return (255 - iodbpmco.cdata[0]).ToString();
- }
- else { return "0"; }
- }
- public string GetEmergencyStopStatus()
- {
- try
- {
- Focas1.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
- int ret = Focas1.pmc_rdpmcrng(Fanuc.h, 0, 0, 7, 12, 14, iodbpmc0);
- if (ret == Focas1.EW_OK)
- {
- byte[] data = iodbpmc0.cdata;
- string value = Convert.ToString(data[1], 16).ToString().PadLeft(2, '0').ToUpper();
- string str = Convert.ToString(Convert.ToInt32(value.ToString().Substring(0, 1)), 2).PadLeft(4, '0').Substring(3, 1);
- if (str == "1")
- {
- return "否";
- }
- else { return "是"; }
- }
- else
- {
- return ret.ToString();
- }
- }
- catch
- {
- return "Filed";
- }
- }
- /// <summary>
- /// 有效轴数
- /// </summary>
- /// <param name="xd">相对位置</param>
- /// <param name="jd">绝对位置</param>
- /// <param name="jx">机械位置</param>
- /// <param name="sy">剩余位置</param>
- /// <returns></returns>
- public string GetPostion(out string xd, out string jd, out string jx, out string sy)
- {
- xd = ""; jd = ""; jx = ""; sy = "";
- Fanuc.ODBPOS fos = new Focas1.ODBPOS();
- short num = Fanuc.MAX_AXIS;
- short type = -1;
- short ret = Fanuc.cnc_rdposition(Fanuc.h, type, ref num, fos);
- if (ret == 0)
- {
- xd += fos.p1.rel.name.ToString() + ": " + fos.p1.rel.data * Math.Pow(10, -fos.p1.rel.dec) + ",";
- xd += fos.p2.rel.name.ToString() + ": " + fos.p2.rel.data * Math.Pow(10, -fos.p2.rel.dec) + ",";
- xd += fos.p3.rel.name.ToString() + ": " + fos.p3.rel.data * Math.Pow(10, -fos.p3.rel.dec) + ",";
- xd += fos.p4.rel.name.ToString() + ": " + fos.p4.rel.data * Math.Pow(10, -fos.p4.rel.dec) + ",";
- xd += fos.p5.rel.name.ToString() + ": " + fos.p5.rel.data * Math.Pow(10, -fos.p5.rel.dec);
- jd += fos.p1.abs.name.ToString() + ": " + fos.p1.abs.data * Math.Pow(10, -fos.p1.abs.dec) + ",";
- jd += fos.p2.abs.name.ToString() + ": " + fos.p2.abs.data * Math.Pow(10, -fos.p2.abs.dec) + ",";
- jd += fos.p3.abs.name.ToString() + ": " + fos.p3.abs.data * Math.Pow(10, -fos.p3.abs.dec) + ",";
- jd += fos.p4.abs.name.ToString() + ": " + fos.p4.abs.data * Math.Pow(10, -fos.p3.abs.dec) + ",";
- jd += fos.p5.abs.name.ToString() + ": " + fos.p5.abs.data * Math.Pow(10, -fos.p5.abs.dec);
- jx += fos.p1.mach.name.ToString() + ": " + fos.p1.mach.data * Math.Pow(10, -fos.p1.mach.dec) + ",";
- jx += fos.p2.mach.name.ToString() + ": " + fos.p2.mach.data * Math.Pow(10, -fos.p2.mach.dec) + ",";
- jx += fos.p3.mach.name.ToString() + ": " + fos.p3.mach.data * Math.Pow(10, -fos.p3.mach.dec) + ",";
- jx += fos.p4.mach.name.ToString() + ": " + fos.p4.mach.data * Math.Pow(10, -fos.p4.mach.dec) + ",";
- jx += fos.p5.mach.name.ToString() + ": " + fos.p5.mach.data * Math.Pow(10, -fos.p5.mach.dec);
- sy += fos.p1.dist.name.ToString() + ": " + fos.p1.dist.data * Math.Pow(10, -fos.p1.dist.dec) + ",";
- sy += fos.p2.dist.name.ToString() + ": " + fos.p2.dist.data * Math.Pow(10, -fos.p2.dist.dec) + ",";
- sy += fos.p3.dist.name.ToString() + ": " + fos.p3.dist.data * Math.Pow(10, -fos.p3.dist.dec) + ",";
- sy += fos.p4.dist.name.ToString() + ": " + fos.p3.dist.data * Math.Pow(10, -fos.p4.dist.dec) + ",";
- sy += fos.p5.dist.name.ToString() + ": " + fos.p5.dist.data * Math.Pow(10, -fos.p5.dist.dec);
- }
- return num.ToString();
- }
- public string GetFeedSpeed()
- {
- Focas1.ODBACT feedspeed = new Focas1.ODBACT();
- Focas1.cnc_actf(Fanuc.h, feedspeed);
- return feedspeed.data.ToString();
- }
- public string GetSdTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(Fanuc.h, 6750, 0, 8, iodbpsd);
- if (ret == 0)
- {
- return iodbpsd.ldata.ToString();
- }
- else { return "Filed"; }
- }
- public string GetRunningTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(Fanuc.h, 6752, -1, 8, iodbpsd);
- if (ret == 0)
- {
- return iodbpsd.ldata.ToString();
- }
- else { return "Filed"; }
- }
- public string GetQXTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int ret = Focas1.cnc_rdparam(Fanuc.h, 6754, 0, 8, iodbpsd);
- if (ret == 0)
- {
- return iodbpsd.ldata.ToString();
- }
- else { return "Filed"; }
- }
- public string GetCycleTime()
- {
- Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
- int min = 0;
- int sec = 0;
- int ret = Focas1.cnc_rdparam(Fanuc.h, 6757, 0, 8, iodbpsd);//秒
- if (ret == Focas1.EW_OK)
- {
- sec = iodbpsd.ldata;
- ret = Focas1.cnc_rdparam(Fanuc.h, 6758, 0, 8, iodbpsd);//分
- if (ret == Focas1.EW_OK)
- {
- min = iodbpsd.ldata;
- return (min * 60 * 1000 + sec).ToString();
- }
- else
- { return "Filed"; }
- }
- else { return "Filed"; }
- }
- public enum MacModeLists
- {
- MDI = 0,
- MEMory = 1,
- Null = 2,
- EDIT = 3,
- HaNDle = 4,
- JOG = 5,
- TeachInJOG = 6,
- TeachInHaNDle = 7,
- INCfeed = 8,
- REFerence = 9,
- ReMoTe = 10,
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="Macmode">工作模式</param>
- /// <param name="Runstatus">加工状态</param>
- public void GetStateInfo(out string Macmode, out string Runstatus)
- {
- Focas1.ODBST cncStatus = new Focas1.ODBST();
- string[] str = new string[3];
- Focas1.cnc_statinfo(Fanuc.h, cncStatus);
- Macmode = Enum.GetNames(typeof(MacModeLists))[cncStatus.aut];
- Runstatus = Enum.GetNames(typeof(MacModeLists))[cncStatus.run];
- }
- /// <summary>
- /// 返回注册程序量
- /// </summary>
- /// <param name="unreg_prg">可用程序数量</param>
- /// <param name="unused_mem">未使用的内存</param>
- /// <param name="used_mem">已经使用内存</param>
- /// <returns></returns>
- public string GetRegPrg(out string unreg_prg, out string unused_mem, out string used_mem)
- {
- Focas1.ODBNC_1 odbnc_1 = new Focas1.ODBNC_1();
- short ret = Focas1.cnc_rdproginfo(Fanuc.h, 0, 16, odbnc_1);
- if (ret == Focas1.EW_OK)
- {
- unreg_prg = odbnc_1.unreg_prg.ToString();
- unused_mem = ((int)(Convert.ToDouble(odbnc_1.unused_mem) / 1024.0 + 0.5)).ToString();
- used_mem = ((int)(Convert.ToDouble(odbnc_1.used_mem) / 1024.0 + 0.5)).ToString();
- return odbnc_1.reg_prg.ToString();
- }
- else
- {
- unreg_prg = "Filed";
- unused_mem = "Filed";
- used_mem = "Filed";
- return "Filed";
- }
- }
- /// <summary>
- /// 获取当前执行的刀号
- /// </summary>
- /// <param name="no"></param>
- /// <returns></returns>
- public string FGetToolNum()
- {
- string result = "";
- Focas1.ODBM odb = new Focas1.ODBM();
- var ret = Focas1.cnc_rdmacro(Fanuc.h, 4320, 10, odb);
- if (ret == 0)
- {
- result = (odb.mcr_val * Math.Pow(10, -odb.dec_val)).ToString();
- return result;
- }
- else
- {
- return "获取失败";
- }
- }
- /// <summary>
- /// 执行行号
- /// </summary>
- /// <param name="no"></param>
- /// <param name="result"></param>
- /// <returns></returns>
- public string FGetPrgSeq()
- {
- string result = "";
- Focas1.ODBSEQ snum = new Focas1.ODBSEQ();
- var ret = Focas1.cnc_rdseqnum(Fanuc.h, snum);
- if (ret == Focas1.EW_OK)
- {
- //return result = (String.Format("N{0:00000}", snum.data));
- return result = snum.data.ToString();
- }
- else
- {
- return "失败";
- }
- }
- /// <summary>
- /// 当前执行代码
- /// </summary>
- /// <returns></returns>
- public string FGetCurrentPrg()
- {
- ushort length = 48;
- short b = 0;
- char[] c = new char[length];
- var ret = Focas1.cnc_rdexecprog(Fanuc.h, ref length, out b, c);
- if (ret == Focas1.EW_OK)
- {
- return new string(c);
- }
- else
- {
- return "失败";
- }
- }
- public string GetCanUsePrg()
- {
- Focas1.ODBNC_1 odbnc_1 = new Focas1.ODBNC_1();
- short ret = Focas1.cnc_rdproginfo(Fanuc.h, 0, 16, odbnc_1);
- if (ret == Focas1.EW_OK)
- {
- return odbnc_1.unreg_prg.ToString();
- }
- else
- {
- return "Filed";
- }
- }
- public string GetUnUsedMes()
- {
- Focas1.ODBNC_1 odbnc_1 = new Focas1.ODBNC_1();
- short ret = Focas1.cnc_rdproginfo(Fanuc.h, 0, 16, odbnc_1);
- if (ret == Focas1.EW_OK)
- {
- return ((int)(Convert.ToDouble(odbnc_1.unused_mem) / 1024.0 + 0.5)).ToString();
- }
- else
- {
- return "Filed";
- }
- }
- public string GetUsedMes()
- {
- Focas1.ODBNC_1 odbnc_1 = new Focas1.ODBNC_1();
- short ret = Focas1.cnc_rdproginfo(Fanuc.h, 0, 16, odbnc_1);
- if (ret == Focas1.EW_OK)
- {
- return ((int)(Convert.ToDouble(odbnc_1.used_mem) / 1024.0 + 0.5)).ToString();
- }
- else
- {
- return "Filed";
- }
- }
- public string GetPmcProgramNumber()
- {
- Focas1.ODBPRO pnum = new Focas1.ODBPRO();
- int ret = Focas1.cnc_rdprgnum(Fanuc.h, pnum);
- if (ret == Focas1.EW_OK)
- {
- return pnum.data.ToString().PadLeft(4, '0');
- }
- else
- {
- return "程序号Filed";
- }
- }
- public string GetProgramCodes(out string ProgramNumber)
- {
- ProgramNumber = GetPmcProgramNumber();
- if (ProgramNumber != "Filed")
- {
- short cncNb = Convert.ToInt16(ProgramNumber);
- string data = "";
- short ret = Fanuc.upload(Fanuc.h, 0, cncNb, ref data, null);
- if (ret == 0)
- {
- data = data.Replace("\n", "\r\n");
- return data;
- }
- else
- {
- return "Filed,可能受保护";
- }
- }
- else
- {
- return "Filed,可能受保护";
- }
- }
- public string Get_tool_offset()
- {
- Fanuc.ODBTLINF inf = new Focas1.ODBTLINF();
- Fanuc.ODBTOFS tof = new Focas1.ODBTOFS();
- Fanuc.cnc_rdtofsinfo(Fanuc.h, inf);
- short a = inf.use_no;
- short b = inf.ofs_type;
- short ret = Fanuc.cnc_rdtofs(Fanuc.h, a, b, 8, tof);
- if (ret == 0)
- {
- return tof.data.ToString();
- }
- else
- return "Filed";
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dpsl">刀片组数量</param>
- /// <param name="dph">刀片号</param>
- /// <param name="dpzh">刀组号</param>
- /// <param name="smjs">寿命计时</param>
- public void Get_tool_life(out string dpsl, out string dph, out string dpzh, out string smjs)//读取获得刀片的有关信息
- {
- Fanuc.ODBTLIFE5 tool = new Focas1.ODBTLIFE5();//读取刀片组的序号
- Fanuc.ODBTLIFE2 tool1 = new Focas1.ODBTLIFE2();//读取刀片组的全部数量
- Fanuc.ODBTLIFE3 tool2 = new Focas1.ODBTLIFE3();//刀具的数量
- Fanuc.ODBTG btg = new Focas1.ODBTG();
- Fanuc.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
- int m = 2;
- Fanuc.cnc_rdgrpid2(Fanuc.h, m, tool);
- int group_numer = tool.data;
- int all = Fanuc.cnc_rdngrp(Fanuc.h, tool1);//刀片组的全部数量
- short b = Convert.ToInt16(group_numer);
- Fanuc.cnc_rdntool(Fanuc.h, b, tool2);//刀具的数量
- Fanuc.cnc_rdlife(Fanuc.h, b, tool2);//刀具寿命
- Fanuc.cnc_rdcount(Fanuc.h, b, tool2);//刀具计时器
- Fanuc.cnc_rdtoolgrp(Fanuc.h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息,很重要;
- Fanuc.cnc_rdtlusegrp(Fanuc.h, grp);//读出正在使用的到组号;
- dpsl = all.ToString();
- dph = tool2.ToString();
- dpzh = group_numer.ToString();
- smjs = tool2.data.ToString();
- }
- public int FreeHndl()
- {
- int ret = Focas1.cnc_freelibhndl(Fanuc.h);
- return ret;
- }
- public string bj()
- {
- string alarmmsg = "";
- short b = -1;//哪一类报警,-1所有报警
- short num = 11;//设大一些没关系
- Focas1.ODBALMMSG odbalmmsg2 = new Focas1.ODBALMMSG();
- var ret = Focas1.cnc_rdalmmsg(Fanuc.h, b, ref num, odbalmmsg2);
- System.Type type = odbalmmsg2.GetType();
- if (ret == 0)
- {
- if (num > 0)
- {
- for (int i = 0; i <= num - 1; i++)
- {
- string str1 = "msg";
- str1 = str1 + (i + 1);
- object obj = type.GetField(str1).GetValue(odbalmmsg2);
- //if(obj.)
- System.Type type1 = obj.GetType();
- if (type1.GetField("alm_msg").GetValue(obj).ToString() != "")
- {
- alarmmsg += "第" + i + "行" + "警报号 " + type1.GetField("alm_no").GetValue(obj).ToString().Trim() + "警报信息" + type1.GetField("alm_msg").GetValue(obj).ToString().Trim() + ",";
- }
- }
- }
- else
- {
- }
- }
- else
- {
- }
- if (alarmmsg == "")
- {
- alarmmsg = "暂无报警";
- }
- return alarmmsg;
- }
- public class AlmInfo
- {
- public string no { get; set; }
- public string msg { get; set; }
- }
- public class NcInfo
- {
- public string no { get; set; }
- public string lentgh { get; set; }
- public string comment { get; set; }
- }
- }
- }
|