ConDevice2.cs 17 KB

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