- public static class S
- {
- //PLC通信[连接PLC]
- public static Plc plc = null;
- public static Boolean Open()
- {
- //PLC类型 IP地址 机架号 槽号
- plc = new Plc(AC.CpuType, AC.Ip, 0, 1);
-
- try
- {
- plc.OpenAsync().Wait(100);
- }
- catch (Exception ex)
- {
-
- MessageBox.Show("PLC无法连接"+ex.ToString());
- }
-
- if (plc.IsConnected == false)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
-
- //发送数据给PLC
- public static void T(string type, string address, string data)
- {
- address = address.Trim();
- data = data.Trim();
- type = type.Trim();
-
- if (string.IsNullOrEmpty(address))
- {
- MessageBox.Show("地址不能为空");
- }
- if (string.IsNullOrEmpty(type))
- {
- MessageBox.Show("类型不能为空");
- }
- if (string.IsNullOrEmpty(data))
- {
- //MessageBox.Show("数据不能为空");
- data = "0";
- }
-
- try
- {
- if (string.Compare(type, "real") == 0)
- {
- plc.Write(address, Convert.ToSingle(data));
- }
- else if (string.Compare(type, "int") == 0)
- {
- plc.Write(address, Convert.ToInt16(data));
- }
- else if (string.Compare(type, "bool") == 0)
- {
- //true: 1 false: 0
- if (Convert.ToBoolean(data))
- {
- plc.Write(address, Convert.ToInt16(1));
- }
- else
- {
- plc.Write(address, Convert.ToInt16(0));
- }
- }
- else if (string.Compare(type, "zbool") == 0)
- {
- //Boolean
- plc.Write(address, Convert.ToBoolean(data));
- }
- else if (string.Compare(type, "word") == 0)
- {
- //TODO:暂不支持
- MessageBox.Show("暂不支持");
- }
- else if (string.Compare(type, "dword") == 0)
- {
- //TODO:暂不支持
- MessageBox.Show("暂不支持");
- }
- else if (string.Compare(type, "dint") == 0)
- {
- plc.Write(address, Convert.ToInt32(data));
- }
- else if (string.Compare(type, "byte") == 0)
- {
- plc.Write(address, Convert.ToByte(data));
- }
- else
- {
- MessageBox.Show("按着流程来!");
- }
- }
- catch (Exception)
- {
-
- //MessageBox.Show("数据不能为空!");
- }
- }
-
- //接收来自PLC的数据
- public static string R(string type, string address)
- {
- // 读取PLC数据,并转为string类型,得以输出。
- string result = null;
- address = address.Trim();
-
- if (string.IsNullOrEmpty(address))
- {
- return "地址为空";
- }
- if (string.IsNullOrEmpty(type))
- {
- return "类型为空";
- }
-
- if (AC.isPlcConnected)
- {
- if (string.Compare(type, "int") == 0)
- {
- result = ((ushort)plc.Read(address)).ToString();
- }
- else if (string.Compare(type, "real") == 0)
- {
- // 保留一个小数位
- result = ((uint)plc.Read(address)).ConvertToFloat().ToString("0.0");
- }
- else if (string.Compare(type, "float") == 0)
- {
- // 保留一个小数位
- result = ((uint)plc.Read(address)).ConvertToFloat().ToString("0.00");
- }
- else if (string.Compare(type, "bool") == 0)
- {
- if (Convert.ToBoolean(plc.Read(address)))
- {
- return "true";
- }
- else
- {
- return "false";
- }
- }
- else if (string.Compare(type, "zbool") == 0)
- {
- result = Convert.ToBoolean(plc.Read(address)).ToString();
-
- }
- else if (string.Compare(type, "word") == 0)
- {
- result = ((ushort)plc.Read(address)).ToString();
- }
- else if (string.Compare(type, "dword") == 0)
- {
- result = ((uint)plc.Read(address)).ToString();
- }
- else if (string.Compare(type, "dint") == 0)
- {
- result = ((uint)plc.Read(address)).ToString();
- }
- else if (string.Compare(type, "byte") == 0)
- {
- //TODO:暂无写法
- }
- else
- {
- //未找到数据类型
- result = "-1";
- }
- }
- else
- {
- //plc未连接
- result = "-2";
- }
- return result;
- }
-
- public static void Close()
- {
- plc.Close();
- }
- }
欢迎讨论,最近一版的西门子PLC通信类,包含PLC连接、发送数据、接收数据、关闭PLC连接四个函数,支出大多数类型,不定期更新,有需要评论。支持的PLC有s7-200、s7-300、s7-1200、s7-1500