KHLCommunicate.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using KHL = LampInspectionMachine.KHLplc.KvHostLink;
  6. using KHST = LampInspectionMachine.KHLplc.KvStruct;
  7. using System.Threading.Tasks;
  8. using LampInspectionMachine.Log4xml;
  9. using System.Windows.Controls;
  10. using System.Threading;
  11. using LampInspectionMachine.Model;
  12. namespace LampInspectionMachine.KHLplc
  13. {
  14. public class KHLCommunicate
  15. {
  16. private bool isConnected = false;
  17. private int sock = 0;
  18. public bool IsConnected { get => isConnected; set => isConnected = value; }
  19. public KHLCommunicate()
  20. {
  21. }
  22. public async Task ConnPlc()
  23. {
  24. while (true)
  25. {
  26. int err = 0;
  27. err = KHL.KHLInit();
  28. if (err != 0)
  29. {
  30. LogHelper.Info("plc连接初始化错误:" + err);
  31. IsConnected = false;
  32. Thread.Sleep(1000);
  33. continue;
  34. }
  35. err = KHL.KHLConnect("192.168.1.10", 8500, 3000, KHLSockType.SOCK_TCP, ref sock);
  36. if (err != 0)
  37. {
  38. LogHelper.Info("plc连接错误:" + err);
  39. IsConnected = false;
  40. Thread.Sleep(1000);
  41. continue;
  42. }
  43. IsConnected = true;
  44. ThreadPool.SetMinThreads(500,500);
  45. LogHelper.Info("plc连接成功");
  46. break;
  47. }
  48. }
  49. public bool[] ReadBool(uint topno, ushort offset, uint bitnum)
  50. {
  51. if (IsConnected)
  52. {
  53. bool[] rdBool = new bool[bitnum];
  54. byte[] readBuf = new byte[2048];
  55. int err = KHL.KHLReadDevicesAsBits(sock, KHLDevType.DEV_EM, topno, offset, bitnum, readBuf);
  56. if (err != 0)
  57. {
  58. IsConnected = false;
  59. LogHelper.Info("plc连接读取错误:" + err);
  60. throw new Exception("plc连接读取错误");
  61. }
  62. KHST.ByteToBool(ref rdBool, readBuf, rdBool.Length, 0, 0);
  63. return rdBool;
  64. }
  65. return null;
  66. }
  67. public bool WriteBool(uint topno, ushort offset, uint bitnum, bool[] wrBool)
  68. {
  69. if (IsConnected)
  70. {
  71. byte[] readBuf = new byte[2048];
  72. byte[] writeBuf = new byte[2048];
  73. bool[] rdBool = new bool[bitnum];
  74. KHST.BoolToByte(ref writeBuf, wrBool, rdBool.Length, 0, 0);
  75. int err = KHL.KHLWriteDevicesAsBits(sock, KHLDevType.DEV_ZF, topno, offset, bitnum, writeBuf);
  76. if (err != 0)
  77. {
  78. IsConnected = false;
  79. LogHelper.Info("plc连接读取错误:" + err);
  80. throw new Exception("plc连接读取错误");
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. public ushort[] ReadUshort(uint topno, uint wordnum)
  87. {
  88. byte[] readBuf = new byte[2048];
  89. byte[] writeBuf = new byte[2048];
  90. ushort[] rdUshort = new ushort[wordnum];
  91. int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_DM, topno, wordnum, readBuf);
  92. if (err != 0)
  93. {
  94. IsConnected = false;
  95. LogHelper.Info("plc连接读取错误:" + err);
  96. throw new Exception("plc连接读取错误");
  97. }
  98. KHST.ByteToUshort(ref rdUshort, readBuf, rdUshort.Length, 0);
  99. if (topno == 200&& rdUshort[0]!=1)
  100. {
  101. int a = 0;
  102. }
  103. return rdUshort;
  104. }
  105. public bool WriteUshort(uint topno, uint wordnum, ushort[] rdUshort)
  106. {
  107. byte[] readBuf = new byte[2048];
  108. byte[] writeBuf = new byte[2048];
  109. KHST.UshortToByte(ref writeBuf, rdUshort, rdUshort.Length, 0);
  110. int err = KHL.KHLWriteDevicesAsWords(sock, KHLDevType.DEV_DM, topno, wordnum, writeBuf);
  111. if (err != 0)
  112. {
  113. IsConnected = false;
  114. LogHelper.Info("plc连接读取错误:" + err);
  115. throw new Exception("plc连接读取错误");
  116. }
  117. return true;
  118. }
  119. public int ReadInt(uint topno)
  120. {
  121. byte[] readBuf = new byte[2048];
  122. byte[] writeBuf = new byte[2048];
  123. int[] rdint = new int[1];
  124. int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, 1, readBuf);
  125. if (err != 0)
  126. {
  127. IsConnected = false;
  128. LogHelper.Info("plc连接读取错误:" + err);
  129. throw new Exception("plc连接读取错误");
  130. }
  131. KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0);
  132. return rdint[0];
  133. }
  134. public int[] ReadInt(uint topno, uint wordnum)
  135. {
  136. byte[] readBuf = new byte[2048];
  137. byte[] writeBuf = new byte[2048];
  138. int[] rdint = new int[wordnum];
  139. int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, wordnum, readBuf);
  140. if (err != 0)
  141. {
  142. IsConnected = false;
  143. LogHelper.Info("plc连接读取错误:" + err);
  144. throw new Exception("plc连接读取错误");
  145. }
  146. KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0);
  147. return rdint;
  148. }
  149. public bool WriteInt(uint topno, uint wordnum, int[] rdInt)
  150. {
  151. byte[] readBuf = new byte[2048];
  152. byte[] writeBuf = new byte[2048];
  153. KHST.IntToByte(ref writeBuf, rdInt, rdInt.Length, 0);
  154. int err = KHL.KHLWriteDevicesAsWords(sock, KHLDevType.DEV_ZF, topno, wordnum, writeBuf);
  155. if (err != 0)
  156. {
  157. IsConnected = false;
  158. LogHelper.Info("plc连接读取错误:" + err);
  159. throw new Exception("plc连接读取错误");
  160. }
  161. return true;
  162. }
  163. }
  164. }