ETH_S7SimensLib.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. namespace SimensCNC
  10. {
  11. public class ETH_S7SimensLib
  12. {
  13. public static string cncIP;
  14. int receiveData;
  15. byte[] sendCommands;
  16. byte[] buffer;//发送报文容器
  17. byte[] datas;//接收报文容器
  18. static IAsyncResult connResult = null;
  19. Func<byte[], int, object> func;
  20. public ETH_S7SimensLib()
  21. {
  22. }
  23. public Socket socket { get; set; }
  24. /// <summary>
  25. /// 链接函数
  26. /// </summary>
  27. public bool ETH_S7SimensConnect(string adrip, ushort port, int overtime)
  28. {
  29. try
  30. {
  31. cncIP = adrip;
  32. IPAddress ip = IPAddress.Parse(adrip);
  33. int ports = Convert.ToInt32(port);
  34. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  35. connResult = socket.BeginConnect(ip, ports, null, null);
  36. socket.ReceiveTimeout = 2000;
  37. Thread.Sleep(5000);
  38. connResult.AsyncWaitHandle.WaitOne(5, true); //等待2秒
  39. if (connResult.IsCompleted)
  40. {
  41. HandShark();
  42. return true;
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. }
  49. catch
  50. {
  51. return false; ;
  52. }
  53. finally
  54. {
  55. }
  56. }
  57. /// <summary>
  58. /// 断开设备连接
  59. /// </summary>
  60. /// <param name="no">设备编号</param>
  61. public void ETH_S7SimensDisConnect(string no)
  62. {
  63. try
  64. {
  65. socket.Shutdown(SocketShutdown.Both);
  66. socket.Close();
  67. }
  68. catch
  69. {
  70. }
  71. finally
  72. {
  73. }
  74. }
  75. /// <summary>
  76. /// 三次握手
  77. /// </summary>
  78. void HandShark()
  79. {
  80. socket.Send(ETH_S7SimensCommands.FIRST_HAND_SHANK);
  81. buffer = new byte[1024];
  82. receiveData = socket.Receive(buffer);
  83. socket.Send(ETH_S7SimensCommands.SENCOND_HAND_SHARK);
  84. buffer = new byte[1024];
  85. receiveData = socket.Receive(buffer);
  86. socket.Send(ETH_S7SimensCommands.THREE_HAND_SHARK);
  87. buffer = new byte[1024];
  88. receiveData = socket.Receive(buffer);
  89. }
  90. /// <summary>
  91. /// 获取CNCIP
  92. /// </summary>
  93. /// <param name="no"></param>
  94. /// <returns></returns>
  95. public bool ETH_S7SimensGetCNCIP(string no,ref object result)
  96. {
  97. if (cncIP != null)
  98. {
  99. result = cncIP;
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. };
  106. }
  107. /// <summary>
  108. /// 获取采集时间
  109. /// </summary>
  110. /// <returns></returns>
  111. public bool ETH_S7SimensCollectTime(ref object result)
  112. {
  113. if (socket.Connected == true)
  114. {
  115. result = DateTime.Now; ;
  116. return true;
  117. }
  118. else
  119. {
  120. return false;
  121. }
  122. }
  123. /// <summary>
  124. /// 获取心跳包
  125. /// </summary>
  126. /// <param name="no"></param>
  127. /// <returns></returns>
  128. public bool ETH_S7SimensGetKeepAlive(string no,ref object result)
  129. {
  130. if (connResult.IsCompleted == true)
  131. {
  132. result = true;
  133. return true;
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. /// <summary>
  141. /// 获取版本信息
  142. /// </summary>
  143. /// <param name="no"></param>
  144. public bool ETH_S7SimensGetVerVison(ref object result)
  145. {
  146. try
  147. {
  148. func = new Func<byte[], int, string>(AnalysisStrData);
  149. return GetData(ETH_S7SimensCommands.READ_VER_INFO, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
  150. }
  151. catch
  152. {
  153. return false;
  154. }
  155. finally
  156. {
  157. }
  158. }
  159. /// <summary>
  160. /// 定义机床指纹
  161. /// </summary>
  162. public bool ETH_S7SimensGetCNCID(ref object result)
  163. {
  164. try
  165. {
  166. func = new Func<byte[], int, string>(AnalysisStrData);
  167. return GetData(ETH_S7SimensCommands.CNC_ID, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
  168. }
  169. catch
  170. {
  171. return false;
  172. }
  173. finally
  174. {
  175. }
  176. }
  177. /// <summary>
  178. /// 获取机床类型
  179. /// </summary>
  180. public bool ETH_S7SimensCNCType(ref object result)
  181. {
  182. try
  183. {
  184. func = new Func<byte[], int, string>(AnalysisStrData);
  185. return GetData(ETH_S7SimensCommands.CNC_TYPE, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
  186. }
  187. catch
  188. {
  189. return true;
  190. }
  191. finally
  192. {
  193. }
  194. }
  195. /// <summary>
  196. /// 操作模式
  197. /// </summary>
  198. /// <param name="no"></param>
  199. /// <returns></returns>
  200. public bool ETH_S7SimensGetModeInfo(ref object result)
  201. {
  202. try
  203. {
  204. func = new Func<byte[], int, object>(AnalusisModeData);
  205. return GetData(ETH_S7SimensCommands.CNC_MODE, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
  206. }
  207. catch
  208. {
  209. return false;
  210. }
  211. finally
  212. {
  213. }
  214. }
  215. /// <summary>
  216. /// 运行状态
  217. /// </summary>
  218. /// <param name="no"></param>
  219. /// <returns></returns>
  220. public bool ETH_S7SimensGetStateInfo(ref object result)
  221. {
  222. try
  223. {
  224. func = new Func<byte[], int, object>(AnalusisStatusData);
  225. return GetData(ETH_S7SimensCommands.CNC_STATUS, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
  226. }
  227. catch
  228. {
  229. return false;
  230. }
  231. finally
  232. {
  233. }
  234. }
  235. /// <summary>
  236. /// 获取加工件数
  237. /// </summary>
  238. /// <param name="no"></param>
  239. /// <returns></returns>
  240. public bool ETH_S7SimensGetProductCounts(ref object result)
  241. {
  242. try
  243. {
  244. func = new Func<byte[], int, object>(AnalysisDoubleData);
  245. return GetData(ETH_S7SimensCommands.CNC_PRODUCTS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  246. }
  247. catch
  248. {
  249. return false;
  250. }
  251. finally
  252. {
  253. }
  254. }
  255. /// <summary>
  256. /// 进给设定速度
  257. /// </summary>
  258. /// <param name="no"></param>
  259. /// <returns></returns>
  260. public bool ETH_S7SimensGetSetFeedSpeed(ref object result)
  261. {
  262. try
  263. {
  264. func = new Func<byte[], int, object>(AnalysisDoubleData);
  265. return GetData(ETH_S7SimensCommands.CNC_FEEDSETSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  266. }
  267. catch
  268. {
  269. return false;
  270. }
  271. finally
  272. {
  273. }
  274. }
  275. /// <summary>
  276. /// 进给实际速度
  277. /// </summary>
  278. /// <param name="no"></param>
  279. /// <returns></returns>
  280. public bool ETH_S7SimensGetActFeedSpeed(ref object result)
  281. {
  282. try
  283. {
  284. func = new Func<byte[], int, object>(AnalysisDoubleData);
  285. return GetData(ETH_S7SimensCommands.CNC_FEEDACTSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  286. }
  287. catch
  288. {
  289. return false;
  290. }
  291. finally
  292. {
  293. }
  294. }
  295. /// <summary>
  296. /// 主轴设定速度
  297. /// </summary>
  298. /// <param name="no"></param>
  299. /// <returns></returns>
  300. public bool ETH_S7SimensGetSetSpSpeed(ref object result)
  301. {
  302. try
  303. {
  304. func = new Func<byte[], int, object>(AnalysisDoubleData);
  305. return GetData(ETH_S7SimensCommands.CNC_SPINDLESETSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  306. }
  307. catch
  308. {
  309. return false;
  310. }
  311. finally
  312. {
  313. }
  314. }
  315. /// <summary>
  316. /// 主轴实际速度
  317. /// </summary>
  318. /// <param name="no"></param>
  319. /// <returns></returns>
  320. public bool ETH_S7SimensGetActSpSpeed(ref object result)
  321. {
  322. try
  323. {
  324. func = new Func<byte[], int, object>(AnalysisDoubleData);
  325. return GetData(ETH_S7SimensCommands.CNC_SPINDLEACTSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  326. }
  327. catch
  328. {
  329. return false;
  330. }
  331. finally
  332. {
  333. }
  334. }
  335. /// <summary>
  336. /// 主轴倍率
  337. /// </summary>
  338. /// <param name="no"></param>
  339. /// <returns></returns>
  340. public bool ETH_S7SimensGetSFeed(ref object result)
  341. {
  342. try
  343. {
  344. func = new Func<byte[], int, object>(AnalysisDoubleData);
  345. return GetData(ETH_S7SimensCommands.CNC_SPINDRATE, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  346. }
  347. catch
  348. {
  349. return false;
  350. }
  351. finally
  352. {
  353. }
  354. }
  355. /// <summary>
  356. /// 进给倍率
  357. /// </summary>
  358. /// <param name="no"></param>
  359. /// <returns></returns>
  360. public bool ETH_S7SimenstGetFeedRate(ref object result)
  361. {
  362. try
  363. {
  364. func = new Func<byte[], int, object>(AnalysisDoubleData);
  365. return GetData(ETH_S7SimensCommands.CNC_FEEDRATE, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  366. }
  367. catch
  368. {
  369. return false;
  370. }
  371. finally
  372. {
  373. }
  374. }
  375. /// <summary>
  376. ///循环时间
  377. /// </summary>
  378. /// <param name="socket"></param>
  379. public bool ETH_S7SimenstGetCycTime(ref object result)
  380. {
  381. try
  382. {
  383. func = new Func<byte[], int, object>(AnalysisDoubleData);
  384. return GetData(ETH_S7SimensCommands.RUN_TIME, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  385. }
  386. catch
  387. {
  388. return false;
  389. }
  390. finally
  391. {
  392. }
  393. }
  394. /// <summary>
  395. ///剩余时间
  396. /// </summary>
  397. /// <param name="socket"></param>
  398. public bool ETH_S7SimenstGetRemTime(ref object result)
  399. {
  400. try
  401. {
  402. func = new Func<byte[], int, object>(AnalysisDoubleData);
  403. return GetData(ETH_S7SimensCommands.REMAIN_TIME, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  404. }
  405. catch
  406. {
  407. return false;
  408. }
  409. finally
  410. {
  411. }
  412. }
  413. /// <summary>
  414. /// 获取加工程序名称
  415. /// </summary>
  416. /// <param name="no"></param>
  417. /// <returns></returns>
  418. public bool ETH_S7SimensGetPrgName(ref object result)
  419. {
  420. try
  421. {
  422. func = new Func<byte[], int, string>(AnalysisStrData);
  423. return GetData(ETH_S7SimensCommands.PROGRAM_NAME, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
  424. }
  425. catch
  426. {
  427. return false;
  428. }
  429. finally
  430. {
  431. }
  432. }
  433. /// <summary>
  434. /// 加工程序内容
  435. /// </summary>
  436. /// <param name="no"></param>
  437. /// <returns></returns>
  438. public bool ETH_S7SimensGetPrgContent(ref object result)
  439. {
  440. try
  441. {
  442. func = new Func<byte[], int, string>(AnalysisStrData);
  443. return GetData(ETH_S7SimensCommands.CurrentPro, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
  444. }
  445. catch
  446. {
  447. return true;
  448. }
  449. finally
  450. {
  451. }
  452. }
  453. /// <summary>
  454. /// 获取轴名称
  455. /// 同时是获取机床参数的方法
  456. /// </summary>
  457. /// <param name="no"></param>
  458. /// <returns></returns>
  459. public bool ETH_S7SimensGetAxisName(ref object result)
  460. {
  461. try
  462. {
  463. func = new Func<byte[], int, string>(AnalysisStrData);
  464. return GetData(ETH_S7SimensCommands.AXIS_NAME, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
  465. }
  466. catch
  467. {
  468. return true;
  469. }
  470. finally
  471. {
  472. }
  473. }
  474. /// <summary>
  475. /// 获取机械坐标
  476. /// </summary>
  477. /// <param name="no"></param>
  478. /// <returns></returns>
  479. public bool ETH_S7SimensGetMacPos(ref object result)
  480. {
  481. try
  482. {
  483. List<object> pos = new List<object>();
  484. foreach (var s in ETH_S7SimensCommands.posflag)
  485. {
  486. func = new Func<byte[], int, object>(AnalysisDoubleData);
  487. ETH_S7SimensCommands.MACHINE_POS[26] = s;
  488. GetData(ETH_S7SimensCommands.MACHINE_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  489. pos.Add(result);
  490. }
  491. if (pos != null)
  492. {
  493. result = string.Join("/", pos);
  494. return true;
  495. }
  496. else
  497. {
  498. return false;
  499. }
  500. }
  501. catch
  502. {
  503. return false;
  504. }
  505. finally
  506. {
  507. }
  508. }
  509. /// <summary>
  510. /// 获取相对坐标,工件坐标
  511. /// </summary>
  512. /// <param name="no"></param>
  513. /// <returns></returns>
  514. public bool ETH_S7SimensGetRelPos(ref object result)
  515. {
  516. try
  517. {
  518. List<object> pos = new List<object>();
  519. foreach (var s in ETH_S7SimensCommands.posflag)
  520. {
  521. func = new Func<byte[], int, object>(AnalysisDoubleData);
  522. ETH_S7SimensCommands.RELATIVELY_POS[26] = s;
  523. GetData(ETH_S7SimensCommands.RELATIVELY_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  524. pos.Add(result);
  525. }
  526. if (pos != null)
  527. {
  528. result = string.Join("/", pos);
  529. return true;
  530. }
  531. else
  532. {
  533. return false;
  534. }
  535. }
  536. catch
  537. {
  538. return false;
  539. }
  540. finally
  541. {
  542. }
  543. }
  544. /// <summary>
  545. /// 获取剩余坐标
  546. /// </summary>
  547. /// <param name="no"></param>
  548. /// <returns></returns>
  549. public bool ETH_S7SimensGetRemPos(ref object result)
  550. {
  551. try
  552. {
  553. List<object> pos = new List<object>();
  554. foreach (var s in ETH_S7SimensCommands.posflag)
  555. {
  556. func = new Func<byte[], int, object>(AnalysisDoubleData);
  557. ETH_S7SimensCommands.REMAIN_POS[26] = s;
  558. GetData(ETH_S7SimensCommands.REMAIN_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  559. pos.Add(result);
  560. }
  561. if (pos != null)
  562. {
  563. result = string.Join("/", pos);
  564. return true;
  565. }
  566. else
  567. {
  568. return false;
  569. }
  570. }
  571. catch
  572. {
  573. return false;
  574. }
  575. finally
  576. {
  577. }
  578. }
  579. /// <summary>
  580. /// 获取刀具号T
  581. /// </summary>
  582. /// <param name="no"></param>
  583. /// <returns></returns>
  584. public bool ETH_S7SimensGetToolNum(ref object result)
  585. {
  586. try
  587. {
  588. func = new Func<byte[], int, object>(AnalysisInt32Data);
  589. return GetData(ETH_S7SimensCommands.TOOL_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
  590. }
  591. catch
  592. {
  593. return false;
  594. }
  595. finally
  596. {
  597. }
  598. }
  599. /// <summary>
  600. /// 获取报警号
  601. /// </summary>
  602. /// <param name="no"></param>
  603. /// <param name="result"></param>
  604. /// <returns></returns>
  605. public bool ETH_S7SimensGetAlarmMsgNum(ref object result)
  606. {
  607. try
  608. {
  609. func = new Func<byte[], int, object>(AnalysisInt32Data);
  610. return GetData(ETH_S7SimensCommands.CNC_ALARM_NUM, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
  611. }
  612. catch
  613. {
  614. return false;
  615. }
  616. finally
  617. {
  618. }
  619. }
  620. /// <summary>
  621. /// 获取报警信息
  622. /// </summary>
  623. /// <param name="no"></param>
  624. /// <returns></returns>
  625. public bool ETH_S7SimensGetAlarmMsg(byte numflag, ref object result)
  626. {
  627. try
  628. {
  629. ETH_S7SimensCommands.CNC_ALARM[26] = numflag;
  630. func = new Func<byte[], int, object>(AnalysisAlarm);
  631. return GetData(ETH_S7SimensCommands.CNC_ALARM, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
  632. }catch
  633. {
  634. return false;
  635. }
  636. finally
  637. {
  638. }
  639. }
  640. /// <summary>
  641. /// 获取刀具半径补偿编号
  642. /// </summary>
  643. /// <param name="no"></param>
  644. /// <returns></returns>
  645. public bool ETH_S7SimensGetDNum(ref object result)
  646. {
  647. func = new Func<byte[], int, object>(AnalysisInt32Data);
  648. return GetData(ETH_S7SimensCommands.TOOL_D_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func,ref result);
  649. }
  650. /// <summary>
  651. /// 获取刀具长度补偿编号
  652. /// </summary>
  653. /// <param name="no"></param>
  654. /// <returns></returns>
  655. public bool ETH_S7SimensGetHNum(ref object result)
  656. {
  657. func = new Func<byte[], int, object>(AnalysisInt32Data);
  658. return GetData(ETH_S7SimensCommands.TOOL_H_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
  659. }
  660. /// <summary>
  661. /// 获取长度补偿X
  662. /// </summary>
  663. /// <param name="no"></param>
  664. /// <returns></returns>
  665. public bool ETH_S7SimensGetLengthX(ref object result)
  666. {
  667. func = new Func<byte[], int, object>(AnalysisDoubleData);
  668. return GetData(ETH_S7SimensCommands.TOOL_X_LENGTH, (int)DataTypeEnum.DATATYPE_DOUBLE, func,ref result);
  669. }
  670. /// <summary>
  671. /// 获取长度补偿Z
  672. /// </summary>
  673. /// <param name="no"></param>
  674. /// <returns></returns>
  675. public bool ETH_S7SimensGetLengthZ(ref object result)
  676. {
  677. func = new Func<byte[], int, object>(AnalysisDoubleData);
  678. return GetData(ETH_S7SimensCommands.TOOL_Z_LENGTH, (int)DataTypeEnum.DATATYPE_DOUBLE, func,ref result);
  679. }
  680. /// <summary>
  681. /// 获取刀具磨损半径
  682. /// </summary>
  683. /// <param name="no"></param>
  684. /// <returns></returns>
  685. public bool ETH_S7SimensGetRadius(ref object result)
  686. {
  687. func = new Func<byte[], int, object>(AnalysisDoubleData);
  688. return GetData(ETH_S7SimensCommands.TOOL_RADIU, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  689. }
  690. /// <summary>
  691. /// 获取刀沿位置
  692. /// </summary>
  693. /// <param name="no"></param>
  694. /// <returns></returns>
  695. public bool ETH_S7SimensGetEDG(ref object result)
  696. {
  697. func = new Func<byte[], int, object>(AnalysisDoubleData);
  698. return GetData(ETH_S7SimensCommands.TOOL_EDG, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  699. }
  700. /// <summary>
  701. /// 获取母线电压
  702. /// </summary>
  703. /// <param name="no"></param>
  704. /// <returns></returns>
  705. public bool ETH_S7SimensGetVoltage(ref object result)
  706. {
  707. try
  708. {
  709. func = new Func<byte[], int, object>(AnalysisFloatData);
  710. return GetData(ETH_S7SimensCommands.DRIVE_VOLTAGE, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  711. }
  712. catch
  713. {
  714. return false;
  715. }
  716. finally
  717. {
  718. }
  719. }
  720. /// <summary>
  721. /// 获取电机功率
  722. /// </summary>
  723. /// <param name="no"></param>
  724. /// <returns></returns>
  725. public bool ETH_S7SimensGetLoad(ref object result)
  726. {
  727. try
  728. {
  729. func = new Func<byte[], int, object>(AnalysisFloatData);
  730. return GetData(ETH_S7SimensCommands.DRIVER_LOAD1, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  731. }
  732. catch
  733. {
  734. return false;
  735. }
  736. finally
  737. {
  738. }
  739. }
  740. public bool ETH_S7SimensGetSPLoad1(ref object result)
  741. {
  742. try
  743. {
  744. func = new Func<byte[], int, object>(AnalysisFloatData);
  745. return GetData(ETH_S7SimensCommands.DRIVER_SPLOAD, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  746. }
  747. catch
  748. {
  749. return false;
  750. }
  751. finally
  752. {
  753. }
  754. }
  755. public bool ETH_S7SimensGetSPLoad2(ref object result)
  756. {
  757. try
  758. {
  759. func = new Func<byte[], int, object>(AnalysisFloatData);
  760. return GetData(ETH_S7SimensCommands.DRIVER_SPLOAD2, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  761. }
  762. catch
  763. {
  764. return false;
  765. }
  766. finally
  767. {
  768. }
  769. }
  770. /// <summary>
  771. /// 获取电机温度
  772. /// </summary>
  773. /// <param name="no"></param>
  774. /// <returns></returns>
  775. public bool ETH_S7SimensGetTemper(ref object result,byte flag)
  776. {
  777. try
  778. {
  779. ETH_S7SimensCommands.DRIVER_TEMPER[22] = flag;
  780. func = new Func<byte[], int, object>(AnalysisFloatData);
  781. return GetData(ETH_S7SimensCommands.DRIVER_TEMPER, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  782. }
  783. catch
  784. {
  785. return false;
  786. }
  787. finally
  788. {
  789. }
  790. }
  791. /// <summary>
  792. /// 获取电流
  793. /// </summary>
  794. /// <param name="no"></param>
  795. /// <returns></returns>
  796. public bool ETH_S7SimensGetCurrent(ref object result)
  797. {
  798. try
  799. {
  800. func = new Func<byte[], int, object>(AnalysisFloatData);
  801. return GetData(ETH_S7SimensCommands.DRIVER_CURRENT, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  802. }
  803. catch
  804. {
  805. return false;
  806. }
  807. finally
  808. {
  809. }
  810. }
  811. //public static byte[] R_Driver ={
  812. // 0x03, 0x00, 0x00, 0x1d, //12+17=29
  813. // 0x02, 0xf0, 0x80, 0x32, 0x01,
  814. // 0x00, 0x00, 0x00, 0x14,
  815. // 0x00, 0x0c, //10+2
  816. // 0x00, 0x00,
  817. // 0x04,
  818. // 0x01,
  819. // 0x12, 0x08, 0x82, 0xa1, 0x00, 0x25, 0x00, 0x01, 0x82, 0x01,//倒数第三位R驱动器下标,如R37[1],0X03=R37[2];倒数第五位为地址,如25为十进制37,第七位为轴,依次递增切换轴
  820. // 0x03, 0x00, 0x00, 0x07, 0x02, 0xf0, 0x00
  821. //};
  822. /// <summary>
  823. /// 获取驱动器
  824. /// </summary>
  825. /// <param name="no">无意义</param>
  826. /// <param name="adrFlag"></param>
  827. /// <param name="axisFlag">byte[] aixsflag = new byte[] { 0xa1, 0xa2, 0xa3 };//s,,x,y轴标识 </param>
  828. /// <param name="flag">下标标识</param>
  829. /// <param name="result"></param>
  830. /// <returns></returns>】
  831. public bool ETH_S7SimensGetRDriver(byte adrFlag, byte axisFlag,byte flag, ref object result)
  832. {
  833. try
  834. {
  835. ETH_S7SimensCommands.R_Driver[24] = adrFlag;
  836. ETH_S7SimensCommands.R_Driver[22] = axisFlag;
  837. ETH_S7SimensCommands.R_Driver[26] = flag;
  838. func = new Func<byte[], int, object>(AnalysisFloatData);
  839. return GetData(ETH_S7SimensCommands.R_Driver, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  840. }catch
  841. {
  842. return false;
  843. }
  844. finally
  845. {
  846. }
  847. }
  848. /// <summary>
  849. /// 读取R变量
  850. /// </summary>
  851. /// <returns></returns>
  852. public bool ETH_S7SimensGetRValue(byte[] flag, ref object result)
  853. {
  854. try
  855. {
  856. ETH_S7SimensCommands.CNC_READ_R[26] = flag[0];
  857. ETH_S7SimensCommands.CNC_READ_R[25] = flag[1];
  858. func = new Func<byte[], int, object>(AnalysisDoubleData);
  859. return GetData(ETH_S7SimensCommands.CNC_READ_R, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
  860. }
  861. catch
  862. {
  863. return true;
  864. }
  865. finally
  866. {
  867. }
  868. }
  869. bool GetData(byte[] byteMessage, int dataType,Func<byte[],int,object> func,ref object result)
  870. {
  871. socket.Send(byteMessage);
  872. Thread.Sleep(10);
  873. buffer = new byte[1024];
  874. receiveData = socket.Receive(buffer);
  875. //监听到消息
  876. if (receiveData > 0)
  877. {
  878. result= func(buffer,receiveData) ;
  879. return true;
  880. }
  881. else
  882. {
  883. return false;
  884. }
  885. }
  886. /// <summary>
  887. /// 解析字符串数据
  888. /// </summary>
  889. /// <param name="byteMessage"></param>
  890. /// <param name="length"></param>
  891. /// <returns></returns>
  892. string AnalysisStrData(byte[] byteMessage, int length)
  893. {
  894. datas = new byte[length] ;
  895. Array.Copy(byteMessage, datas, length);
  896. var ss=BitConverter.ToString(datas);
  897. var s= (Encoding.GetEncoding("UTF-8").GetString(datas.Skip(25).ToArray())).Replace("\0", "");
  898. if (s=="")
  899. {
  900. return "未加工";
  901. }
  902. else
  903. {
  904. return s;
  905. }
  906. }
  907. public bool ETH_S7SimensGetTemper_a1(ref object result)
  908. {
  909. try
  910. {
  911. func = new Func<byte[], int, object>(AnalysisFloatData);
  912. return GetData(ETH_S7SimensCommands.DRIVER_TEMPER, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
  913. }
  914. catch
  915. {
  916. return false;
  917. }
  918. finally
  919. {
  920. }
  921. }
  922. /// <summary>
  923. /// 解析操作模式
  924. /// </summary>
  925. /// <param name="byteMessage"></param>
  926. /// <param name="length"></param>
  927. /// <returns></returns>
  928. object AnalusisModeData(byte[] byteMessage, int length)
  929. {
  930. try
  931. {
  932. datas = new byte[length];
  933. Array.Copy(byteMessage, datas, length);
  934. if (datas[24] == 0x02)
  935. {
  936. if (datas[31] == 0x00)
  937. {
  938. if (datas[25] == 0x00)
  939. {
  940. return SimensMode.JOG;
  941. }
  942. else if (datas[25] == 0x01)
  943. {
  944. return SimensMode.MDA;
  945. }
  946. else if (datas[25] == 0x02)
  947. {
  948. return SimensMode.AUTO;
  949. }
  950. else
  951. {
  952. return SimensMode.OTHER;
  953. }
  954. }
  955. else if (datas[31] == 0x03)
  956. {
  957. return SimensMode.REFPOINT;
  958. }
  959. else
  960. {
  961. return SimensMode.OTHER;
  962. }
  963. }
  964. else
  965. {
  966. return SimensMode.OTHER;
  967. }
  968. }
  969. catch
  970. {
  971. return SimensMode.OTHER;
  972. }
  973. }
  974. /// <summary>
  975. /// 运行状态
  976. /// </summary>
  977. /// <param name="byteMessage"></param>
  978. /// <param name="length"></param>
  979. /// <returns></returns>
  980. object AnalusisStatusData(byte[] byteMessage, int length)
  981. {
  982. try
  983. {
  984. datas = new byte[length];
  985. Array.Copy(byteMessage, datas, length);
  986. if (datas[24] == 0x02)
  987. {
  988. if ((datas[25] == 0x00) && (datas[31] == 0x05)) return SimensStatus.RESET;
  989. else if ((datas[25] == 0x02) && (datas[31] == 0x02)) return SimensStatus.STOP;
  990. else if ((datas[25] == 0x01) && (datas[31] == 0x03)) return SimensStatus.START;
  991. else if ((datas[25] == 0x01) && (datas[31] == 0x05)) return SimensStatus.SPENDLE_CW_CCW;
  992. else return SimensStatus.OTHER;
  993. }
  994. else
  995. {
  996. return SimensStatus.OTHER;
  997. }
  998. }
  999. catch
  1000. {
  1001. return SimensStatus.OTHER;
  1002. }
  1003. }
  1004. /// <summary>
  1005. /// 解析小端序float
  1006. /// </summary>
  1007. /// <param name="byteMessage"></param>
  1008. /// <param name="length"></param>
  1009. /// <returns></returns>
  1010. object AnalysisFloatData(byte[] byteMessage, int length)
  1011. {
  1012. try
  1013. {
  1014. datas = new byte[length];
  1015. Array.Copy(byteMessage, datas, length);
  1016. //Console.WriteLine(BitConverter.ToString(datas));
  1017. var value = BitConverter.ToSingle(datas.Skip(25).Reverse().ToArray(), 0);
  1018. return value;
  1019. }
  1020. catch(Exception ex)
  1021. {
  1022. return ((int)DataTypeEnum.DATATYPE_ERRO);
  1023. }
  1024. }
  1025. /// <summary>
  1026. /// 解析double
  1027. /// </summary>
  1028. /// <param name="byteMessage"></param>
  1029. /// <param name="length"></param>
  1030. /// <returns></returns>
  1031. object AnalysisDoubleData(byte[] byteMessage, int length)
  1032. {
  1033. object value = null;
  1034. try
  1035. {
  1036. datas = new byte[length];
  1037. Array.Copy(byteMessage, datas, length);;
  1038. if (datas[3] == 33)
  1039. {
  1040. value = BitConverter.ToDouble(datas.Skip(25).Take(8).ToArray(), 0);
  1041. }
  1042. else
  1043. {
  1044. value = BitConverter.ToDouble(datas.Skip(0).ToArray(), 0);
  1045. }
  1046. return value;
  1047. }
  1048. catch (Exception ex)
  1049. {
  1050. return ((int)DataTypeEnum.DATATYPE_ERRO);
  1051. }
  1052. }
  1053. /// <summary>
  1054. /// 解析整型数据
  1055. /// </summary>
  1056. object AnalysisInt32Data(byte[] byteMessage, int length)
  1057. {
  1058. try
  1059. {
  1060. datas = new byte[length];
  1061. Array.Copy(byteMessage, datas, length);
  1062. var ss = BitConverter.ToString(datas);
  1063. var s = (Encoding.GetEncoding("UTF-8").GetString(datas.Skip(25).ToArray())).Replace("\0", "");
  1064. var value = BitConverter.ToInt16(datas.Skip(25).Take(2).ToArray(), 0);
  1065. return value;
  1066. }
  1067. catch (Exception ex)
  1068. {
  1069. return (int)DataTypeEnum.DATATYPE_ERRO;
  1070. }
  1071. }
  1072. /// <summary>
  1073. /// 解析报警号
  1074. /// </summary>
  1075. object AnalysisAlarm(byte[] byteMessage, int length)
  1076. {
  1077. try
  1078. {
  1079. datas = new byte[length];
  1080. Array.Copy(byteMessage, datas, length);
  1081. Console.WriteLine(BitConverter.ToString(datas));
  1082. var s = datas.Skip(25).Take(4).ToArray();
  1083. var value = BitConverter.ToInt32(s, 0);
  1084. return value;
  1085. }
  1086. catch (Exception ex)
  1087. {
  1088. return (int)DataTypeEnum.DATATYPE_ERRO;
  1089. }
  1090. }
  1091. object AnalysisWorkSystem(byte[] byteMessage, int length)
  1092. {
  1093. try
  1094. {
  1095. datas = new byte[length];
  1096. Array.Copy(byteMessage, datas, length);
  1097. var s = datas.Skip(39).Take(2).ToArray();
  1098. var value = BitConverter.ToInt16(s, 0);
  1099. return value;
  1100. }
  1101. catch (Exception ex)
  1102. {
  1103. return (int)DataTypeEnum.DATATYPE_ERRO;
  1104. }
  1105. }
  1106. }
  1107. }