ConDevice.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. 
  2. using FANUC;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.Configuration;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Threading;
  10. namespace fanuc采集
  11. {
  12. public class ConDevice
  13. {
  14. private int FANUC_RETARDO_MANUAL = 20;
  15. private int FANUC_RETARDO = 200;
  16. //发那科远程文件路径
  17. private string FanucRemotePath = "//CNC_MEM/USER/PATH1/";
  18. //发那科本地文件路径
  19. private string FanucLocalPath = "D:\\myFtp\\";
  20. //pmc位置类型
  21. Dictionary<int, String> pmcAddress = new Dictionary<int, string>();
  22. //不能删除程序
  23. StringCollection unDelProcs = new StringCollection();
  24. public ConDevice() {
  25. pmcAddress.Add(0, "G");
  26. pmcAddress.Add(1, "F");
  27. pmcAddress.Add(2, "Y");
  28. pmcAddress.Add(3, "X");
  29. pmcAddress.Add(4, "A");
  30. pmcAddress.Add(5, "R");
  31. pmcAddress.Add(6, "T");
  32. pmcAddress.Add(7, "K");
  33. pmcAddress.Add(8, "C");
  34. pmcAddress.Add(9, "D");
  35. unDelProcs.Add("O1");
  36. unDelProcs.Add("O7");
  37. unDelProcs.Add("O1234");
  38. unDelProcs.Add("O1235");
  39. unDelProcs.Add("O7999");
  40. unDelProcs.Add("O8000");
  41. unDelProcs.Add("O8001");
  42. unDelProcs.Add("O8002");
  43. unDelProcs.Add("O8003");
  44. unDelProcs.Add("O8004");
  45. unDelProcs.Add("O8005");
  46. unDelProcs.Add("O8006");
  47. unDelProcs.Add("O8014");
  48. unDelProcs.Add("O7999");
  49. }
  50. //选择程序
  51. public int SelectedNcProg(string IP, string port,string fileName)
  52. {
  53. ushort h = 0;
  54. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h); //object file_path = "//CNC_MEM/USER/PATH1/O0999";
  55. if (ret == Focas1.EW_OK)
  56. {
  57. string fullPath = @FanucLocalPath + fileName;
  58. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);
  59. ret = Focas1.cnc_pdf_slctmain(h, FanucRemotePath + "O" + fileNameWithoutExtension);
  60. Fanuc.cnc_freelibhndl(h);
  61. }
  62. return ret;
  63. }
  64. //删除程序
  65. public int DeleteNcProg(string IP, string port, string prgname)
  66. {
  67. ushort h = 0;
  68. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  69. if (ret == Focas1.EW_OK)
  70. {
  71. Fanuc.cnc_freelibhndl(h);
  72. ret = Focas1.cnc_pdf_del(h, FanucRemotePath + prgname);
  73. }
  74. return ret;
  75. }
  76. //上传程序
  77. public int UploadNcProg(string IP, string port, string prgname)
  78. {
  79. string fullPath = @FanucLocalPath + prgname;
  80. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);
  81. string prg = File.ReadAllText(fullPath);
  82. ushort h = 0;
  83. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 120, out h);
  84. if (ret == Focas1.EW_OK)
  85. {
  86. int n = 0;
  87. // Focas1.cnc_pdf_slctmain(h, FanucRemotePath + "O" + fileNameWithoutExtension); ////CNC_MEM/USER/PATH1/O111
  88. //主程序换成临时文件
  89. ret = Focas1.cnc_pdf_slctmain(h, FanucRemotePath + "O7999");
  90. //查询所有程序,删除已存在文件
  91. StringCollection procs = GetAllProgrammFileName(FanucRemotePath, h);
  92. if (procs != null && procs.Count > 0)
  93. {
  94. foreach (string proc in procs)
  95. {
  96. if (!unDelProcs.Contains(proc))
  97. {
  98. ret = Focas1.cnc_pdf_del(h, FanucRemotePath + proc);
  99. }
  100. }
  101. }
  102. //开始上传
  103. ret = Focas1.cnc_dwnstart4(h, 0, FanucRemotePath);
  104. //ret = Focas1.cnc_dwnstart3(h, 0);
  105. if (ret != Focas1.EW_OK) {
  106. return ret;
  107. }
  108. int len = prg.Length;
  109. int j = len;
  110. while (len > 0)
  111. {
  112. j = len;
  113. ret = Focas1.cnc_download4(h, ref j, prg);
  114. if (ret == (short)Focas1.focas_ret.EW_BUFFER)
  115. {
  116. continue;
  117. }
  118. if (ret == Focas1.EW_OK)
  119. {
  120. len -= j;
  121. prg = prg.Substring(j, len);
  122. }
  123. if (ret != Focas1.EW_OK)
  124. {
  125. break;
  126. }
  127. }
  128. ret = Focas1.cnc_dwnend4(h);
  129. Fanuc.cnc_freelibhndl(h);
  130. }
  131. return ret;
  132. }
  133. //启动程序
  134. public int StartNcProg(string IP, string port)
  135. {
  136. // 复位x01
  137. string[] data = "1 1 0 0 0".Split(' ');
  138. byte[] buf = new byte[5];
  139. for (int i = 0; i < 5; i++)
  140. {
  141. buf[i] = Convert.ToByte(data[i], 16);
  142. }
  143. ushort h = 0;
  144. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  145. if (ret == Fanuc.EW_OK)
  146. {
  147. }
  148. return 0;
  149. }
  150. //获取程序列表
  151. public StringCollection GetAllProgrammFileName(string path, ushort FlibHndl)
  152. {
  153. short top_prg_2 = 1;
  154. StringCollection strNames = new StringCollection();
  155. List<Focas1.ODBPDFADIR> _dir_list = new List<Focas1.ODBPDFADIR>();
  156. Focas1.IDBPDFADIR prgin = new Focas1.IDBPDFADIR();
  157. Focas1.ODBPDFADIR prgout2 = new Focas1.ODBPDFADIR();
  158. prgin.path = path;
  159. prgin.type = 0;
  160. prgin.size_kind = 0;
  161. prgin.req_num = 0;
  162. short num_prg = 0;
  163. while (true)
  164. {
  165. prgout2 = new Focas1.ODBPDFADIR();
  166. prgin.req_num = num_prg;
  167. switch (Focas1.cnc_rdpdf_alldir(FlibHndl, ref top_prg_2, prgin, prgout2))
  168. {
  169. case 3:
  170. return strNames;
  171. default:
  172. return strNames;
  173. case 0:
  174. if (prgout2.data_kind.ToString() == "1")
  175. {
  176. try
  177. {
  178. strNames.Add(prgout2.d_f.ToString());
  179. }
  180. catch (Exception e)
  181. {
  182. }
  183. }
  184. num_prg = (short)(num_prg + 1);
  185. if (top_prg_2 <= 0)
  186. {
  187. return strNames;
  188. }
  189. break;
  190. }
  191. }
  192. }
  193. // 参照soflex读写监听PMC,marco宏变量,启动用
  194. public int ReadPmcMarco(string IP, string port, String proName)
  195. {
  196. ushort h = 0;
  197. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  198. if (ret == Fanuc.EW_OK)
  199. {
  200. // 选中程序
  201. string fullPath = @FanucLocalPath + proName;
  202. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullPath);
  203. ret = Focas1.cnc_pdf_slctmain(h, FanucRemotePath + "O" + fileNameWithoutExtension);
  204. // 执行程序启动,写PMC变量、宏变量通知机床启动程序
  205. string writeAddress = "R0372";
  206. string readAddress1 = "R0373";
  207. string readAddress2 = "R0374";
  208. string writeValue = "00000000";
  209. ret = WriterPmc(writeAddress, writeValue);
  210. if (ret == Fanuc.EW_OK)
  211. {
  212. while (true)
  213. {
  214. char[] chars = ReadPmc(readAddress1);
  215. if (chars != null && chars[7].ToString() == "1")
  216. {
  217. }
  218. else {
  219. continue;
  220. }
  221. }
  222. }
  223. Fanuc.cnc_freelibhndl(h);
  224. return ret;
  225. }
  226. return ret;
  227. }
  228. public int ReadMacroRange(string IP, string port, String address)
  229. {
  230. List<int> values = new List<int>();
  231. Focas1.IODBMR odbmr = new Focas1.IODBMR();
  232. ushort h = 0;
  233. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  234. if (ret == Fanuc.EW_OK)
  235. {
  236. short ncdataAddress = (short)Convert.ToUInt16(address);
  237. // 读取ncdata地址
  238. ret = Fanuc.cnc_rdmacror(h, ncdataAddress, ncdataAddress, 8 + (8 * 1), odbmr);
  239. if (ret == 0)
  240. {
  241. values.Add(GetMacroValue(odbmr.data.data1));
  242. }
  243. else
  244. {
  245. // 记录日志
  246. }
  247. // 写ncdataC变量
  248. odbmr.data.data1.mcr_val = 654900000;
  249. odbmr.data.data1.dec_val = 5;
  250. ret = Fanuc.cnc_wrmacror(h, 12 + (8 * 1), odbmr);
  251. }
  252. Fanuc.cnc_freelibhndl(h);
  253. return ret;
  254. }
  255. public char[] ReadPmc(string address)
  256. {
  257. char[] chars = null;
  258. try
  259. {
  260. string addressKindStr = address.Substring(0, 1);
  261. int addressKindInt = 0;
  262. int addressNo = int.Parse(address.Substring(1, 4).TrimStart('0') == "" ? "0" : address.Substring(1, 4).TrimStart('0'));
  263. for (int i = 0; i < pmcAddress.Keys.Count; i++)
  264. {
  265. if (pmcAddress[i].ToString() == addressKindStr)
  266. {
  267. addressKindInt = i;
  268. break;
  269. }
  270. }
  271. short a = Convert.ToInt16(addressKindInt);
  272. short b = 0;
  273. ushort start = Convert.ToUInt16(addressNo);
  274. ushort end = Convert.ToUInt16(addressNo + 1);
  275. ushort f = 0;
  276. ushort N = (ushort)(end - start + 1);
  277. switch (b)
  278. {
  279. case 0:
  280. f = (ushort)(8 + N);
  281. break;
  282. case 1:
  283. f = (ushort)(8 + N * 2);
  284. break;
  285. case 2:
  286. f = (ushort)(8 + N * 4);
  287. break;
  288. }
  289. Fanuc.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
  290. int ret = Fanuc.pmc_rdpmcrng(Fanuc.h, a, b, start, end, f, iodbpmc0);
  291. if (ret == Fanuc.EW_OK)
  292. {
  293. byte[] data = iodbpmc0.cdata;
  294. int j = 0;
  295. for (ushort i = start; i < end; i++, j++)
  296. {
  297. string add = getpmcadd(iodbpmc0.type_a, i);
  298. int value = Convert.ToInt32(Convert.ToString(data[j], 16).ToString());
  299. string temp = Convert.ToString(value, 2).PadLeft(8, '0');
  300. chars = temp.ToCharArray();
  301. /* textBox9.Text = chars[7].ToString();
  302. textBox8.Text = chars[6].ToString();
  303. textBox7.Text = chars[5].ToString();
  304. textBox6.Text = chars[4].ToString();
  305. textBox5.Text = chars[3].ToString();
  306. textBox4.Text = chars[2].ToString();
  307. textBox3.Text = chars[1].ToString();
  308. textBox2.Text = chars[0].ToString();*/
  309. }
  310. }
  311. }
  312. catch (Exception e)
  313. {
  314. e.ToString();
  315. return chars;
  316. }
  317. return chars;
  318. }
  319. //写pmc地址
  320. public int WriterPmc(string address, string oneData)
  321. {
  322. //提示数据长度错误,但是对象中没有相应的属性可以设置
  323. //Specify the type of the PMC data.
  324. string addressKindStr = address.Substring(0, 1);
  325. int addressKindInt = 0;
  326. int addressNo = int.Parse(address.Substring(1, 4).TrimStart('0') == "" ? "0" : address.Substring(1, 4).TrimStart('0'));
  327. for (int i = 0; i < pmcAddress.Keys.Count; i++)
  328. {
  329. if (pmcAddress[i].ToString() == addressKindStr)
  330. {
  331. addressKindInt = i;
  332. break;
  333. }
  334. }
  335. ushort a = 8 + 2 * 1;
  336. Fanuc.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
  337. iodbpmc0.datano_s = Convert.ToInt16(addressNo);
  338. iodbpmc0.datano_e = Convert.ToInt16(addressNo + 1);
  339. iodbpmc0.type_a = Convert.ToInt16(addressKindInt);
  340. iodbpmc0.type_d = 0;
  341. string[] data = { oneData };
  342. byte[] buf = new byte[5];
  343. for (int i = 0; i < 1; i++)
  344. {
  345. buf[i] = Convert.ToByte(data[i], 16);
  346. }
  347. iodbpmc0.cdata = buf;
  348. int ret = Fanuc.pmc_wrpmcrng(Fanuc.h, a, iodbpmc0);
  349. return ret;
  350. }
  351. public int GetMacroValue(Focas1.IODBMR_data data)
  352. {
  353. if (data.mcr_val == 0 && data.dec_val == -1)
  354. {
  355. return 0;
  356. }
  357. else
  358. {
  359. return (int)(data.mcr_val / Math.Pow(10.0, data.dec_val));
  360. }
  361. }
  362. // 读取工件坐标系
  363. public int ReadWriteZofs(string IP, string port)
  364. {
  365. ushort h = 0;
  366. Focas1.IODBZOR zofsX = new Focas1.IODBZOR();
  367. Focas1.IODBZOR zofsZ = new Focas1.IODBZOR();
  368. Focas1.IODBZOR zofsAbsX = new Focas1.IODBZOR();
  369. Focas1.IODBZOR zofsAbsZ = new Focas1.IODBZOR();
  370. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  371. if (ret == Fanuc.EW_OK)
  372. {
  373. // 读取工件坐标系
  374. ret = Focas1.cnc_rdzofsr(h, 1, 1, 1, 8 + 4, zofsX);
  375. if (ret == Focas1.EW_OK)
  376. {
  377. }
  378. ret = Focas1.cnc_rdzofsr(h, 1, 2, 1, 8 + 4, zofsZ);
  379. if (ret == Focas1.EW_OK)
  380. {
  381. }
  382. ret = Focas1.cnc_rdzofsr(h, 0, 1, 0, 8 + 4, zofsAbsX);
  383. if (ret == Focas1.EW_OK)
  384. {
  385. }
  386. ret = Focas1.cnc_rdzofsr(h, 0, 2, 0, 8 + 4, zofsAbsZ);
  387. if (ret == Focas1.EW_OK)
  388. {
  389. }
  390. // 写工件坐标系
  391. /*
  392. zofsX.data[0] = 666666;
  393. ret = Focas1.cnc_wrzofsr(h, 8 + 4, zofsX);
  394. zofsZ.data[0] = 666666;
  395. ret = Focas1.cnc_wrzofsr(h, 8 + 4, zofsZ);
  396. zofsAbsX.data[0] = 666;
  397. ret = Focas1.cnc_wrzofsr(h, 8 + 4, zofsAbsX);
  398. zofsAbsZ.data[0] = 666;
  399. ret = Focas1.cnc_wrzofsr(h, 8 + 4, zofsAbsZ);
  400. return (ret);*/
  401. }
  402. return ret;
  403. }
  404. public string MainProg(string IP, string port)
  405. {
  406. ushort h = 0;
  407. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  408. if (ret == Fanuc.EW_OK)
  409. {
  410. Focas1.ODBPRO pnum = new Focas1.ODBPRO();
  411. ret = Focas1.cnc_rdprgnum(h, pnum);
  412. if (ret == Fanuc.EW_OK)
  413. {
  414. Fanuc.cnc_freelibhndl(h);
  415. return pnum.data.ToString().PadLeft(4, '0');
  416. }
  417. else
  418. {
  419. return "error";
  420. }
  421. }
  422. else
  423. { return "error2"; }
  424. }
  425. public string ActSpindle(string IP, string port)
  426. {
  427. ushort h = 0;
  428. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  429. if (ret == Fanuc.EW_OK)
  430. {
  431. Focas1.ODBACT pindle = new Focas1.ODBACT();
  432. ret = Focas1.cnc_acts(h, pindle);
  433. if (ret == Fanuc.EW_OK)
  434. {
  435. Fanuc.cnc_freelibhndl(h);
  436. return pindle.data.ToString();
  437. }
  438. else
  439. {
  440. return "error";
  441. }
  442. }
  443. else
  444. { return "error"; }
  445. }
  446. public string Status(string IP, string port)
  447. {
  448. ushort h = 0;
  449. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  450. if (ret == Fanuc.EW_OK)
  451. {
  452. Focas1.ODBST cncStatus = new Focas1.ODBST();
  453. string[] str = new string[3];
  454. ret = Focas1.cnc_statinfo(h, cncStatus);
  455. if (ret == Fanuc.EW_OK)
  456. {
  457. Fanuc.cnc_freelibhndl(h);
  458. return cncStatus.run.ToString();
  459. }
  460. else
  461. {
  462. return "error";
  463. }
  464. }
  465. else
  466. { return "error"; }
  467. }
  468. public string Mode(string IP, string port)
  469. {
  470. ushort h = 0;
  471. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  472. if (ret == Fanuc.EW_OK)
  473. {
  474. Focas1.ODBST cncStatus = new Focas1.ODBST();
  475. string[] str = new string[3];
  476. ret = Focas1.cnc_statinfo(h, cncStatus);
  477. if (ret == Fanuc.EW_OK)
  478. {
  479. Fanuc.cnc_freelibhndl(h);
  480. return cncStatus.aut.ToString();
  481. }
  482. else
  483. {
  484. return "error";
  485. }
  486. }
  487. else
  488. { return "error"; }
  489. }
  490. public string EMG(string IP, string port)
  491. {
  492. ushort h = 0;
  493. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  494. if (ret == Fanuc.EW_OK)
  495. {
  496. Focas1.IODBPMC0 iodbpmc0 = new Focas1.IODBPMC0();
  497. ret = Focas1.pmc_rdpmcrng(h, 0, 0, 7, 12, 14, iodbpmc0);
  498. if (ret == Fanuc.EW_OK)
  499. {
  500. Fanuc.cnc_freelibhndl(h);
  501. byte[] data = iodbpmc0.cdata;
  502. string value = Convert.ToString(data[1], 16).ToString().PadLeft(2, '0').ToUpper();
  503. string str = Convert.ToString(Convert.ToInt32(value.ToString().Substring(0, 1)), 2).PadLeft(4, '0').Substring(3, 1);
  504. if (str == "1")
  505. return "1";
  506. else
  507. return "0";
  508. }
  509. else
  510. {
  511. return "error";
  512. }
  513. }
  514. else
  515. { return "error"; }
  516. }
  517. public string ActFeed(string IP, string port)
  518. {
  519. ushort h = 0;
  520. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  521. if (ret == Fanuc.EW_OK)
  522. {
  523. Focas1.IODBPMC0 iodbpmco = new Focas1.IODBPMC0();
  524. ret = Focas1.pmc_rdpmcrng(h, 0, 1, 12, 13, 10, iodbpmco);
  525. if (ret == Fanuc.EW_OK)
  526. {
  527. Fanuc.cnc_freelibhndl(h);
  528. return (100 - (iodbpmco.cdata[0] - 155)).ToString();
  529. }
  530. else
  531. {
  532. return "error";
  533. }
  534. }
  535. else
  536. { return "error"; }
  537. }
  538. public string ServoLoadX(string IP, string port)
  539. {
  540. ushort h = 0;
  541. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  542. if (ret == Fanuc.EW_OK)
  543. {
  544. Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
  545. short a = 6;//伺服轴的数量
  546. ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
  547. if (ret == Fanuc.EW_OK)
  548. {
  549. Fanuc.cnc_freelibhndl(h);
  550. return sv.svload1.data.ToString();
  551. }
  552. else
  553. {
  554. return "error";
  555. }
  556. }
  557. else
  558. { return "error"; }
  559. }
  560. public string ServoLoadY(string IP, string port)
  561. {
  562. ushort h = 0;
  563. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  564. if (ret == Fanuc.EW_OK)
  565. {
  566. Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
  567. short a = 6;//伺服轴的数量
  568. ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
  569. if (ret == Fanuc.EW_OK)
  570. {
  571. Fanuc.cnc_freelibhndl(h);
  572. return sv.svload2.data.ToString();
  573. }
  574. else
  575. {
  576. return "error";
  577. }
  578. }
  579. else
  580. { return "error"; }
  581. }
  582. public string ServoLoadZ(string IP, string port)
  583. {
  584. ushort h = 0;
  585. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  586. if (ret == Fanuc.EW_OK)
  587. {
  588. Focas1.ODBSVLOAD sv = new Focas1.ODBSVLOAD();
  589. short a = 6;//伺服轴的数量
  590. ret = Focas1.cnc_rdsvmeter(h, ref a, sv);
  591. if (ret == Fanuc.EW_OK)
  592. {
  593. Fanuc.cnc_freelibhndl(h);
  594. return sv.svload3.data.ToString();
  595. }
  596. else
  597. {
  598. return "error";
  599. }
  600. }
  601. else
  602. { return "error"; }
  603. }
  604. public string PowerOnTime(string IP, string port)
  605. {
  606. ushort h = 0;
  607. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  608. if (ret == Fanuc.EW_OK)
  609. {
  610. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  611. ret = Focas1.cnc_rdparam(h, 6750, 0, 8, iodbpsd);
  612. if (ret == Fanuc.EW_OK)
  613. {
  614. Fanuc.cnc_freelibhndl(h);
  615. return iodbpsd.ldata.ToString();
  616. }
  617. else
  618. {
  619. return "error";
  620. }
  621. }
  622. else
  623. { return "error"; }
  624. }
  625. public string AccumulateCuttingTime(string IP, string port)
  626. {
  627. ushort h = 0;
  628. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  629. if (ret == Fanuc.EW_OK)
  630. {
  631. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  632. ret = Focas1.cnc_rdparam(h, 6754, 0, 8, iodbpsd);
  633. if (ret == Fanuc.EW_OK)
  634. {
  635. Fanuc.cnc_freelibhndl(h);
  636. return iodbpsd.ldata.ToString();
  637. }
  638. else
  639. {
  640. return "error";
  641. }
  642. }
  643. else
  644. { return "error"; }
  645. }
  646. public string CuttingTimePerCycle(string IP, string port)
  647. {
  648. ushort h = 0;
  649. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  650. if (ret == Fanuc.EW_OK)
  651. {
  652. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  653. int min = 0;
  654. int sec = 0;
  655. ret = Focas1.cnc_rdparam(h, 6757, 0, 8, iodbpsd);//秒
  656. if (ret == Focas1.EW_OK)
  657. {
  658. sec = iodbpsd.ldata;
  659. ret = Focas1.cnc_rdparam(h, 6758, 0, 8, iodbpsd);//分
  660. if (ret == Focas1.EW_OK)
  661. {
  662. min = iodbpsd.ldata;
  663. Fanuc.cnc_freelibhndl(h);
  664. return (min * 60 * 1000 + sec).ToString();
  665. }
  666. else
  667. { return "error"; }
  668. }
  669. else { return "error"; }
  670. }
  671. else
  672. { return "error"; }
  673. }
  674. public string WorkTime(string IP, string port)
  675. {
  676. ushort h = 0;
  677. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  678. if (ret == Fanuc.EW_OK)
  679. {
  680. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  681. ret = Focas1.cnc_rdparam(h, 6752, -1, 8, iodbpsd);
  682. if (ret == Fanuc.EW_OK)
  683. {
  684. Fanuc.cnc_freelibhndl(h);
  685. return iodbpsd.ldata.ToString();
  686. }
  687. else
  688. {
  689. return "error";
  690. }
  691. }
  692. else
  693. { return "error"; }
  694. }
  695. public string Part_Count(string IP, string port)
  696. {
  697. ushort h = 0;
  698. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  699. if (ret == Fanuc.EW_OK)
  700. {
  701. Focas1.IODBPSD_1 iodbpsd = new Focas1.IODBPSD_1();
  702. ret = Focas1.cnc_rdparam(h, 6712, 0, 8, iodbpsd);
  703. if (ret == Fanuc.EW_OK)
  704. {
  705. Fanuc.cnc_freelibhndl(h);
  706. return iodbpsd.ldata.ToString();
  707. }
  708. else
  709. {
  710. return "error";
  711. }
  712. }
  713. else
  714. { return "error"; }
  715. }
  716. public string ToolNo(string IP, string port)
  717. {
  718. ushort h = 0;
  719. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  720. if (ret == Fanuc.EW_OK)
  721. {
  722. Focas1.ODBM odb = new Focas1.ODBM();
  723. ret = Focas1.cnc_rdmacro(h, 523, 10, odb);
  724. if (ret == Fanuc.EW_OK)
  725. {
  726. Fanuc.cnc_freelibhndl(h);
  727. return (odb.mcr_val * Math.Pow(10, -odb.dec_val)).ToString();
  728. }
  729. else
  730. {
  731. return "error";
  732. }
  733. }
  734. else
  735. { return "error"; }
  736. }
  737. public List<Tool> ToolLife(string IP, string port)
  738. {
  739. ushort h = 0;
  740. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  741. List<Tool> tools = new List<Tool>();
  742. Tool toolEntity = new Tool();
  743. if (ret == Fanuc.EW_OK)
  744. {
  745. Fanuc.ODBTLIFE5 tool = new Focas1.ODBTLIFE5();//读取刀片组的序号
  746. Fanuc.ODBTLIFE2 tool1 = new Focas1.ODBTLIFE2();//读取刀片组的全部数量
  747. Fanuc.ODBTLIFE3 tool2 = new Focas1.ODBTLIFE3();//刀具的数量
  748. Fanuc.ODBTG btg = new Focas1.ODBTG();
  749. Fanuc.ODBUSEGRP grp = new Focas1.ODBUSEGRP();
  750. int m = 1;
  751. Fanuc.cnc_rdgrpid2(h, m, tool);
  752. int group_numer = tool.data;
  753. int all = Fanuc.cnc_rdngrp(Fanuc.h, tool1);//刀片组的全部数量
  754. short b = Convert.ToInt16(group_numer);
  755. Fanuc.cnc_rdntool(h, b, tool2);//刀具的数量
  756. toolEntity.number = tool2.data.ToString();
  757. Fanuc.cnc_rdlife(h, b, tool2);//刀具寿命
  758. Fanuc.cnc_rdcount(h, b, tool2);//刀具计时器
  759. Fanuc.cnc_rdtoolgrp(h, 2, 20 + 20 * 1, btg);//根据刀组号读出所有信息,很重要;
  760. Fanuc.cnc_rdtlusegrp(h, grp);//读出正在使用的到组号;
  761. if (ret == Fanuc.EW_OK)
  762. {
  763. Fanuc.cnc_freelibhndl(h);
  764. toolEntity.targetLife = tool2.data.ToString();
  765. tools.Add(toolEntity);
  766. return tools;
  767. }
  768. else
  769. {
  770. return tools;
  771. }
  772. }
  773. else
  774. { return tools; }
  775. }
  776. public string getpmcadd(short a, ushort b)
  777. {
  778. string tempa = "";
  779. switch (a)
  780. {
  781. case 0:
  782. tempa = "G";
  783. break;
  784. case 1:
  785. tempa = "F";
  786. break;
  787. case 2:
  788. tempa = "Y";
  789. break;
  790. case 3:
  791. tempa = "X";
  792. break;
  793. case 4:
  794. tempa = "A";
  795. break;
  796. case 5:
  797. tempa = "R";
  798. break;
  799. case 6:
  800. tempa = "T";
  801. break;
  802. case 7:
  803. tempa = "K";
  804. break;
  805. case 8:
  806. tempa = "C";
  807. break;
  808. case 9:
  809. tempa = "D";
  810. break;
  811. default:
  812. tempa = "n";
  813. break;
  814. }
  815. string tempb = b.ToString().PadLeft(4, '0');
  816. return tempa + tempb;
  817. }
  818. public List<AlmInfo> AlmMsg(string IP, string port, out string IsAlarm)
  819. {
  820. List<AlmInfo> stateinfo = new List<AlmInfo>();
  821. IsAlarm = "0";
  822. ushort h = 0;
  823. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  824. if (ret == Fanuc.EW_OK)
  825. {
  826. short b = -1;//哪一类报警,-1所有报警
  827. short num = 11;//设大一些没关系
  828. Focas1.ODBALMMSG odbalmmsg2 = new Focas1.ODBALMMSG();
  829. ret = Focas1.cnc_rdalmmsg(h, b, ref num, odbalmmsg2);
  830. System.Type type = odbalmmsg2.GetType();
  831. if (ret == 0)
  832. {
  833. if (num > 0)
  834. {
  835. IsAlarm = "1";
  836. for (int i = 0; i <= num - 1; i++)
  837. {
  838. string str1 = "msg";
  839. str1 = str1 + (i + 1);
  840. object obj = type.GetField(str1).GetValue(odbalmmsg2);
  841. //if(obj.)
  842. System.Type type1 = obj.GetType();
  843. if (type1.GetField("alm_msg").GetValue(obj).ToString() != "")
  844. {
  845. AlmInfo s = new AlmInfo();
  846. s.no = Convert.ToInt32(type1.GetField("alm_no").GetValue(obj).ToString()).ToString();
  847. s.msg = type1.GetField("alm_msg").GetValue(obj).ToString();
  848. stateinfo.Add(s);
  849. }
  850. }
  851. Fanuc.cnc_freelibhndl(h);
  852. return stateinfo;
  853. }
  854. else
  855. {
  856. IsAlarm = "0";
  857. AlmInfo s2 = new AlmInfo();
  858. s2.no = "0";
  859. s2.msg = "无报警";
  860. stateinfo.Add(s2);
  861. Fanuc.cnc_freelibhndl(h);
  862. return stateinfo;
  863. }
  864. }
  865. else
  866. {
  867. IsAlarm = "0";
  868. AlmInfo s3 = new AlmInfo();
  869. s3.no = "0";
  870. s3.msg = "无报警";
  871. stateinfo.Add(s3);
  872. return stateinfo;
  873. }
  874. }
  875. else
  876. {
  877. return stateinfo;
  878. }
  879. }
  880. public List<NcInfo> NcList(string IP, string port)
  881. {
  882. Focas1.PRGDIR2 prgdir2 = new Focas1.PRGDIR2();
  883. short Prg_type = 2;
  884. short num_prog = 10;
  885. short top_prog = 0;
  886. List<NcInfo> ncinfo = new List<NcInfo>();
  887. ushort h = 0;
  888. int ret = Fanuc.cnc_allclibhndl3(IP, Convert.ToUInt16(port), 1, out h);
  889. if (ret == Fanuc.EW_OK)
  890. {
  891. do
  892. {
  893. ret = Focas1.cnc_rdprogdir2(h, Prg_type, ref top_prog, ref num_prog, prgdir2);
  894. if (ret == Focas1.EW_OK)
  895. {
  896. string sprgdir2 = "dir";
  897. string sprgdir2_data = "number";
  898. string sprgdir2_data2 = "length";
  899. string sprgdir2_data3 = "comment";
  900. int Endnum = 0;
  901. if (ret == Focas1.EW_OK)
  902. {
  903. System.Type type = prgdir2.GetType();
  904. for (int j = 1; j < num_prog + 1; j++)
  905. {
  906. sprgdir2 = "dir" + j;
  907. FieldInfo fieidinfo = type.GetField(sprgdir2);
  908. object ob = fieidinfo.GetValue(prgdir2);
  909. System.Type typeob = ob.GetType();
  910. string ncNumber = "O" + typeob.GetField(sprgdir2_data).GetValue(ob).ToString().PadLeft(4, '0');
  911. double temp = Convert.ToDouble(typeob.GetField(sprgdir2_data2).GetValue(ob).ToString());
  912. temp = temp / 1024.0 + 0.5;
  913. if (temp < 1)
  914. temp += 0.5;
  915. string ncLength = ((int)temp).ToString();
  916. Endnum = Convert.ToInt32(ncNumber.Remove(0, 1));
  917. string ncComment = typeob.GetField(sprgdir2_data3).GetValue(ob).ToString();
  918. NcInfo nc = new NcInfo { no = ncNumber, lentgh = ncLength, comment = ncComment };
  919. ncinfo.Add(nc);
  920. }
  921. top_prog = (short)(Endnum + 1);
  922. }
  923. }
  924. } while (num_prog != 0);
  925. Fanuc.cnc_freelibhndl(h);
  926. }
  927. return ncinfo;
  928. }
  929. public class AlmInfo
  930. {
  931. public string no { get; set; }
  932. public string msg { get; set; }
  933. }
  934. public class NcInfo
  935. {
  936. public string no { get; set; }
  937. public string lentgh { get; set; }
  938. public string comment { get; set; }
  939. }
  940. public class Tool
  941. {
  942. /// <summary>
  943. /// 刀具号
  944. /// </summary>
  945. public string number { get; set; }
  946. /// <summary>
  947. /// 刀具名称
  948. /// </summary>
  949. public string name { get; set; }
  950. /// <summary>
  951. /// 刀具寿命预警
  952. /// </summary>
  953. public string warnLife { get; set; }
  954. /// <summary>
  955. /// 刀具寿命目标值
  956. /// </summary>
  957. public string targetLife { get; set; }
  958. /// <summary>
  959. /// 工件数
  960. /// </summary>
  961. public string workPiece { get; set; }
  962. /// <summary>
  963. /// 工件数预警值
  964. /// </summary>
  965. public string workPieceWarn { get; set; }
  966. /// <summary>
  967. /// 工件数目标值
  968. /// </summary>
  969. public string workPieceTarget { get; set; }
  970. /// <summary>
  971. /// 磨损量
  972. /// </summary>
  973. public string wear { get; set; }
  974. /// <summary>
  975. /// 磨损量预警值
  976. /// </summary>
  977. public string wearWarn { get; set; }
  978. /// <summary>
  979. /// 磨损量目标值
  980. /// </summary>
  981. public string wearTarget { get; set; }
  982. /// <summary>
  983. /// 姊妹刀号
  984. /// </summary>
  985. public string sisterNo { get; set; }
  986. /// <summary>
  987. /// 刀沿号
  988. /// </summary>
  989. public string toolEdgeNo { get; set; }
  990. /// <summary>
  991. /// 刀具位置
  992. /// </summary>
  993. public string toolPosition { get; set; }
  994. /// <summary>
  995. /// 刀具半径
  996. /// </summary>
  997. public string toolRadius { get; set; }
  998. }
  999. }
  1000. }