KHLCommunicate.cs 4.8 KB

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