DeviceFanucHandle.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using IMCS.Device.FanucBase;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using YG.Device;
  8. using static IMCS.Device.FanucBase.Focas1;
  9. namespace IMCS.Device
  10. {
  11. public class Fanuc : Focas1
  12. {
  13. //下载程序 5-27
  14. //开始
  15. private static short dwnstart(ushort handle, short type)
  16. {
  17. return Fanuc.cnc_dwnstart3(handle, type);
  18. }
  19. //结束
  20. private static short dwnend(ushort handle)
  21. {
  22. return Fanuc.cnc_dwnend3(handle);
  23. }
  24. //下载
  25. private static short dwnload(ushort handle, ref int datalength, string data)
  26. {
  27. //开始下载程序 datalength将会被返回,实际的输出的字符数量
  28. return Fanuc.cnc_download3(handle, ref datalength, data);
  29. }
  30. //获取详细的错误信息
  31. private static short getdtailerr(ushort handle, Fanuc.ODBERR odberr)
  32. {
  33. return Fanuc.cnc_getdtailerr(handle, odberr);
  34. }
  35. //下载程序的入口点
  36. /// <summary>
  37. /// 向CNC下载指定类型的程序
  38. /// </summary>
  39. /// <param name="handle">句柄</param>
  40. /// <param name="type">程序类型</param>
  41. /// <param name="data">程序的内容</param>
  42. /// <param name="odberr">保存返回错误信息的详细内容,为null不返回</param>
  43. /// <returns>错误码</returns>
  44. public static short download(ushort handle, short type, string data, Fanuc.ODBERR odberr)
  45. {
  46. int datalength = data.Length;
  47. short ret = dwnstart(handle, type);
  48. if (ret == 0)
  49. {
  50. int olddata = datalength;
  51. while (true)
  52. {
  53. ret = dwnload(handle, ref datalength, data);
  54. //说明缓存已满或为空,继续尝试
  55. if (ret == (short)Fanuc.focas_ret.EW_BUFFER)
  56. {
  57. continue;
  58. }
  59. if (ret == Fanuc.EW_OK)
  60. {
  61. //说明当前下载完成,temp记录剩余下载量
  62. int temp = olddata - datalength;
  63. if (temp <= 0)
  64. {
  65. break;
  66. }
  67. else
  68. {
  69. data = data.Substring(datalength, temp);
  70. datalength = temp; olddata = temp;
  71. }
  72. }
  73. else
  74. {
  75. //下载出现错误,解析出具体的错误信息
  76. if (odberr != null)
  77. {
  78. getdtailerr(handle, odberr);
  79. }
  80. //下载出错
  81. break;
  82. }
  83. }
  84. //判断是哪里出的问题
  85. if (ret == 0)
  86. {
  87. ret = dwnend(handle);
  88. //结束下载出错
  89. return ret;
  90. }
  91. else
  92. {
  93. dwnend(handle);
  94. //下载出错
  95. return ret;
  96. }
  97. }
  98. else
  99. {
  100. dwnend(handle);
  101. //开始下载出错
  102. return ret;
  103. }
  104. }
  105. //下载程序 5-27
  106. //上传程序 5-28
  107. //开始
  108. private static short upstart(ushort handle, short type, int startno, int endno)
  109. {
  110. return cnc_upstart3(handle, type, startno, endno);
  111. }
  112. //上传
  113. private static short uplod(ushort handle, int length, char[] databuf)
  114. {
  115. return cnc_upload3(handle, ref length, databuf);
  116. }
  117. //结束
  118. private static short upend(ushort handle)
  119. {
  120. return cnc_upend3(handle);
  121. }
  122. //上传程序的入口
  123. /// <summary>
  124. /// 根据程序号读取指定程序
  125. /// </summary>
  126. /// <param name="handle">句柄</param>
  127. /// <param name="type">类型</param>
  128. /// <param name="no">程序号</param>
  129. /// <param name="odberr">详细错误内容,null不返回</param>
  130. /// <param name="data">返回的程序内容</param>
  131. /// <returns>错误码</returns>
  132. public static short upload(ushort handle, short type, int no, ref string data, Fanuc.ODBERR odberr)
  133. {
  134. int startno = no; int endno = no;
  135. int length = 256; char[] databuf = new char[256];
  136. short ret = upstart(handle, type, startno, endno);
  137. if (ret == Fanuc.EW_OK)
  138. {
  139. string temp = "";
  140. while (true)
  141. {
  142. //上传
  143. ret = uplod(handle, length, databuf);
  144. temp = new string(databuf);
  145. int one = temp.Length;
  146. if (ret == (short)Fanuc.focas_ret.EW_BUFFER)
  147. {
  148. continue;
  149. }
  150. if (ret == Fanuc.EW_OK)
  151. {
  152. temp = temp.Replace("\0", "");
  153. data += temp;
  154. string lastchar = temp.Substring(temp.Length - 1, 1);
  155. if (lastchar == "%")
  156. {
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. //下载出现错误,解析出具体的错误信息
  163. if (odberr != null)
  164. {
  165. getdtailerr(handle, odberr);
  166. }
  167. //下载出错
  168. break;
  169. }
  170. }
  171. //判断是哪里出的问题
  172. if (ret == 0)
  173. {
  174. ret = upend(handle);
  175. //结束上传出错
  176. return ret;
  177. }
  178. else
  179. {
  180. upend(handle);
  181. //上传出错
  182. return ret;
  183. }
  184. }
  185. else
  186. {
  187. //开始出错
  188. upend(handle);
  189. return ret;
  190. }
  191. }
  192. //上传程序 5-28
  193. //根据alm_grp 编号 返回 提示内容 简
  194. public static string getalmgrp(short no)
  195. {
  196. string ret = "";
  197. switch (no)
  198. {
  199. case 0:
  200. ret = "SW";
  201. break;
  202. case 1:
  203. ret = "PW";
  204. break;
  205. case 2:
  206. ret = "IO";
  207. break;
  208. case 3:
  209. ret = "PS";
  210. break;
  211. case 4:
  212. ret = "OT";
  213. break;
  214. case 5:
  215. ret = "OH";
  216. break;
  217. case 6:
  218. ret = "SV";
  219. break;
  220. case 7:
  221. ret = "SR";
  222. break;
  223. case 8:
  224. ret = "MC";
  225. break;
  226. case 9:
  227. ret = "SP";
  228. break;
  229. case 10:
  230. ret = "DS";
  231. break;
  232. case 11:
  233. ret = "IE";
  234. break;
  235. case 12:
  236. ret = "BG";
  237. break;
  238. case 13:
  239. ret = "SN";
  240. break;
  241. case 14:
  242. ret = "reserved";
  243. break;
  244. case 15:
  245. ret = "EX";
  246. break;
  247. case 19:
  248. ret = "PC";
  249. break;
  250. default:
  251. ret = "Not used";
  252. break;
  253. }
  254. return ret;
  255. }
  256. //根据alm_grp 编号 返回 提示内容 简
  257. //2016-6-2 根据地址码和地址号,返回完整的显示信息
  258. public static string getpmcadd(short a, ushort b)
  259. {
  260. string tempa = "";
  261. switch (a)
  262. {
  263. case 0:
  264. tempa = "G";
  265. break;
  266. case 1:
  267. tempa = "F";
  268. break;
  269. case 2:
  270. tempa = "Y";
  271. break;
  272. case 3:
  273. tempa = "X";
  274. break;
  275. case 4:
  276. tempa = "A";
  277. break;
  278. case 5:
  279. tempa = "R";
  280. break;
  281. case 6:
  282. tempa = "T";
  283. break;
  284. case 7:
  285. tempa = "K";
  286. break;
  287. case 8:
  288. tempa = "C";
  289. break;
  290. case 9:
  291. tempa = "D";
  292. break;
  293. default:
  294. tempa = "n";
  295. break;
  296. }
  297. string tempb = b.ToString().PadLeft(4, '0');
  298. return tempa + tempb;
  299. }
  300. //2016-6-2 根据地址码和地址号,返回完整的显示信息
  301. }
  302. /// <summary>
  303. /// 发那科
  304. /// </summary>
  305. public class DeviceFanucHandle
  306. {
  307. public bool IsOn { get; set; }
  308. public ushort h;
  309. public DeviceFanucHandle(string ip, int port, int outtime = 10)
  310. {
  311. int ret = Fanuc.cnc_allclibhndl3(ip, Convert.ToUInt16(port), outtime, out h);
  312. if (ret == Fanuc.EW_OK)
  313. {
  314. IsOn = true;
  315. YG.Log.Instance.WriteLogAdd($"Fanuc连接成功--->{DateTime.Now}");
  316. }
  317. else
  318. {
  319. IsOn = false;
  320. YG.Log.Instance.WriteLogAdd($"Fanuc连接失败--->{DateTime.Now}");
  321. }
  322. }
  323. /// <summary>
  324. /// 关闭连接
  325. /// </summary>
  326. /// <returns></returns>
  327. public bool Fanuc_Close()
  328. {
  329. bool result = false;
  330. if (IsOn)
  331. {
  332. int ret = Fanuc.cnc_freelibhndl(h);
  333. if (ret == Fanuc.EW_OK)
  334. {
  335. YG.Log.Instance.WriteLogAdd("断开连接成功!");
  336. result = true;
  337. }
  338. else
  339. {
  340. YG.Log.Instance.WriteLogAdd($"{ret}--->断开连接失败-->>{DateTime.Now}");
  341. }
  342. }
  343. return result;
  344. }
  345. /// <summary>
  346. /// 读取正在执行的程序名称
  347. /// </summary>
  348. /// <returns></returns>
  349. public bool Fanuc_ReadCurrentProc()
  350. {
  351. bool result = false;
  352. ODBEXEPRG oDBEXEPRG = new ODBEXEPRG();
  353. short ret = Fanuc.cnc_exeprgname(h, oDBEXEPRG);
  354. if (ret == Fanuc.EW_OK)
  355. {
  356. YG.Log.Instance.WriteLogAdd("成功读取当前正在执行的程序名称!");
  357. result = true;
  358. }
  359. else
  360. {
  361. YG.Log.Instance.WriteLogAdd($"{ret}--->读取当前正在执行的程序名称失败-->>{DateTime.Now}");
  362. }
  363. return result;
  364. }
  365. /// <summary>
  366. /// 读取当前机床刀具信息
  367. /// </summary>
  368. /// <returns></returns>
  369. public bool Fanuc_ReadTool()
  370. {
  371. bool result = false;
  372. get_tool_life();
  373. return result;
  374. }
  375. /// <summary>
  376. /// 读取获得刀片的有关信息
  377. /// </summary>
  378. private void get_tool_life()//
  379. {
  380. Fanuc.ODBTLIFE5 tool = new Focas1.ODBTLIFE5();//读取刀片组的序号
  381. Fanuc.ODBTLIFE2 tool1 = new Focas1.ODBTLIFE2();//
  382. Fanuc.ODBTLIFE3 tool2 = new Focas1.ODBTLIFE3();//刀具的数量
  383. Fanuc.ODBTG btg = new Focas1.ODBTG();
  384. Fanuc.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
  385. int m = 2;
  386. //读取刀具寿命管理数据
  387. Fanuc.cnc_rdgrpid2(h, m, tool);
  388. int group_numer = tool.data;
  389. int all = Fanuc.cnc_rdngrp(h, tool1);//刀片组的全部数量
  390. short b = Convert.ToInt16(group_numer);
  391. Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量
  392. Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
  393. Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
  394. Fanuc.cnc_rdtoolgrp(h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息,很重要;
  395. Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号;
  396. System.Console.WriteLine("刀片组号:" + group_numer);
  397. System.Console.WriteLine("type:" + tool.type);
  398. System.Console.WriteLine("刀片组的全部数量" + all);
  399. System.Console.WriteLine("刀片号:" + tool2.data);
  400. System.Console.WriteLine("刀片组号;" + group_numer + " 寿命:" + tool2.data);
  401. System.Console.WriteLine("刀片组号;" + group_numer + " 寿命计时:" + tool2.data);
  402. }
  403. }
  404. }