ConDevice2.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. 
  2. using FANUC;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using static fanuc采集.ConDevice;
  11. namespace HttpServer
  12. {
  13. public class ConDevice2
  14. {
  15. private string remotePath = "//CNC_MEM/USER/PATH1/";
  16. public ushort h = 0;
  17. public ConDevice2(string ip, string port)
  18. {
  19. Fanuc.cnc_allclibhndl3(ip, Convert.ToUInt16(port), 1, out h);
  20. }
  21. public void freehand()
  22. {
  23. Focas1.cnc_freelibhndl(h);
  24. }
  25. public string MainProg()
  26. {
  27. Focas1.ODBPRO pnum = new Focas1.ODBPRO();
  28. int ret = Focas1.cnc_rdprgnum(h, pnum);
  29. if (ret == Fanuc.EW_OK)
  30. {
  31. return pnum.data.ToString().PadLeft(4, '0');
  32. }
  33. else
  34. {
  35. return "error";
  36. }
  37. }
  38. public string Status()
  39. {
  40. Focas1.ODBST cncStatus = new Focas1.ODBST();
  41. string[] str = new string[3];
  42. int ret = Focas1.cnc_statinfo(h, cncStatus);
  43. if (ret == Fanuc.EW_OK)
  44. {
  45. return cncStatus.run.ToString();
  46. }
  47. else
  48. {
  49. return "error";
  50. }
  51. }
  52. public string Mode()
  53. {
  54. Focas1.ODBST cncStatus = new Focas1.ODBST();
  55. string[] str = new string[3];
  56. int ret = Focas1.cnc_statinfo(h, cncStatus);
  57. if (ret == Fanuc.EW_OK)
  58. {
  59. return cncStatus.aut.ToString();
  60. }
  61. else
  62. {
  63. return "error";
  64. }
  65. }
  66. public string EMG()
  67. {
  68. Focas1.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
  69. int ret = Focas1.pmc_rdpmcrng(h, 0, 0, 7, 12, 14, iodbpmc0);
  70. if (ret == Fanuc.EW_OK)
  71. {
  72. byte[] data = iodbpmc0.cdata;
  73. string value = Convert.ToString(data[1], 16).ToString().PadLeft(2, '0').ToUpper();
  74. string str = Convert.ToString(Convert.ToInt32(value.ToString().Substring(0, 1)), 2).PadLeft(4, '0').Substring(3, 1);
  75. if (str == "1")
  76. return "1";
  77. else
  78. return "0";
  79. }
  80. else
  81. {
  82. return "error";
  83. }
  84. }
  85. public string ActFeed()
  86. {
  87. //主轴进给率
  88. Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
  89. int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 12, 13, 10, iodbpmco);
  90. if (ret == Fanuc.EW_OK)
  91. {
  92. return (100 - (iodbpmco.cdata[0] - 155)).ToString();
  93. }
  94. else
  95. {
  96. return "error";
  97. }
  98. }
  99. public string FeedSpeed()
  100. {
  101. Focas1.ODBACT feedspeed = new Focas1.ODBACT();
  102. int ret = Focas1.cnc_actf(h, feedspeed);
  103. if (ret == Fanuc.EW_OK)
  104. {
  105. return feedspeed.data.ToString();
  106. }
  107. else
  108. {
  109. return "error";
  110. }
  111. }
  112. public string GetFeedrate()
  113. {
  114. Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
  115. int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 12, 13, 10, iodbpmco);
  116. if (ret == 0)
  117. {
  118. return (255 - iodbpmco.cdata[0]).ToString();
  119. }
  120. else { return "0"; }
  121. }
  122. public string spindleMagnification()
  123. {
  124. //主轴倍率
  125. Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
  126. int ret = Focas1.pmc_rdpmcrng(h, 0, 1, 30, 31, 10, iodbpmco);
  127. if (ret == Fanuc.EW_OK)
  128. {
  129. return iodbpmco.cdata[0].ToString();
  130. }
  131. else
  132. {
  133. return "error";
  134. }
  135. }
  136. public string SpindleLoad()
  137. {
  138. //主轴负载
  139. String loaderX = this.ServoLoadX();
  140. String loaderY = this.ServoLoadY();
  141. String loaderZ = this.ServoLoadZ();
  142. if (loaderX == "error" || loaderY == "error" || loaderZ == "error") return "error";
  143. return loaderX + "/" + loaderY + "/" + loaderZ;
  144. }
  145. public string ActSpindle()
  146. {
  147. //主轴转数 pmc_rdpmcrng(hFanuc, 1, 2, 22, 25, 8 + 1 * 4, iodbpmco);
  148. //主轴实际转数
  149. Focas1.ODBACT pindle = new Focas1.ODBACT();
  150. int ret = Focas1.cnc_acts(h, pindle);
  151. if (ret == Fanuc.EW_OK)
  152. {
  153. return pindle.data.ToString();
  154. }
  155. else
  156. {
  157. return "error";
  158. }
  159. }
  160. public string ServoLoadX()
  161. {
  162. Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
  163. short a = 6;//伺服轴的数量
  164. int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
  165. if (ret == Fanuc.EW_OK)
  166. {
  167. return sv.svload1.data.ToString();
  168. }
  169. else
  170. {
  171. return "error";
  172. }
  173. }
  174. public string ServoLoadY()
  175. {
  176. Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
  177. short a = 6;//伺服轴的数量
  178. int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
  179. if (ret == Fanuc.EW_OK)
  180. {
  181. return sv.svload2.data.ToString();
  182. }
  183. else
  184. {
  185. return "error";
  186. }
  187. }
  188. public string ServoLoadZ()
  189. {
  190. Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
  191. short a = 6;//伺服轴的数量
  192. int ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
  193. if (ret == Fanuc.EW_OK)
  194. {
  195. return sv.svload3.data.ToString();
  196. }
  197. else
  198. {
  199. return "error";
  200. }
  201. }
  202. public string PowerOnTime()
  203. {
  204. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  205. int ret = Focas1.cnc_rdparam(h, 6750, 0, 8, iodbpsd);
  206. if (ret == Fanuc.EW_OK)
  207. {
  208. return iodbpsd.ldata.ToString();
  209. }
  210. else
  211. {
  212. return "error";
  213. }
  214. }
  215. public string AccumulateCuttingTime()
  216. {
  217. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  218. int ret = Focas1.cnc_rdparam(h, 6754, 0, 8, iodbpsd);
  219. if (ret == Fanuc.EW_OK)
  220. {
  221. return iodbpsd.ldata.ToString();
  222. }
  223. else
  224. {
  225. return "error";
  226. }
  227. }
  228. public string CuttingTimePerCycle()
  229. {
  230. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  231. int min = 0;
  232. int sec = 0;
  233. int ret = Focas1.cnc_rdparam(h, 6757, 0, 8, iodbpsd);//秒
  234. if (ret == Focas1.EW_OK)
  235. {
  236. sec = iodbpsd.ldata;
  237. ret = Focas1.cnc_rdparam(h, 6758, 0, 8, iodbpsd);//分
  238. if (ret == Focas1.EW_OK)
  239. {
  240. min = iodbpsd.ldata;
  241. return (min * 60 * 1000 + sec).ToString();
  242. }
  243. else
  244. { return "error"; }
  245. }
  246. else { return "error"; }
  247. }
  248. public string WorkTime()
  249. {
  250. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  251. int ret = Focas1.cnc_rdparam(h, 6752, -1, 8, iodbpsd);
  252. if (ret == Fanuc.EW_OK)
  253. {
  254. return iodbpsd.ldata.ToString();
  255. }
  256. else
  257. {
  258. return "error";
  259. }
  260. }
  261. public string Part_Count()
  262. {
  263. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  264. int ret = Focas1.cnc_rdparam(h, 6712, 0, 8, iodbpsd);
  265. if (ret == Fanuc.EW_OK)
  266. {
  267. return iodbpsd.ldata.ToString();
  268. }
  269. else
  270. {
  271. return "error";
  272. }
  273. }
  274. public string ToolNo()
  275. {
  276. Focas1.ODBM odb = new Focas1.ODBM();
  277. int ret = Focas1.cnc_rdmacro(h, 523, 10, odb);
  278. if (ret == Fanuc.EW_OK)
  279. {
  280. return (odb.mcr_val * Math.Pow(10, -odb.dec_val)).ToString();
  281. }
  282. else
  283. {
  284. return "error";
  285. }
  286. }
  287. public string ToolLife(string IP, string port)
  288. {
  289. ushort h = 0;
  290. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  291. List<Tool> tools = new List<Tool>();
  292. Tool toolEntity = new Tool();
  293. if (ret == Fanuc.EW_OK)
  294. {
  295. 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" };
  296. foreach (string _grp_no in grp_nos)
  297. {
  298. short grp_no = short.Parse(_grp_no);
  299. short num = short.Parse("10");
  300. Focas1.IODBTLGRP iodbtlgrp = new Focas1.IODBTLGRP();
  301. ret = Focas1.cnc_rdtlgrp(h, grp_no, ref num, iodbtlgrp);
  302. Console.WriteLine("=============================group_no====" + grp_no + "=====================");
  303. Console.WriteLine(num);
  304. //此组第一把刀的信息
  305. Console.WriteLine(iodbtlgrp.data1.ntool);
  306. Console.WriteLine(iodbtlgrp.data1.nfree);
  307. Console.WriteLine(iodbtlgrp.data1.life);
  308. Console.WriteLine(iodbtlgrp.data1.count);
  309. Console.WriteLine(iodbtlgrp.data1.use_tool);
  310. Console.WriteLine(iodbtlgrp.data1.opt_grpno);
  311. Console.WriteLine(iodbtlgrp.data1.life_rest);
  312. Console.WriteLine(iodbtlgrp.data1.rest_sig);
  313. //以此类推 此组第二把刀的信息
  314. Console.WriteLine(iodbtlgrp.data2.ntool);
  315. switch (iodbtlgrp.data1.count_type)
  316. {
  317. case 0:
  318. Console.WriteLine("count型");
  319. break;
  320. case 1:
  321. Console.WriteLine("minute型");
  322. break;
  323. }
  324. }
  325. var targetLifes = from t in tools select t.targetLife.ToString();
  326. return string.Join(",", targetLifes.ToArray());
  327. }
  328. else { return "error"; }
  329. }
  330. public string ToolLife()
  331. {
  332. Fanuc.ODBTLIFE5 tool = new Focas1.ODBTLIFE5();//读取刀片组的序号
  333. Fanuc.ODBTLIFE2 tool1 = new Focas1.ODBTLIFE2();//读取刀片组的全部数量
  334. Fanuc.ODBTLIFE3 tool2 = new Focas1.ODBTLIFE3();//刀具的数量
  335. Fanuc.ODBTG btg = new Focas1.ODBTG();
  336. Fanuc.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
  337. int m = 2;
  338. Fanuc.cnc_rdgrpid2(h, m, tool);
  339. int group_numer = tool.data;
  340. int all = Fanuc.cnc_rdngrp(h, tool1);//刀片组的全部数量
  341. short b = Convert.ToInt16(group_numer);
  342. int ret = Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量
  343. ret = Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
  344. ret = Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
  345. ret = Fanuc.cnc_rdtoolgrp(h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息,很重要;
  346. //int ret =Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号;
  347. if (ret == Fanuc.EW_OK)
  348. {
  349. return tool2.data.ToString();
  350. }
  351. else if(ret == (short)Fanuc.focas_ret.EW_NOOPT)
  352. {
  353. return "0";
  354. }
  355. else
  356. {
  357. return "error";
  358. }
  359. }
  360. public List<AlmInfo> AlmMsg( out string IsAlarm)
  361. {
  362. List<AlmInfo> stateinfo = new List<AlmInfo>();
  363. IsAlarm = "0";
  364. ushort h = 0;
  365. short b = -1;//哪一类报警,-1所有报警
  366. short num = 11;//设大一些没关系
  367. Focas1.ODBALMMSG odbalmmsg2 = new Focas1.ODBALMMSG();
  368. int ret = Focas1.cnc_rdalmmsg(h, b, ref num, odbalmmsg2);
  369. System.Type type = odbalmmsg2.GetType();
  370. if (ret == 0)
  371. {
  372. if (num > 0)
  373. {
  374. IsAlarm = "1";
  375. for (int i = 0; i <= num - 1; i++)
  376. {
  377. string str1 = "msg";
  378. str1 = str1 + (i + 1);
  379. object obj = type.GetField(str1).GetValue(odbalmmsg2);
  380. //if(obj.)
  381. System.Type type1 = obj.GetType();
  382. if (type1.GetField("alm_msg").GetValue(obj).ToString() != "")
  383. {
  384. AlmInfo s = new AlmInfo();
  385. s.no = Convert.ToInt32(type1.GetField("alm_no").GetValue(obj).ToString()).ToString();
  386. s.msg = type1.GetField("alm_msg").GetValue(obj).ToString();
  387. stateinfo.Add(s);
  388. }
  389. }
  390. return stateinfo;
  391. }
  392. else
  393. {
  394. IsAlarm = "0";
  395. AlmInfo s2 = new AlmInfo();
  396. s2.no = "0";
  397. s2.msg = "无报警";
  398. stateinfo.Add(s2);
  399. return stateinfo;
  400. }
  401. }
  402. else
  403. {
  404. IsAlarm = "0";
  405. AlmInfo s3 = new AlmInfo();
  406. s3.no = "0";
  407. s3.msg = "无报警";
  408. stateinfo.Add(s3);
  409. return stateinfo;
  410. }
  411. }
  412. //履历信息
  413. public List<AlmInfo> operationAlarm()
  414. {
  415. List<AlmInfo> stateinfo = new List<AlmInfo>();
  416. string IsAlarm = "0";
  417. //停止日志Stop sampling
  418. var ret = Focas1.cnc_stopophis(h);
  419. ushort s_no = 1;
  420. ushort e_no = 10;
  421. ushort length = 700;
  422. Focas1.ODBOMHIS2 odbomhis2 = new Focas1.ODBOMHIS2();
  423. ret = Focas1.cnc_rdomhistry2(h, s_no, e_no, length, odbomhis2);
  424. //System.Type type = odbomhis2.GetType();
  425. ushort num = odbomhis2.e_no;
  426. if (ret == 0)
  427. {
  428. if (num > 0)
  429. {
  430. Focas1.OPM_HIS opm_his = odbomhis2.opm_his;
  431. System.Type type = opm_his.GetType();
  432. for (int i = 1; i <= num ; i++)
  433. {
  434. string str1 = "data";
  435. str1 = str1 + i ;
  436. object obj = type.GetField(str1).GetValue(opm_his);
  437. System.Type dataType = obj.GetType();
  438. if (dataType.GetField("alm_msg").GetValue(obj).ToString() != "")
  439. {
  440. AlmInfo s = new AlmInfo();
  441. s.no = Convert.ToInt32(dataType.GetField("om_no").GetValue(obj).ToString()).ToString();
  442. s.msg = dataType.GetField("alm_msg").GetValue(obj).ToString();
  443. stateinfo.Add(s);
  444. }
  445. }
  446. }
  447. }
  448. //else
  449. //{
  450. // IsAlarm = "0";
  451. // AlmInfo s2 = new AlmInfo();
  452. // s2.no = "0";
  453. // s2.msg = "无报警";
  454. // stateinfo.Add(s2);
  455. // return stateinfo;
  456. //}
  457. return stateinfo;
  458. }
  459. //履历
  460. public List<AlmInfo> rdalmhistry5()
  461. {
  462. List<AlmInfo> stateinfo = new List<AlmInfo>();
  463. string IsAlarm = "0";
  464. //停止日志Stop sampling
  465. var ret = Focas1.cnc_stopophis(h);
  466. ushort s_no = 1;
  467. ushort e_no = 10;
  468. ushort length = 700;
  469. Focas1.ODBAHIS5 almHis5 = new Focas1.ODBAHIS5();
  470. ret = Focas1.cnc_rdalmhistry5(h, s_no, e_no, length, almHis5);
  471. ushort num = almHis5.e_no;
  472. if (ret == 0)
  473. {
  474. if (num > 0)
  475. {
  476. Focas1.ALM_HIS5 opm_his = almHis5.alm_his;
  477. System.Type type = opm_his.GetType();
  478. for (int i = 1; i <= num; i++)
  479. {
  480. string str1 = "data";
  481. str1 = str1 + i;
  482. object obj = type.GetField(str1).GetValue(opm_his);
  483. System.Type dataType = obj.GetType();
  484. if (dataType.GetField("alm_msg").GetValue(obj).ToString() != "")
  485. {
  486. AlmInfo s = new AlmInfo();
  487. s.no = Convert.ToInt32(dataType.GetField("om_no").GetValue(obj).ToString()).ToString();
  488. s.msg = dataType.GetField("alm_msg").GetValue(obj).ToString();
  489. stateinfo.Add(s);
  490. }
  491. }
  492. }
  493. }
  494. //else
  495. //{
  496. // IsAlarm = "0";
  497. // AlmInfo s2 = new AlmInfo();
  498. // s2.no = "0";
  499. // s2.msg = "无报警";
  500. // stateinfo.Add(s2);
  501. // return stateinfo;
  502. //}
  503. return stateinfo;
  504. }
  505. }
  506. }