KHLCommunicate.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. namespace LampInspectionMachine.KHLplc
  12. {
  13. public class KHLCommunicate
  14. {
  15. byte[] readBuf = new byte[2048];
  16. byte[] writeBuf = new byte[2048];
  17. private bool isConnected=false;
  18. private int sock=0;
  19. public bool IsConnected { get => isConnected; set => isConnected = value; }
  20. public KHLCommunicate()
  21. {
  22. }
  23. public async Task ConnPlc()
  24. {
  25. while (true)
  26. {
  27. int err = 0;
  28. err = KHL.KHLInit();
  29. if (err != 0)
  30. {
  31. LogHelper.Info("plc连接初始化错误:" + err);
  32. IsConnected = false;
  33. Thread.Sleep(1000);
  34. continue;
  35. }
  36. err = KHL.KHLConnect("192.168.1.10", 8500, 3000, KHLSockType.SOCK_TCP, ref sock);
  37. if (err != 0)
  38. {
  39. LogHelper.Info("plc连接错误:" + err);
  40. IsConnected = false;
  41. Thread.Sleep(1000);
  42. continue;
  43. }
  44. IsConnected = true;
  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. int err = KHL.KHLReadDevicesAsBits(sock, KHLDevType.DEV_EM, topno, offset, bitnum, readBuf);
  55. if ( err != 0 )
  56. {
  57. LogHelper.Info("plc连接读取错误:" + err);
  58. throw new Exception("plc连接读取错误");
  59. }
  60. KHST.ByteToBool(ref rdBool, readBuf, rdBool.Length, 0, 0);
  61. return rdBool;
  62. }
  63. return null;
  64. }
  65. public bool WriteBool(uint topno, ushort offset, uint bitnum,bool[] wrBool)
  66. {
  67. if ( IsConnected )
  68. {
  69. bool[] rdBool = new bool[bitnum];
  70. KHST.BoolToByte(ref writeBuf, wrBool, rdBool.Length, 0, 0);
  71. int err = KHL.KHLWriteDevicesAsBits(sock, KHLDevType.DEV_ZF, topno, offset, bitnum, writeBuf);
  72. if ( err != 0 )
  73. {
  74. LogHelper.Info("plc连接读取错误:" + err);
  75. throw new Exception("plc连接读取错误");
  76. }
  77. return true;
  78. }
  79. return false;
  80. }
  81. public ushort[] ReadUshort(uint topno, uint wordnum)
  82. {
  83. ushort[] rdUshort = new ushort[wordnum];
  84. int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, wordnum, readBuf);
  85. if ( err != 0 )
  86. {
  87. LogHelper.Info("plc连接读取错误:" + err);
  88. throw new Exception("plc连接读取错误");
  89. }
  90. KHST.ByteToUshort(ref rdUshort, readBuf, rdUshort.Length, 0);
  91. return rdUshort;
  92. }
  93. public bool WriteUshort(uint topno, uint wordnum)
  94. {
  95. ushort[] rdUshort = new ushort[wordnum];
  96. KHST.UshortToByte(ref writeBuf, rdUshort, rdUshort.Length, 0);
  97. int err = KHL.KHLWriteDevicesAsWords(sock, KHLDevType.DEV_ZF, topno, wordnum, writeBuf);
  98. if ( err != 0 )
  99. {
  100. LogHelper.Info("plc连接读取错误:" + err);
  101. throw new Exception("plc连接读取错误");
  102. }
  103. return true;
  104. }
  105. public int ReadInt(uint topno)
  106. {
  107. int[] rdint = new int[1];
  108. int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, 1, readBuf);
  109. if (err != 0)
  110. {
  111. LogHelper.Info("plc连接读取错误:" + err);
  112. throw new Exception("plc连接读取错误");
  113. }
  114. KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0);
  115. return rdint[0];
  116. }
  117. public int[] ReadInt(uint topno, uint wordnum)
  118. {
  119. int[] rdint = new int[wordnum];
  120. int err = KHL.KHLReadDevicesAsWords(sock, KHLDevType.DEV_EM, topno, wordnum, readBuf);
  121. if ( err != 0 )
  122. {
  123. LogHelper.Info("plc连接读取错误:" + err);
  124. throw new Exception("plc连接读取错误");
  125. }
  126. KHST.ByteToInt(ref rdint, readBuf, rdint.Length, 0);
  127. return rdint;
  128. }
  129. public bool WriteInt(uint topno, uint wordnum, int[] rdInt)
  130. {
  131. KHST.IntToByte(ref writeBuf, rdInt, rdInt.Length, 0);
  132. int err = KHL.KHLWriteDevicesAsWords(sock, KHLDevType.DEV_ZF, topno, wordnum, writeBuf);
  133. if ( err != 0 )
  134. {
  135. LogHelper.Info("plc连接读取错误:" + err);
  136. throw new Exception("plc连接读取错误");
  137. }
  138. return true;
  139. }
  140. }
  141. }