• 西门子系列PLC与C#winform通信类


    1. public static class S
    2. {
    3. //PLC通信[连接PLC]
    4. public static Plc plc = null;
    5. public static Boolean Open()
    6. {
    7. //PLC类型 IP地址 机架号 槽号
    8. plc = new Plc(AC.CpuType, AC.Ip, 0, 1);
    9. try
    10. {
    11. plc.OpenAsync().Wait(100);
    12. }
    13. catch (Exception ex)
    14. {
    15. MessageBox.Show("PLC无法连接"+ex.ToString());
    16. }
    17. if (plc.IsConnected == false)
    18. {
    19. return false;
    20. }
    21. else
    22. {
    23. return true;
    24. }
    25. }
    26. //发送数据给PLC
    27. public static void T(string type, string address, string data)
    28. {
    29. address = address.Trim();
    30. data = data.Trim();
    31. type = type.Trim();
    32. if (string.IsNullOrEmpty(address))
    33. {
    34. MessageBox.Show("地址不能为空");
    35. }
    36. if (string.IsNullOrEmpty(type))
    37. {
    38. MessageBox.Show("类型不能为空");
    39. }
    40. if (string.IsNullOrEmpty(data))
    41. {
    42. //MessageBox.Show("数据不能为空");
    43. data = "0";
    44. }
    45. try
    46. {
    47. if (string.Compare(type, "real") == 0)
    48. {
    49. plc.Write(address, Convert.ToSingle(data));
    50. }
    51. else if (string.Compare(type, "int") == 0)
    52. {
    53. plc.Write(address, Convert.ToInt16(data));
    54. }
    55. else if (string.Compare(type, "bool") == 0)
    56. {
    57. //true: 1 false: 0
    58. if (Convert.ToBoolean(data))
    59. {
    60. plc.Write(address, Convert.ToInt16(1));
    61. }
    62. else
    63. {
    64. plc.Write(address, Convert.ToInt16(0));
    65. }
    66. }
    67. else if (string.Compare(type, "zbool") == 0)
    68. {
    69. //Boolean
    70. plc.Write(address, Convert.ToBoolean(data));
    71. }
    72. else if (string.Compare(type, "word") == 0)
    73. {
    74. //TODO:暂不支持
    75. MessageBox.Show("暂不支持");
    76. }
    77. else if (string.Compare(type, "dword") == 0)
    78. {
    79. //TODO:暂不支持
    80. MessageBox.Show("暂不支持");
    81. }
    82. else if (string.Compare(type, "dint") == 0)
    83. {
    84. plc.Write(address, Convert.ToInt32(data));
    85. }
    86. else if (string.Compare(type, "byte") == 0)
    87. {
    88. plc.Write(address, Convert.ToByte(data));
    89. }
    90. else
    91. {
    92. MessageBox.Show("按着流程来!");
    93. }
    94. }
    95. catch (Exception)
    96. {
    97. //MessageBox.Show("数据不能为空!");
    98. }
    99. }
    100. //接收来自PLC的数据
    101. public static string R(string type, string address)
    102. {
    103. // 读取PLC数据,并转为string类型,得以输出。
    104. string result = null;
    105. address = address.Trim();
    106. if (string.IsNullOrEmpty(address))
    107. {
    108. return "地址为空";
    109. }
    110. if (string.IsNullOrEmpty(type))
    111. {
    112. return "类型为空";
    113. }
    114. if (AC.isPlcConnected)
    115. {
    116. if (string.Compare(type, "int") == 0)
    117. {
    118. result = ((ushort)plc.Read(address)).ToString();
    119. }
    120. else if (string.Compare(type, "real") == 0)
    121. {
    122. // 保留一个小数位
    123. result = ((uint)plc.Read(address)).ConvertToFloat().ToString("0.0");
    124. }
    125. else if (string.Compare(type, "float") == 0)
    126. {
    127. // 保留一个小数位
    128. result = ((uint)plc.Read(address)).ConvertToFloat().ToString("0.00");
    129. }
    130. else if (string.Compare(type, "bool") == 0)
    131. {
    132. if (Convert.ToBoolean(plc.Read(address)))
    133. {
    134. return "true";
    135. }
    136. else
    137. {
    138. return "false";
    139. }
    140. }
    141. else if (string.Compare(type, "zbool") == 0)
    142. {
    143. result = Convert.ToBoolean(plc.Read(address)).ToString();
    144. }
    145. else if (string.Compare(type, "word") == 0)
    146. {
    147. result = ((ushort)plc.Read(address)).ToString();
    148. }
    149. else if (string.Compare(type, "dword") == 0)
    150. {
    151. result = ((uint)plc.Read(address)).ToString();
    152. }
    153. else if (string.Compare(type, "dint") == 0)
    154. {
    155. result = ((uint)plc.Read(address)).ToString();
    156. }
    157. else if (string.Compare(type, "byte") == 0)
    158. {
    159. //TODO:暂无写法
    160. }
    161. else
    162. {
    163. //未找到数据类型
    164. result = "-1";
    165. }
    166. }
    167. else
    168. {
    169. //plc未连接
    170. result = "-2";
    171. }
    172. return result;
    173. }
    174. public static void Close()
    175. {
    176. plc.Close();
    177. }
    178. }

    欢迎讨论,最近一版的西门子PLC通信类,包含PLC连接、发送数据、接收数据、关闭PLC连接四个函数,支出大多数类型,不定期更新,有需要评论。支持的PLC有s7-200、s7-300、s7-1200、s7-1500

  • 相关阅读:
    HFSS-API入门第一弹:画个Box
    Pycharm配置python3环境
    C++中的静态库与动态库
    vue响应式详解
    umich cv-5-1 神经网络训练1
    uniapp使用webview将页面转换成图片支持h5、app、小程序
    APP中RN页面热更新流程-ReactNative源码分析
    JavaScript实现经典消方块游戏
    Windows平台Unity下实现camera场景推送RTMP|轻量级RTSP服务|实时录像
    二叉树的最大深度
  • 原文地址:https://blog.csdn.net/qq_36683522/article/details/132914640