KvHostLink.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace LampInspectionMachine.KHLplc
  8. {
  9. public enum KHLSockType : byte
  10. {
  11. SOCK_TCP = 0,
  12. SOCK_UDP = 1,
  13. }
  14. // KV Mode
  15. public enum KHLMode : byte
  16. {
  17. MODE_RUN = 0x01,
  18. MODE_PROG = 0x02,
  19. }
  20. // KV Model
  21. public enum KHLModelCode : byte
  22. {
  23. KVX550 = 0x1B,
  24. KVX530 = 0x1A,
  25. KVX520 = 0x19,
  26. KVX310 = 0x18,
  27. KV8000 = 0x17,
  28. KV7500 = 0x16,
  29. KV7300 = 0x15,
  30. KVNC32 = 0x14,
  31. KVN60 = 0x13,
  32. KVN40 = 0x12,
  33. KVN24 = 0x11,
  34. KVN14 = 0x10,
  35. }
  36. // Device Type
  37. public enum KHLDevType : byte
  38. {
  39. DEV_R = 0x00,
  40. DEV_CR = 0x01,
  41. DEV_T = 0x02, // timer contract
  42. DEV_C = 0x03, // counter contract
  43. DEV_DM = 0x04,
  44. DEV_CM = 0x05,
  45. DEV_TM = 0x06,
  46. DEV_MR = 0x07,
  47. DEV_LR = 0x08,
  48. DEV_VB = 0x09,
  49. DEV_EM = 0x0A,
  50. DEV_FM = 0x0B,
  51. DEV_B = 0x0C,
  52. DEV_W = 0x0D,
  53. DEV_VM = 0x0E,
  54. DEV_TC = 0x0F, // timer current val
  55. DEV_TS = 0x10, // timer setting val
  56. DEV_CC = 0x11, // counter current val
  57. DEV_CS = 0x12, // counter setting val
  58. DEV_AT = 0x13, // digital trimer
  59. DEV_ZF = 0x14,
  60. DEV_UG = 0x15, // buffer memory
  61. DEV_Z = 0x16, // index register
  62. // XYM represent
  63. DEV_X = 0x17,
  64. DEV_Y = 0x18,
  65. DEV_M = 0x19,
  66. DEV_L = 0x1A,
  67. DEV_D = 0x1B,
  68. DEV_E = 0x1C,
  69. DEV_F = 0x1D,
  70. }
  71. // User Error
  72. public enum KHLUserError : long
  73. {
  74. KHL_ERR_NONE = 0, // Normal termination
  75. KHL_ERR_INVALID_SOCKET_TYPE = 1, // Invalid socket type
  76. KHL_ERR_INVALID_IP_ADDRESS = 2, // Invalid IP address
  77. KHL_ERR_CONNECT_TIMEOUT = 3, // Connection request timed out
  78. KHL_ERR_RECEIVED_SIZE_EXCEEDED_LIMIT = 4, // Received byte size exceeded the maximum value
  79. KHL_ERR_DISCONNECTED = 5, // Communication has been disconnected
  80. KHL_ERR_INVALID_DEVICE_TYPE = 6, // Invalid device type
  81. KHL_ERR_RELAY_DEVICE_TOP_NO = 7, // Abnormal relay device specified at the beginning
  82. KHL_ERR_INVALID_EXTENSION_UNIT_DEVTYPE = 8, // A device that does not exist in the extension unit was specified
  83. KHL_ERR_DWORD_DEVICE_ONLY_READ_AS_DWORD_SIZE = 9, // When reading a double-word device, the read size can only be in double-word device units
  84. KHL_ERR_RESOLVE_VARIABLE_FAILED = 10, // Variable name resolution failed
  85. KHL_ERR_RESOLVE_VARIABLE_TIMEOUT = 11, // Variable name resolution timed out
  86. KHL_ERR_RESOLVE_VARIABLE_BEYOND_ARRAY_SIZE = 12, // Variable name resolution is beyond the array size
  87. KHL_ERR_RESOLVE_VARIABLE_INVALID_DIMENSION = 13, // Variable name resolution has an abnormal array dimension
  88. KHL_ERR_ZERO_WORD_SIZE = 14, // 0 is specified for the word size
  89. KHL_ERR_FAIL_SOCK_INITIALIZE = 15, // Failed to initialize the socket
  90. KHL_ERR_INVALID_MODEL_CODE = 16, // Communication with an unsupported KV model
  91. KHL_ERR_CPU_NOT_SUPPORT_VARIABLE = 17, // The CPU model does not support variables
  92. KHL_ERR_DWORD_DEVICE_CANT_SET_BIT_OFFSET = 18, // Cannot read the bits in a word of a double-word device
  93. KHL_ERR_INVALID_MODE = 19, // Invalid mode setting
  94. KHL_ERR_COMMAND_TIMEOUT = 20, // Command reception request timed out
  95. KHL_ERR_NO_SOCKET = 21, // Socket is unconnected or disconnected
  96. KHL_ERR_BEYOND_DEVICE_NO = 22, // Attempted to read beyond the device number range
  97. KHL_ERR_RECV_FAILED = 23 // Failed to receive the command
  98. }
  99. // PLC Error
  100. public enum KHLPlcError : long
  101. {
  102. KHL_ERR_INVALID_DEV_NO = 1002, // Abnormal device number
  103. KHL_ERR_CANT_RUN = 1003 // No program or cannot switch to RUN mode because the RUN/PROG switch is set to PROG
  104. }
  105. public class KvHostLink
  106. {
  107. // Initialize
  108. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  109. public extern static int KHLInit();
  110. // Connect
  111. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  112. public extern static int KHLConnect(string ipAddr, ushort port, uint timeOutMs, KHLSockType sockType, ref int hSock);
  113. // Disconnect
  114. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  115. public extern static int KHLDisconnect(int hSock);
  116. // Confirm connection status
  117. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  118. public extern static int KHLIsConnected(int hSock);
  119. // Get PLC Model Code
  120. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  121. public extern static int KHLGetModelCode(int hSock, ref KHLModelCode modeCode);
  122. // Get PLC Mode
  123. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  124. public extern static int KHLGetMode(int hSock, ref KHLMode mode);
  125. // Change PLC Mode
  126. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  127. public extern static int KHLChangeMode(int hSock, KHLMode mode);
  128. // Get CPU Error Code
  129. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  130. public extern static int KHLGetErrorCode(int hSock, ref byte errCode, ref byte errLevel);
  131. // CPU Error Clear
  132. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  133. public extern static int KHLErrorClear(int hSock);
  134. // Read CPU Global Device
  135. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  136. public extern static int KHLReadDevicesAsWords(int hSock, KHLDevType devType, uint devTopNo, uint wordNum, byte[] data);
  137. // Read CPU Global Device
  138. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  139. public extern static int KHLReadDevicesAsBits(int hSock, KHLDevType devType, uint devTopNo, ushort bitOffset, uint bitNum, byte[] data);
  140. // Write CPU Global Device
  141. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  142. public extern static int KHLWriteDevicesAsWords(int hSock, KHLDevType devType, uint devTopNo, uint wordNum, byte[] data);
  143. // Write CPU Global Device
  144. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  145. public extern static int KHLWriteDevicesAsBits(int hSock, KHLDevType devType, uint devTopNo, ushort bitOffset, uint bitNum, byte[] data);
  146. // Read Extension Unit Device
  147. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  148. public extern static int KHLReadExtUnitDevicesAsWords(int hSock, byte unitNo, KHLDevType devType, uint devTopNo, uint wordNum, byte[] data);
  149. // Read Extension Unit Device
  150. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  151. public extern static int KHLReadExtUnitDevicesAsBits(int hSock, byte unitNo, KHLDevType devType, uint devTopNo, ushort bitOffset, uint bitNum, byte[] data);
  152. // Write Extension Unit Device
  153. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  154. public extern static int KHLWriteExtUnitDevicesAsWords(int hSock, byte unitNo, KHLDevType devType, uint devTopNo, uint wordNum, byte[] data);
  155. // Resolve Variable Name
  156. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  157. public extern static int KHLResolveVariableName(int hSock, string varName);
  158. // Read Variable
  159. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  160. public extern static int KHLReadVariable(int hSock, string varName, byte[] data, bool isChkCnv);
  161. // Write Variable
  162. [DllImport("KvHostLinkBase.dll", CharSet = CharSet.Unicode)]
  163. public extern static int KHLWriteVariable(int hSock, string varName, byte[] data, bool isChkCnv);
  164. }
  165. }