PLCCom.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using OmronFinsTCP.Net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PLCTool.Common
  10. {
  11. public class PLCCom
  12. {
  13. private static string plcIp = ConfigurationManager.AppSettings["PLCIp"];
  14. private static string plcPort = ConfigurationManager.AppSettings["PLCPort"];
  15. private EtherNetPLC ENT;//plc连接
  16. private static PLCCom instance;
  17. private bool isConnect = false;
  18. private PLCCom()
  19. {
  20. ConnectPLC();
  21. }
  22. public static PLCCom GetInstance()
  23. {
  24. if (instance == null)
  25. {
  26. instance = new PLCCom();
  27. }
  28. return instance;
  29. }
  30. private bool ConnectPLC()
  31. {
  32. ENT = new EtherNetPLC();
  33. short re = ENT.Link(plcIp, short.Parse(plcPort), 500);
  34. if (re == 0)
  35. {
  36. //_logger.Info("PLC连接成功!");
  37. return true;
  38. }
  39. else
  40. {
  41. //_logger.Info("PLC连接失败!");
  42. return false;
  43. }
  44. }
  45. public string ReadPlcObject(string address, VarType valueType)
  46. {
  47. string value = string.Empty; ;
  48. short rb;
  49. short reSuc = -1;
  50. if (isConnect== false)
  51. {
  52. return string.Empty;
  53. }
  54. switch (valueType)
  55. {
  56. case VarType.Bit:
  57. reSuc = ENT.GetBitState(PlcMemory.DM, address, out rb);
  58. //读取成功
  59. if (reSuc == 0)
  60. {
  61. value = rb.ToString();
  62. }
  63. break;
  64. case VarType.Byte:
  65. break;
  66. case VarType.Word:
  67. reSuc = ENT.ReadWord(PlcMemory.DM, short.Parse(address), out rb);
  68. //读取成功
  69. if(reSuc==0)
  70. {
  71. value = rb.ToString();
  72. }
  73. break;
  74. case VarType.DWord:
  75. break;
  76. case VarType.Int:
  77. break;
  78. case VarType.DInt:
  79. break;
  80. case VarType.Real:
  81. break;
  82. default:
  83. return null;
  84. }
  85. return value;
  86. }
  87. public bool WritePlcObject(string address, VarType valueType,string writeValue)
  88. {
  89. bool isSuccess=false;
  90. short reSuc = -1;
  91. if (isConnect == false)
  92. {
  93. return false;
  94. }
  95. switch (valueType)
  96. {
  97. case VarType.Bit:
  98. BitState bit= BitState.OFF;
  99. if(writeValue.Trim()=="1")
  100. {
  101. bit = BitState.ON;
  102. }
  103. else
  104. {
  105. if (writeValue.Trim() == "0")
  106. {
  107. bit = BitState.OFF;
  108. }
  109. }
  110. reSuc = ENT.SetBitState(PlcMemory.DM, address,bit);
  111. //写成功
  112. if (reSuc == 0)
  113. {
  114. isSuccess = true;
  115. }
  116. break;
  117. case VarType.Byte:
  118. break;
  119. case VarType.Word:
  120. short wValue = Convert.ToInt16(writeValue);
  121. reSuc = ENT.WriteWord(PlcMemory.DM, short.Parse(address), wValue);
  122. //写成功
  123. if (reSuc == 0)
  124. {
  125. isSuccess = true;
  126. }
  127. break;
  128. case VarType.DWord:
  129. break;
  130. case VarType.Int:
  131. break;
  132. case VarType.DInt:
  133. break;
  134. case VarType.Real:
  135. break;
  136. }
  137. return isSuccess;
  138. }
  139. private static object GetVariableValue(VarType varType, object var)
  140. {
  141. switch (varType)
  142. {
  143. case VarType.Bit:
  144. return (bool)var;
  145. case VarType.Byte:
  146. return (byte)var;
  147. case VarType.Word:
  148. return (ushort)var;
  149. case VarType.DWord:
  150. return (uint)var;
  151. case VarType.Int:
  152. return (short)var;
  153. case VarType.DInt:
  154. return (int)var;
  155. case VarType.Real:
  156. return (float)var;
  157. default:
  158. return null;
  159. }
  160. }
  161. }
  162. public enum VarType
  163. {
  164. /// <summary>
  165. /// Bit variable type (bool)
  166. /// </summary>
  167. Bit,
  168. /// <summary>
  169. /// Byte variable type (8 bits)
  170. /// </summary>
  171. Byte,
  172. /// <summary>
  173. /// Word variable type (16 bits, 2 bytes)
  174. /// </summary>
  175. Word,
  176. /// <summary>
  177. ///
  178. /// </summary>
  179. DWord,
  180. /// <summary>
  181. /// Int variable type (16 bits, 2 bytes)
  182. /// </summary>
  183. Int,
  184. /// <summary>
  185. /// DInt variable type (32 bits, 4 bytes)
  186. /// </summary>
  187. DInt,
  188. /// <summary>
  189. /// Real variable type (32 bits, 4 bytes)
  190. /// </summary>
  191. Real,
  192. /// <summary>
  193. /// Char Array / C-String variable type (variable)
  194. /// </summary>
  195. String,
  196. }
  197. }