先在NuGet上搜索S7netplus,并安装,如下图所示
在博图中创建数据库DB2,如图所示,包含2个布尔量,一个int型变量,一个real型变量,地址分别为DB2.DBX0.0,DB2.DBX0.1,DB2.DBW2,DB2.DBD4
C#界面如下:
//具体代码如下:
using S7.Net; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 读写PLC数据 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Plc plc = new Plc(CpuType.S71500, "127.0.0.1", 0, 1); //定义新的PLC对象 private void Button1_Click(object sender, EventArgs e) { try { plc.Open();//连接PLC if (plc.IsConnected) { MessageBox.Show("连接成功!", "连接提示", MessageBoxButtons.OK); } else { MessageBox.Show("连接失败!", "连接提示", MessageBoxButtons.OK); } } catch (Exception Ex) { MessageBox.Show(Ex.ToString()); } } private void Btn_disconnect_Click(object sender, EventArgs e) { plc.Close();//断开PLC } #region 读PLC数据 private void Btn_read_Click(object sender, EventArgs e) { if (plc.IsConnected == false) { MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK);//检查PLC是否连接; } try { string[] arr = (txt_read_addr.Text.ToUpper()).Split('.');//将txt_read_addr文本框中的数据转为大写字母,并用“.”拆分后存放到arr数组中 string valuetype = arr[1].Substring(0, 3);//取数组中的第二个元素的前三位,用以确认读取的PLC数据类型 //西门子PLC数据类型:DBX(位,bool)DBB(字节,byte)DBW(字,word)DBD(双字,dword) //以下是按不同的数据类型,对PLC数据进行读取 if (valuetype == "DBX") { bool test1 = (bool)plc.Read(txt_read_addr.Text.ToUpper()); ShowMsg(txt_read_addr.Text + ":" + test1.ToString()); } if (valuetype == "DBW") { short test3 = ((ushort)plc.Read(txt_read_addr.Text.ToUpper())).ConvertToShort(); ShowMsg(txt_read_addr.Text + ":" + test3.ToString()); } if (valuetype == "DBD") { double test5 = ((uint)plc.Read(txt_read_addr.Text.ToUpper())).ConvertToFloat(); ShowMsg(txt_read_addr.Text + ":" + test5.ToString()); } if (valuetype != "DBX" & valuetype != "DBW" & valuetype != "DBD") { MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK); } } catch (Exception Ex) { MessageBox.Show(Ex.ToString()); } } #endregion 将数据写入PLC #region private void Btn_write_Click(object sender, EventArgs e) { if (plc.IsConnected==false) { MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK); } try { string[] arr = (txt_write_addr.Text.ToUpper()).Split('.'); string valuetype = arr[1].Substring(0, 3); if (valuetype == "DBX") { plc.Write(txt_write_addr.Text.ToUpper(), Convert.ToBoolean(txt_value.Text)); } if (valuetype == "DBW") { var value = short.Parse(txt_value.Text); plc.Write(txt_write_addr.Text.ToUpper(), value); } if (valuetype == "DBD") { double value = double.Parse(txt_value.Text); plc.Write(txt_write_addr.Text.ToUpper(), value); } if (valuetype != "DBX" & valuetype != "DBW" & valuetype != "DBD") { MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK); } } catch (Exception Ex) { MessageBox.Show(Ex.ToString()); } } #endregion private void Btn_clear_Click(object sender, EventArgs e) { txt_result.Text = null;//清空“结果”文本框 } private void ShowMsg(string v) { txt_result.AppendText(v + "rn");//将读取的PLC数据追加到“结果”文本框中 } } }
- 版权声明:文章来源于网络采集,版权归原创者所有,均已注明来源,如未注明可能来源未知,如有侵权请联系管理员删除。