123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
-
- using FANUC;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using static fanuc采集.ConDevice;
- namespace HttpServer
- {
- public class ConDevice2
- {
- private string remotePath = "//CNC_MEM/USER/PATH1/";
- public ushort h = 0;
- public ConDevice2(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 (100 - (iodbpmco.cdata[0] - 155)).ToString();
- }
- else
- {
- return "error";
- }
- }
- public string spindleMagnification()
- {
- //主轴倍率
- Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
- int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 30, 31, 10, iodbpmco);
- if (ret == Fanuc.EW_OK)
- {
- return iodbpmco.cdata[0].ToString();
- }
- else
- {
- return "error";
- }
- }
- public string SpindleLoad()
- {
- //主轴负载
- String loaderX = this.ServoLoadX();
- String loaderY = this.ServoLoadY();
- String loaderZ = this.ServoLoadZ();
- if (loaderX == "error" || loaderY == "error" || loaderZ == "error") return "error";
- return loaderX + "/" + loaderY + "/" + loaderZ;
- }
- public string ActSpindle()
- {
- //主轴转数 pmc_rdpmcrng(hFanuc, 1, 2, 22, 25, 8 + 1 * 4, iodbpmco);
- //主轴实际转数
- 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(string IP, string port)
- {
- ushort h = 0;
- int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
- List<Tool> tools = new List<Tool>();
- Tool toolEntity = new Tool();
- if (ret == Fanuc.EW_OK)
- {
- string[] grp_nos = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
- foreach (string _grp_no in grp_nos)
- {
- short grp_no = short.Parse(_grp_no);
- short num = short.Parse("10");
- Focas1.IODBTLGRP iodbtlgrp = new Focas1.IODBTLGRP();
- ret = Focas1.cnc_rdtlgrp(h, grp_no, ref num, iodbtlgrp);
- Console.WriteLine("=============================group_no====" + grp_no + "=====================");
- Console.WriteLine(num);
- //此组第一把刀的信息
- Console.WriteLine(iodbtlgrp.data1.ntool);
- Console.WriteLine(iodbtlgrp.data1.nfree);
- Console.WriteLine(iodbtlgrp.data1.life);
- Console.WriteLine(iodbtlgrp.data1.count);
- Console.WriteLine(iodbtlgrp.data1.use_tool);
- Console.WriteLine(iodbtlgrp.data1.opt_grpno);
- Console.WriteLine(iodbtlgrp.data1.life_rest);
- Console.WriteLine(iodbtlgrp.data1.rest_sig);
- //以此类推 此组第二把刀的信息
- Console.WriteLine(iodbtlgrp.data2.ntool);
- switch (iodbtlgrp.data1.count_type)
- {
- case 0:
- Console.WriteLine("count型");
- break;
- case 1:
- Console.WriteLine("minute型");
- break;
- }
- }
- var targetLifes = from t in tools select t.targetLife.ToString();
- return string.Join(",", targetLifes.ToArray());
- }
- 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(h, tool1);//刀片组的全部数量
- short b = Convert.ToInt16(group_numer);
- int ret = Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量
- ret = Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
- ret = Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
- ret = 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 if(ret == (short)Fanuc.focas_ret.EW_NOOPT)
- {
- return "0";
- }
- 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 List<AlmInfo> operationAlarm()
- {
- List<AlmInfo> stateinfo = new List<AlmInfo>();
- string IsAlarm = "0";
- //停止日志Stop sampling
- var ret = Focas1.cnc_stopophis(h);
- ushort s_no = 1;
- ushort e_no = 10;
- ushort length = 700;
- Focas1.ODBOMHIS2 odbomhis2 = new Focas1.ODBOMHIS2();
- ret = Focas1.cnc_rdomhistry2(h, s_no, e_no, length, odbomhis2);
- //System.Type type = odbomhis2.GetType();
- ushort num = odbomhis2.e_no;
- if (ret == 0)
- {
- if (num > 0)
- {
- Focas1.OPM_HIS opm_his = odbomhis2.opm_his;
- System.Type type = opm_his.GetType();
- for (int i = 1; i <= num ; i++)
- {
-
- string str1 = "data";
- str1 = str1 + i ;
- object obj = type.GetField(str1).GetValue(opm_his);
- System.Type dataType = obj.GetType();
-
-
- if (dataType.GetField("alm_msg").GetValue(obj).ToString() != "")
- {
- AlmInfo s = new AlmInfo();
- s.no = Convert.ToInt32(dataType.GetField("om_no").GetValue(obj).ToString()).ToString();
- s.msg = dataType.GetField("alm_msg").GetValue(obj).ToString();
- stateinfo.Add(s);
- }
- }
- }
-
- }
- //else
- //{
- // IsAlarm = "0";
- // AlmInfo s2 = new AlmInfo();
- // s2.no = "0";
- // s2.msg = "无报警";
- // stateinfo.Add(s2);
- // return stateinfo;
- //}
- return stateinfo;
- }
- //履历
- public List<AlmInfo> rdalmhistry5()
- {
- List<AlmInfo> stateinfo = new List<AlmInfo>();
- string IsAlarm = "0";
- //停止日志Stop sampling
- var ret = Focas1.cnc_stopophis(h);
- ushort s_no = 1;
- ushort e_no = 10;
- ushort length = 700;
- Focas1.ODBAHIS5 almHis5 = new Focas1.ODBAHIS5();
- ret = Focas1.cnc_rdalmhistry5(h, s_no, e_no, length, almHis5);
-
- ushort num = almHis5.e_no;
- if (ret == 0)
- {
- if (num > 0)
- {
- Focas1.ALM_HIS5 opm_his = almHis5.alm_his;
- System.Type type = opm_his.GetType();
- for (int i = 1; i <= num; i++)
- {
- string str1 = "data";
- str1 = str1 + i;
- object obj = type.GetField(str1).GetValue(opm_his);
- System.Type dataType = obj.GetType();
- if (dataType.GetField("alm_msg").GetValue(obj).ToString() != "")
- {
- AlmInfo s = new AlmInfo();
- s.no = Convert.ToInt32(dataType.GetField("om_no").GetValue(obj).ToString()).ToString();
- s.msg = dataType.GetField("alm_msg").GetValue(obj).ToString();
- stateinfo.Add(s);
- }
- }
- }
-
- }
- //else
- //{
- // IsAlarm = "0";
- // AlmInfo s2 = new AlmInfo();
- // s2.no = "0";
- // s2.msg = "无报警";
- // stateinfo.Add(s2);
- // return stateinfo;
- //}
- return stateinfo;
- }
- }
-
- }
|