SimenssPlc.cs 28 KB

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