SimenssPlc.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using PLC.Siemens.Communication;
  6. using PLC.Siemens.Protocol.Common;
  7. using PLC.Siemens.Protocol.Iso;
  8. using PLC.Siemens.Protocol.Negotiate;
  9. using PLC.Siemens.Protocol.ReadData;
  10. using PLC.Siemens.Protocol.Szl;
  11. using PLC.Siemens.ProtocolHandle;
  12. using PLC.Siemens.O;
  13. using System.Linq;
  14. namespace PLC.Siemens.O
  15. {
  16. public class SimenssPlc
  17. {
  18. public ConnectionType ConnectionType { get; private set; }
  19. public ushort SrcRef { get; set; }
  20. public ushort DstRef { get; set; }
  21. public ushort SrcTSap { get; private set; }
  22. public ushort DstTSap { get; private set; }
  23. public ushort PduRequest { get; set; }
  24. public int RecvTimeout { get; set; }
  25. public int RemotePort { get; set; }
  26. public int IsoPduSize { get; set; }
  27. // ReSharper disable once InconsistentNaming
  28. public string IP { get; private set; }
  29. public bool Connected
  30. {
  31. get
  32. {
  33. if (_socket == null)
  34. return false;
  35. return _socket.Connected;
  36. }
  37. }
  38. private Action<PLC.Siemens.O.MessageEvent> _onMessageEvent;
  39. private readonly IsoSocket _socket;
  40. public SimenssPlc()
  41. {
  42. _socket = new IsoSocket();
  43. PduRequest = 480;
  44. IsoPduSize = 1024;
  45. RecvTimeout = 3000;
  46. RemotePort = 102;
  47. SrcRef = 0x0001; // RFC0983 states that SrcRef and DetRef should be 0
  48. // and, in any case, they are ignored.
  49. // S7 instead requires a number != 0
  50. // Libnodave uses 0x0100
  51. // S7Manager uses 0x0D00
  52. // TIA Portal V12 uses 0x1D00
  53. // WinCC uses 0x0300
  54. // Seems that every non zero value is good enough...
  55. DstRef = 0x0000;
  56. SrcTSap = 0x0100;
  57. DstTSap = 0x0000; // It's filled by connection functions
  58. ConnectionType = ConnectionType.PG;
  59. }
  60. public SimenssPlc(string ip, int rack, int slot)
  61. {
  62. _socket = new IsoSocket();
  63. PduRequest = 480*2;
  64. IsoPduSize = 1024;
  65. RecvTimeout = 3000;
  66. RemotePort = 102;
  67. SrcRef = 0x0001; // RFC0983 states that SrcRef and DetRef should be 0
  68. // and, in any case, they are ignored.
  69. // S7 instead requires a number != 0
  70. // Libnodave uses 0x0100
  71. // S7Manager uses 0x0D00
  72. // TIA Portal V12 uses 0x1D00
  73. // WinCC uses 0x0300
  74. // Seems that every non zero value is good enough...
  75. DstRef = 0x0000;
  76. SrcTSap = 0x0100;
  77. DstTSap = 0x0000; // It's filled by connection functions
  78. ConnectionType = ConnectionType.PG;
  79. var remoteTsap = (ushort)(((ushort)ConnectionType << 8) + (rack * 0x20) + slot);
  80. SetConnectionParams(ip, SrcTSap, remoteTsap);
  81. }
  82. #region 连接
  83. public void RegisterMessageEvent(Action<MessageEvent> messageAction)
  84. {
  85. if (messageAction == null) throw new ArgumentNullException("messageAction");
  86. _onMessageEvent = messageAction;
  87. }
  88. private void OnMessage(string method, string message)
  89. {
  90. if(_onMessageEvent!=null)
  91. _onMessageEvent.Invoke(new MessageEvent(method, message));
  92. }
  93. public void SetConnectionType(ConnectionType connectionType)
  94. {
  95. ConnectionType = connectionType;
  96. }
  97. public void SetConnectionParams(string ip, ushort localTsap, ushort remoteTsap)
  98. {
  99. SrcTSap = localTsap;
  100. DstTSap = remoteTsap;
  101. IP = ip;
  102. }
  103. public bool ConnectTo(string ip, int rack, int slot)
  104. {
  105. var remoteTsap = (ushort)(((ushort)ConnectionType << 8) + (rack * 0x20) + slot);
  106. SetConnectionParams(ip, SrcTSap, remoteTsap);
  107. return Connect();
  108. }
  109. //暂时用系统SOCKET测试
  110. private int _connecting;//连接中
  111. private int _processing;//处理中
  112. private string _processMehtod;
  113. public bool Connect()
  114. {
  115. try
  116. {
  117. if (Interlocked.CompareExchange(ref _connecting, 1, 0) != 0)
  118. return false;
  119. OnMessage("Connect", "正在连接");
  120. if (!_socket.Connect(IP, 102))
  121. return false;
  122. IsoControlPduHandle pduHandle = new IsoControlPduHandle()
  123. {
  124. IsoPduSize = IsoPduSize,
  125. DstRef = DstRef,
  126. DstTSap = DstTSap,
  127. SrcRef = SrcRef,
  128. SrcTSap = SrcTSap
  129. };
  130. pduHandle.Handle(_socket);
  131. if (pduHandle.ResultCode == ResultCode.OK)
  132. {
  133. NegotiateHandle negotiateHandle = new NegotiateHandle() { PduRequest = PduRequest };
  134. negotiateHandle.Handle(_socket);
  135. if (negotiateHandle.ResultCode == ResultCode.OK)
  136. {
  137. PduRequest = negotiateHandle.Responses[0].Params.PduLength;
  138. return true;
  139. }
  140. else
  141. {
  142. OnMessage("Connect", "PDU握手失败");
  143. }
  144. }
  145. else
  146. {
  147. OnMessage("Connect", "PDU握手失败");
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. OnMessage("Connect", ex.Message);
  153. }
  154. finally
  155. {
  156. Interlocked.CompareExchange(ref _connecting, 0, 1);
  157. }
  158. return false;
  159. }
  160. #endregion
  161. public bool GetLock(string method)
  162. {
  163. if (Interlocked.CompareExchange(ref _processing, 1, 0) != 0)
  164. {
  165. OnMessage(method, "执行失败,{_processMehtod}处理正在执行中");
  166. return false;
  167. }
  168. _processMehtod = method;
  169. return true;
  170. }
  171. public bool ReleaseLock()
  172. {
  173. _processMehtod = "";
  174. return Interlocked.CompareExchange(ref _processing, 0, 1) == 0;
  175. }
  176. #region [System Info functions]
  177. private SzlResponse ReadSzlHelper(SzlCmd szlcmd)
  178. {
  179. SzlHandle handle = new SzlHandle { SzlCmd = szlcmd };
  180. handle.Handle(_socket);
  181. return handle.SzlResponseFirst;
  182. }
  183. public SzlResponse ReadSzl(SzlCmd szlcmd)
  184. {
  185. if (!GetLock("ReadSzl"))
  186. {
  187. return null;
  188. }
  189. try
  190. {
  191. SzlHandle handle = new SzlHandle { SzlCmd = szlcmd };
  192. handle.Handle(_socket);
  193. OnMessage("ReadSzl", "{handle.ResultCode}");
  194. return handle.SzlResponseFirst;
  195. }
  196. catch (Exception ex)
  197. {
  198. OnMessage("ReadSzl", ex.Message);
  199. }
  200. finally
  201. {
  202. ReleaseLock();
  203. }
  204. return null;
  205. }
  206. public OrderInfo GetOrderCode()
  207. {
  208. OrderInfo orderCode = new OrderInfo();
  209. if (!GetLock("GetOrderCode"))
  210. {
  211. return orderCode;
  212. }
  213. try
  214. {
  215. var aaaa = ReadSzlHelper(SzlInfo.OrderCode);
  216. OnMessage("GetOrderCode", aaaa.Params.Err.ToString());
  217. if (aaaa.Params.Err != ResultCode.OK)
  218. return orderCode;
  219. orderCode.Code = Encoding.UTF8.GetString(aaaa.Data.Data, 2, 20);
  220. orderCode.V1 = aaaa.Data.Data[aaaa.Data.Data.Length - 3];
  221. orderCode.V2 = aaaa.Data.Data[aaaa.Data.Data.Length - 2];
  222. orderCode.V3 = aaaa.Data.Data[aaaa.Data.Data.Length - 1];
  223. return orderCode;
  224. }
  225. catch (Exception ex)
  226. {
  227. OnMessage("GetOrderCode", ex.Message);
  228. return orderCode;
  229. }
  230. finally
  231. {
  232. ReleaseLock();
  233. }
  234. }
  235. public CpuInfo GetCpuInfo()
  236. {
  237. CpuInfo cpuInfo = new CpuInfo();
  238. if (!GetLock("GetCpuInfo"))
  239. return cpuInfo;
  240. try
  241. {
  242. var aaaa = ReadSzlHelper(SzlInfo.CpuInfo);
  243. OnMessage("GetCpuInfo", aaaa.Params.Err.ToString());
  244. if (aaaa.Params.Err != ResultCode.OK)
  245. return cpuInfo;
  246. cpuInfo.ModuleTypeName = Encoding.UTF8.GetString(aaaa.Data.Data, 172, 32).TrimEnd('\0');
  247. cpuInfo.SerialNumber = Encoding.UTF8.GetString(aaaa.Data.Data, 138, 24).TrimEnd('\0');
  248. cpuInfo.AsName = Encoding.UTF8.GetString(aaaa.Data.Data, 2, 24).TrimEnd('\0');
  249. cpuInfo.Copyright = Encoding.UTF8.GetString(aaaa.Data.Data, 104, 26).TrimEnd('\0');
  250. cpuInfo.ModuleName = Encoding.UTF8.GetString(aaaa.Data.Data, 36, 24).TrimEnd('\0');
  251. }
  252. catch (Exception ex)
  253. {
  254. OnMessage("GetCpuInfo", ex.Message);
  255. }
  256. finally
  257. {
  258. ReleaseLock();
  259. }
  260. return cpuInfo;
  261. }
  262. public CpInfo GetCpInfo()
  263. {
  264. CpInfo info = new CpInfo();
  265. if (!GetLock("GetCpuInfo"))
  266. return info;
  267. try
  268. {
  269. var aaaa = ReadSzlHelper(SzlInfo.CpInfo);
  270. OnMessage("GetCpInfo", aaaa.Params.Err.ToString());
  271. if (aaaa.Params.Err != ResultCode.OK)
  272. return info;
  273. info.MaxPduLengt = DWordAt(aaaa.Data.Data, 2, 2);
  274. //aaaa.ResDataFirst.Data[2] * 256 + aaaa.ResDataFirst.Data[3];
  275. info.MaxConnections = DWordAt(aaaa.Data.Data, 4, 2);
  276. // aaaa.ResDataFirst.Data[4] * 256 + aaaa.ResDataFirst.Data[5];
  277. info.MaxMpiRate = DWordAt(aaaa.Data.Data, 6, 4);
  278. //aaaa.ResDataFirst.Data[6] * 256 + aaaa.ResDataFirst.Data[7];
  279. info.MaxBusRate = DWordAt(aaaa.Data.Data, 10, 4);
  280. //aaaa.ResDataFirst.Data[10] * 256 + aaaa.ResDataFirst.Data[7];
  281. }
  282. catch (Exception ex)
  283. {
  284. OnMessage("GetCpInfo", ex.Message);
  285. }
  286. finally
  287. {
  288. ReleaseLock();
  289. }
  290. return info;
  291. }
  292. #endregion
  293. private int DWordAt(byte[] buffer, int index, int length)
  294. {
  295. int value = 0;
  296. for (int i = 0; i < length; i++)
  297. {
  298. value = (value << 8) + buffer[index + i];
  299. }
  300. return value;
  301. }
  302. #region [Date/Time functions]
  303. public DateTime GetS7DateTime()
  304. {
  305. if (!GetLock("GetS7DateTime"))
  306. return DateTime.MinValue;
  307. try
  308. {
  309. GetDateTimeHandle handle = new GetDateTimeHandle();
  310. handle.Handle(_socket);
  311. OnMessage("GetS7DateTime", handle.ResultCode.ToString());
  312. if (handle.ResultCode == ResultCode.OK)
  313. {
  314. return handle.Response.GetDateTime();
  315. }
  316. else
  317. {
  318. return DateTime.MinValue;
  319. }
  320. }
  321. catch (Exception ex)
  322. {
  323. OnMessage("GetS7DateTime", ex.Message);
  324. return DateTime.MinValue;
  325. }
  326. finally
  327. {
  328. ReleaseLock();
  329. }
  330. }
  331. public bool SetS7DateTime(DateTime curDateTime)
  332. {
  333. if (!GetLock("SetS7DateTime"))
  334. return false;
  335. try
  336. {
  337. SetDateTimeHandle handle = new SetDateTimeHandle { DateTime = curDateTime };
  338. handle.Handle(_socket);
  339. OnMessage("SetS7DateTime", handle.ResultCode.ToString());
  340. return handle.ResultCode == ResultCode.OKFF;
  341. }
  342. catch (Exception ex)
  343. {
  344. OnMessage("SetS7DateTime", ex.Message);
  345. return false;
  346. }
  347. finally
  348. {
  349. ReleaseLock();
  350. }
  351. }
  352. #endregion
  353. #region [Control functions]
  354. public S7CpuStatus GetPlcStatus()
  355. {
  356. S7CpuStatus status = S7CpuStatus.Unknown;
  357. if (!GetLock("GetPlcStatus"))
  358. {
  359. return status;
  360. }
  361. try
  362. {
  363. SzlResponse first = ReadSzlHelper(SzlInfo.PlcStatus);
  364. OnMessage("GetPlcStatus", first.Params.Err.ToString());
  365. if (first.Params.Err == ResultCode.OK)
  366. {
  367. status = (S7CpuStatus)first.Data.Data[3];
  368. if (status != S7CpuStatus.Run)
  369. status = S7CpuStatus.Stop;
  370. }
  371. return status;
  372. }
  373. catch (Exception ex)
  374. {
  375. OnMessage("GetPlcStatus", ex.Message);
  376. return status;
  377. }
  378. finally
  379. {
  380. ReleaseLock();
  381. }
  382. }
  383. public bool PlcStop()
  384. {
  385. if (!GetLock("PlcStop"))
  386. return false;
  387. try
  388. {
  389. var handle = new PlcStopHandle();
  390. handle.Handle(_socket);
  391. OnMessage("PlcStop", handle.ResultCode.ToString());
  392. return handle.ResultCode == ResultCode.OK;
  393. }
  394. catch (Exception ex)
  395. {
  396. OnMessage("PlcStop", ex.Message);
  397. return false;
  398. }
  399. finally
  400. {
  401. ReleaseLock();
  402. }
  403. }
  404. public bool PlcHotStart()
  405. {
  406. if (!GetLock("PlcHotStart"))
  407. return false;
  408. try
  409. {
  410. var handle = new PlcHotStartHandle();
  411. handle.Handle(_socket);
  412. OnMessage("PlcHotStart", handle.ResultCode.ToString());
  413. return handle.ResultCode == ResultCode.OK;
  414. }
  415. catch (Exception ex)
  416. {
  417. OnMessage("PlcHotStart", ex.Message);
  418. return false;
  419. }
  420. finally
  421. {
  422. ReleaseLock();
  423. }
  424. }
  425. public bool PlcColdStart()
  426. {
  427. if (!GetLock("PlcColdStart"))
  428. return false;
  429. try
  430. {
  431. var handle = new PlcColdStartHandle();
  432. handle.Handle(_socket);
  433. OnMessage("PlcColdStart", handle.ResultCode.ToString());
  434. return handle.ResultCode == ResultCode.OK;
  435. }
  436. catch (Exception ex)
  437. {
  438. OnMessage("PlcColdStart", ex.Message);
  439. return false;
  440. }
  441. finally
  442. {
  443. ReleaseLock();
  444. }
  445. }
  446. public bool CopyRamToRom()
  447. {
  448. if (!GetLock("CopyRamToRom"))
  449. return false;
  450. try
  451. {
  452. var handle = new CopyRamToRomHandle();
  453. handle.Handle(_socket);
  454. OnMessage("CopyRamToRom", handle.ResultCode.ToString());
  455. return handle.ResultCode == ResultCode.OK;
  456. }
  457. catch (Exception ex)
  458. {
  459. OnMessage("CopyRamToRom", ex.Message);
  460. return false;
  461. }
  462. finally
  463. {
  464. ReleaseLock();
  465. }
  466. }
  467. public bool Compress()
  468. {
  469. if (!GetLock("Compress"))
  470. return false;
  471. try
  472. {
  473. var handle = new CompressHandle();
  474. handle.Handle(_socket);
  475. OnMessage("Compress", handle.Response.Header.Error.ToString());
  476. return handle.ResultCode == ResultCode.OK;
  477. }
  478. catch (Exception ex)
  479. {
  480. OnMessage("Compress", ex.Message);
  481. return false;
  482. }
  483. finally
  484. {
  485. ReleaseLock();
  486. }
  487. }
  488. #endregion
  489. #region [Security functions]
  490. public ProtectionInfo GetProtection()
  491. {
  492. ProtectionInfo info = new ProtectionInfo();
  493. if (!GetLock("GetProtection"))
  494. {
  495. return info;
  496. }
  497. try
  498. {
  499. var sss = ReadSzlHelper(SzlInfo.Protection);
  500. OnMessage("GetProtection", sss.Data.Ret.ToString());
  501. if (sss.Params.Err != ResultCode.OK) return info;
  502. info.SchSchal = (RWLevel)DWordAt(sss.Data.Data, 2, 2);
  503. info.SchPar = (ProtectionLevel)DWordAt(sss.Data.Data, 4, 2);
  504. info.SchRel = (CpuLevel)DWordAt(sss.Data.Data, 6, 2);
  505. info.BartSch = (RunStatus)DWordAt(sss.Data.Data, 8, 2);
  506. info.AnlSch = (StartupSwitch)DWordAt(sss.Data.Data, 10, 2);
  507. return info;
  508. }
  509. catch (Exception ex)
  510. {
  511. OnMessage("GetProtection", ex.Message);
  512. return info;
  513. }
  514. finally
  515. {
  516. ReleaseLock();
  517. }
  518. }
  519. public bool SetPwd(string password)
  520. {
  521. if (!GetLock("SetPwd")) return false;
  522. try
  523. {
  524. var handle = new SetPasswordHandle { Password = password };
  525. handle.Handle(_socket);
  526. OnMessage("SetPwd", handle.Response.Params.Err.ToString());
  527. return handle.ResultCode == ResultCode.OK;
  528. }
  529. catch (Exception ex)
  530. {
  531. OnMessage("SetPwd", ex.Message);
  532. return false;
  533. }
  534. finally
  535. {
  536. ReleaseLock();
  537. }
  538. }
  539. public bool ClearPwd()
  540. {
  541. if (!GetLock("ClearPwd")) return false;
  542. try
  543. {
  544. var handle = new SetPasswordHandle();
  545. handle.Handle(_socket);
  546. OnMessage("ClearPwd", handle.ResultCode.ToString());
  547. return handle.ResultCode == ResultCode.OK;
  548. }
  549. catch (Exception ex)
  550. {
  551. OnMessage("ClearPwd", ex.Message);
  552. return false;
  553. }
  554. finally
  555. {
  556. ReleaseLock();
  557. }
  558. }
  559. #endregion
  560. #region [Blocks]
  561. public ListBlocksHandle ListBlocks()
  562. {
  563. if (!GetLock("ListBlocks")) return null;
  564. try
  565. {
  566. var handle = new ListBlocksHandle();
  567. handle.Handle(_socket);
  568. OnMessage("ListBlocks", handle.ResultCode.ToString());
  569. return handle;
  570. }
  571. catch (Exception ex)
  572. {
  573. OnMessage("ListBlocks", ex.Message);
  574. return null;
  575. }
  576. finally
  577. {
  578. ReleaseLock();
  579. }
  580. }
  581. public DataBlockOfTypeHandle ListBlocksOfType(BlockType blockType)
  582. {
  583. if (!GetLock("ListBlocksOfType")) return null;
  584. try
  585. {
  586. var handle = new DataBlockOfTypeHandle();
  587. handle.BlockType = blockType;
  588. handle.Handle(_socket);
  589. OnMessage("ListBlocksOfType", handle.ResultCode.ToString());
  590. return handle;
  591. }
  592. catch (Exception ex)
  593. {
  594. OnMessage("ListBlocksOfType", ex.Message);
  595. return null;
  596. }
  597. finally
  598. {
  599. ReleaseLock();
  600. }
  601. }
  602. public DataBlockInfoHandle BlockInfoGet(ushort db, BlockType blockType)
  603. {
  604. if (!GetLock("BlockInfoHandle")) return null;
  605. try
  606. {
  607. var handle = new DataBlockInfoHandle();
  608. handle.BlockType = blockType;
  609. handle.DB = db;
  610. handle.Handle(_socket);
  611. OnMessage("BlockInfoHandle", handle.ResultCode.ToString());
  612. return handle;
  613. }
  614. catch (Exception ex)
  615. {
  616. OnMessage("BlockInfoHandle", ex.Message);
  617. return null;
  618. }
  619. finally
  620. {
  621. ReleaseLock();
  622. }
  623. }
  624. #endregion
  625. object readlock = new object();
  626. public DataItem ReadArea(AreaType area, ushort db, ushort start, ushort amount, DataType dataType)
  627. {
  628. lock (readlock)
  629. {
  630. if (!GetLock("ReadArea"))
  631. {
  632. return null;
  633. }
  634. else
  635. {
  636. }
  637. try
  638. {
  639. var handle = new ReadHandle
  640. {
  641. PduLength = PduRequest,
  642. DataItem = new DataItem()
  643. {
  644. AreaType = area,
  645. Db = db,
  646. Start = start,
  647. Length = amount,
  648. DataType = dataType
  649. }
  650. };
  651. handle.Handle(_socket);
  652. OnMessage("ReadArea", handle.ResultCode.ToString());
  653. if (handle.ResultCode != ResultCode.OK)
  654. {
  655. throw new Exception(handle.ResultCode.ToString());
  656. }
  657. return handle.DataItem;
  658. }
  659. catch (Exception ex)
  660. {
  661. OnMessage("ReadArea", ex.Message);
  662. }
  663. finally
  664. {
  665. ReleaseLock();
  666. }
  667. return null;
  668. }
  669. }
  670. public ReadMultiHandle ReadMulti(List<DataItem> items)
  671. {
  672. if (!GetLock("ReadMulti")) return null;
  673. try
  674. {
  675. var handle = new ReadMultiHandle { DataItem = items };
  676. handle.Handle(_socket);
  677. OnMessage("ReadMulti", handle.ResultCode.ToString());
  678. return handle;
  679. }
  680. catch (Exception ex)
  681. {
  682. OnMessage("ReadMulti", ex.Message);
  683. return null;
  684. }
  685. finally
  686. {
  687. ReleaseLock();
  688. }
  689. }
  690. public bool WriteArea(AreaType area, ushort db, ushort start, ushort amount, DataType dataType, byte[] writeData)
  691. {
  692. if (IP == "192.168.0.120" && db == 520)
  693. {
  694. if (start % 36 == 16)
  695. {
  696. if (writeData.Max() == 0)
  697. {
  698. }
  699. }
  700. else if (start % 36 == 0)
  701. {
  702. if (writeData.Length > 16)
  703. {
  704. if (writeData.Skip(16).Take(2).Max() == 0)
  705. {
  706. }
  707. }
  708. else
  709. {
  710. }
  711. }
  712. }
  713. if (!GetLock("WriteArea"))
  714. return false;
  715. try
  716. {
  717. var handle = new WriteHandle()
  718. {
  719. PduLength = PduRequest,
  720. DataItem = new DataItem()
  721. {
  722. AreaType = area,
  723. Db = db,
  724. Start = start,
  725. Length = amount,
  726. DataType = dataType,
  727. Data = writeData
  728. }
  729. };
  730. OnMessage("WriteArea", handle.ResultCode.ToString());
  731. handle.Handle(_socket);
  732. return handle.ResultCode == ResultCode.OK|| handle.ResultCode == ResultCode.OKFF;
  733. }
  734. catch (Exception ex)
  735. {
  736. OnMessage("WriteArea", ex.Message);
  737. return false;
  738. }
  739. finally
  740. {
  741. ReleaseLock();
  742. }
  743. }
  744. public WriteMultiHandle WriteMulti(List<DataItem> items)
  745. {
  746. if (!GetLock("WriteMulti")) return null;
  747. try
  748. {
  749. var handle = new WriteMultiHandle { DataItem = items };
  750. handle.Handle(_socket);
  751. OnMessage("WriteMulti", handle.ResultCode.ToString());
  752. return handle;
  753. }
  754. catch (Exception ex)
  755. {
  756. OnMessage("WriteMulti", ex.Message);
  757. return null;
  758. }
  759. finally
  760. {
  761. ReleaseLock();
  762. }
  763. }
  764. // ReSharper disable once InconsistentNaming
  765. public DataItem DBGet(ushort db)
  766. {
  767. //if (!GetLock("DBGet")) return null;
  768. try
  769. {
  770. var handle = new DataBlockInfoHandle();
  771. handle.BlockType = BlockType.DB;
  772. handle.DB = db;
  773. handle.Handle(_socket);
  774. if (handle.ResultCode == ResultCode.OK)
  775. {
  776. var res = ReadArea(AreaType.DB, handle.DB, 0, handle.Response.Data.Mc7Len, DataType.Byte);
  777. if (res.Err == ResultCode.OKFF)
  778. {
  779. return res;
  780. }
  781. OnMessage("DBGet", "ReadArea:{handle.ResultCode}");
  782. }
  783. else
  784. OnMessage("DBGet", "RequestBlock:{handle.ResultCode}");
  785. }
  786. catch (Exception ex)
  787. {
  788. OnMessage("DBGet", ex.Message);
  789. }
  790. //finally
  791. //{
  792. // ReleaseLock();
  793. //}
  794. return null;
  795. }
  796. // ReSharper disable once InconsistentNaming
  797. public bool DBFill(ushort db, byte value)
  798. {
  799. try
  800. {
  801. var handle = new DataBlockInfoHandle();
  802. handle.BlockType = BlockType.DB;
  803. handle.DB = db;
  804. handle.Handle(_socket);
  805. if (handle.ResultCode == ResultCode.OK)
  806. {
  807. byte[] datas = new byte[handle.Response.Data.Mc7Len];
  808. for (int i = 0; i < handle.Response.Data.Mc7Len; i++)
  809. {
  810. datas[i] = value;
  811. }
  812. return WriteArea(AreaType.DB, handle.DB, 0, handle.Response.Data.Mc7Len, DataType.Byte, datas);
  813. }
  814. else
  815. OnMessage("DBFill", "RequestBlock:{handle.ResultCode}");
  816. return false;
  817. }
  818. catch (Exception ex)
  819. {
  820. OnMessage("DBFill", ex.Message);
  821. return false;
  822. }
  823. //finally
  824. //{
  825. // ReleaseLock();
  826. //}
  827. }
  828. public bool UpLoad(BlockType blockType, ushort db, bool isFull)
  829. {
  830. UploadStartHandle handle = new UploadStartHandle();
  831. handle.BlockType = blockType;
  832. handle.BlockNum = db;
  833. handle.Handle(_socket);
  834. if (handle.ResultCode != ResultCode.OK)
  835. return false;
  836. UploadHandle handle1 = new UploadHandle();
  837. handle1.UploadID = handle.Response.Params.UploadID;
  838. handle1.Handle(_socket);
  839. return false;
  840. }
  841. }
  842. }