ConDevice2.cs 19 KB

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