ConDevice2.cs 18 KB

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