123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- using IMCS.Device.FanucBase;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using YG.Device;
- using static IMCS.Device.FanucBase.Focas1;
- namespace IMCS.Device
- {
- public class Fanuc : Focas1
- {
- //下载程序 5-27
- //开始
- private static short dwnstart(ushort handle, short type)
- {
- return Fanuc.cnc_dwnstart3(handle, type);
- }
- //结束
- private static short dwnend(ushort handle)
- {
- return Fanuc.cnc_dwnend3(handle);
- }
- //下载
- private static short dwnload(ushort handle, ref int datalength, string data)
- {
- //开始下载程序 datalength将会被返回,实际的输出的字符数量
- return Fanuc.cnc_download3(handle, ref datalength, data);
- }
- //获取详细的错误信息
- private static short getdtailerr(ushort handle, Fanuc.ODBERR odberr)
- {
- return Fanuc.cnc_getdtailerr(handle, odberr);
- }
- //下载程序的入口点
- /// <summary>
- /// 向CNC下载指定类型的程序
- /// </summary>
- /// <param name="handle">句柄</param>
- /// <param name="type">程序类型</param>
- /// <param name="data">程序的内容</param>
- /// <param name="odberr">保存返回错误信息的详细内容,为null不返回</param>
- /// <returns>错误码</returns>
- public static short download(ushort handle, short type, string data, Fanuc.ODBERR odberr)
- {
- int datalength = data.Length;
- short ret = dwnstart(handle, type);
- if (ret == 0)
- {
- int olddata = datalength;
- while (true)
- {
- ret = dwnload(handle, ref datalength, data);
- //说明缓存已满或为空,继续尝试
- if (ret == (short)Fanuc.focas_ret.EW_BUFFER)
- {
- continue;
- }
- if (ret == Fanuc.EW_OK)
- {
- //说明当前下载完成,temp记录剩余下载量
- int temp = olddata - datalength;
- if (temp <= 0)
- {
- break;
- }
- else
- {
- data = data.Substring(datalength, temp);
- datalength = temp; olddata = temp;
- }
- }
- else
- {
- //下载出现错误,解析出具体的错误信息
- if (odberr != null)
- {
- getdtailerr(handle, odberr);
- }
- //下载出错
- break;
- }
- }
- //判断是哪里出的问题
- if (ret == 0)
- {
- ret = dwnend(handle);
- //结束下载出错
- return ret;
- }
- else
- {
- dwnend(handle);
- //下载出错
- return ret;
- }
- }
- else
- {
- dwnend(handle);
- //开始下载出错
- return ret;
- }
- }
- //下载程序 5-27
- //上传程序 5-28
- //开始
- private static short upstart(ushort handle, short type, int startno, int endno)
- {
- return cnc_upstart3(handle, type, startno, endno);
- }
- //上传
- private static short uplod(ushort handle, int length, char[] databuf)
- {
- return cnc_upload3(handle, ref length, databuf);
- }
- //结束
- private static short upend(ushort handle)
- {
- return cnc_upend3(handle);
- }
- //上传程序的入口
- /// <summary>
- /// 根据程序号读取指定程序
- /// </summary>
- /// <param name="handle">句柄</param>
- /// <param name="type">类型</param>
- /// <param name="no">程序号</param>
- /// <param name="odberr">详细错误内容,null不返回</param>
- /// <param name="data">返回的程序内容</param>
- /// <returns>错误码</returns>
- public static short upload(ushort handle, short type, int no, ref string data, Fanuc.ODBERR odberr)
- {
- int startno = no; int endno = no;
- int length = 256; char[] databuf = new char[256];
- short ret = upstart(handle, type, startno, endno);
- if (ret == Fanuc.EW_OK)
- {
- string temp = "";
- while (true)
- {
- //上传
- ret = uplod(handle, length, databuf);
- temp = new string(databuf);
- int one = temp.Length;
- if (ret == (short)Fanuc.focas_ret.EW_BUFFER)
- {
- continue;
- }
- if (ret == Fanuc.EW_OK)
- {
- temp = temp.Replace("\0", "");
- data += temp;
- string lastchar = temp.Substring(temp.Length - 1, 1);
- if (lastchar == "%")
- {
- break;
- }
- }
- else
- {
- //下载出现错误,解析出具体的错误信息
- if (odberr != null)
- {
- getdtailerr(handle, odberr);
- }
- //下载出错
- break;
- }
- }
- //判断是哪里出的问题
- if (ret == 0)
- {
- ret = upend(handle);
- //结束上传出错
- return ret;
- }
- else
- {
- upend(handle);
- //上传出错
- return ret;
- }
- }
- else
- {
- //开始出错
- upend(handle);
- return ret;
- }
- }
- //上传程序 5-28
- //根据alm_grp 编号 返回 提示内容 简
- public static string getalmgrp(short no)
- {
- string ret = "";
- switch (no)
- {
- case 0:
- ret = "SW";
- break;
- case 1:
- ret = "PW";
- break;
- case 2:
- ret = "IO";
- break;
- case 3:
- ret = "PS";
- break;
- case 4:
- ret = "OT";
- break;
- case 5:
- ret = "OH";
- break;
- case 6:
- ret = "SV";
- break;
- case 7:
- ret = "SR";
- break;
- case 8:
- ret = "MC";
- break;
- case 9:
- ret = "SP";
- break;
- case 10:
- ret = "DS";
- break;
- case 11:
- ret = "IE";
- break;
- case 12:
- ret = "BG";
- break;
- case 13:
- ret = "SN";
- break;
- case 14:
- ret = "reserved";
- break;
- case 15:
- ret = "EX";
- break;
- case 19:
- ret = "PC";
- break;
- default:
- ret = "Not used";
- break;
- }
- return ret;
- }
- //根据alm_grp 编号 返回 提示内容 简
- //2016-6-2 根据地址码和地址号,返回完整的显示信息
- public static string getpmcadd(short a, ushort b)
- {
- string tempa = "";
- switch (a)
- {
- case 0:
- tempa = "G";
- break;
- case 1:
- tempa = "F";
- break;
- case 2:
- tempa = "Y";
- break;
- case 3:
- tempa = "X";
- break;
- case 4:
- tempa = "A";
- break;
- case 5:
- tempa = "R";
- break;
- case 6:
- tempa = "T";
- break;
- case 7:
- tempa = "K";
- break;
- case 8:
- tempa = "C";
- break;
- case 9:
- tempa = "D";
- break;
- default:
- tempa = "n";
- break;
- }
- string tempb = b.ToString().PadLeft(4, '0');
- return tempa + tempb;
- }
- //2016-6-2 根据地址码和地址号,返回完整的显示信息
- }
- /// <summary>
- /// 发那科
- /// </summary>
- public class DeviceFanucHandle
- {
- public bool IsOn { get; set; }
- public ushort h;
- public DeviceFanucHandle(string ip, int port, int outtime = 10)
- {
- int ret = Fanuc.cnc_allclibhndl3(ip, Convert.ToUInt16(port), outtime, out h);
- if (ret == Fanuc.EW_OK)
- {
- IsOn = true;
- YG.Log.Instance.WriteLogAdd($"Fanuc连接成功--->{DateTime.Now}");
- }
- else
- {
- IsOn = false;
- YG.Log.Instance.WriteLogAdd($"Fanuc连接失败--->{DateTime.Now}");
- }
- }
- /// <summary>
- /// 关闭连接
- /// </summary>
- /// <returns></returns>
- public bool Fanuc_Close()
- {
- bool result = false;
- if (IsOn)
- {
- int ret = Fanuc.cnc_freelibhndl(h);
- if (ret == Fanuc.EW_OK)
- {
- YG.Log.Instance.WriteLogAdd("断开连接成功!");
- result = true;
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"{ret}--->断开连接失败-->>{DateTime.Now}");
- }
- }
- return result;
- }
- /// <summary>
- /// 读取正在执行的程序名称
- /// </summary>
- /// <returns></returns>
- public bool Fanuc_ReadCurrentProc()
- {
- bool result = false;
- ODBEXEPRG oDBEXEPRG = new ODBEXEPRG();
- short ret = Fanuc.cnc_exeprgname(h, oDBEXEPRG);
- if (ret == Fanuc.EW_OK)
- {
- YG.Log.Instance.WriteLogAdd("成功读取当前正在执行的程序名称!");
- result = true;
- }
- else
- {
- YG.Log.Instance.WriteLogAdd($"{ret}--->读取当前正在执行的程序名称失败-->>{DateTime.Now}");
- }
- return result;
- }
- /// <summary>
- /// 读取当前机床刀具信息
- /// </summary>
- /// <returns></returns>
- public bool Fanuc_ReadTool()
- {
- bool result = false;
- get_tool_life();
- return result;
- }
- /// <summary>
- /// 读取获得刀片的有关信息
- /// </summary>
- private void get_tool_life()//
- {
- 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);
- 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);//根据刀组号读出所有信息,很重要;
- Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号;
- System.Console.WriteLine("刀片组号:" + group_numer);
- System.Console.WriteLine("type:" + tool.type);
- System.Console.WriteLine("刀片组的全部数量" + all);
- System.Console.WriteLine("刀片号:" + tool2.data);
- System.Console.WriteLine("刀片组号;" + group_numer + " 寿命:" + tool2.data);
- System.Console.WriteLine("刀片组号;" + group_numer + " 寿命计时:" + tool2.data);
- }
-
- }
- }
|