ConDevice.cs 37 KB

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