|
|
|
当前的位置 >> 返回首页 >> 解决方案 |
|
西安软件外包读写PLC数据 |
|
发布者:西安软件公司 发布时间:2022/8/19 阅读:次 |
|
西安软件外包采用读写PLC数据 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.S71200, "127.0.0.1", 0, 1); //定义新的PLC对象 private void Button1_Click(object sender, EventArgs e) { plc.Open();//连接PLC if (plc.IsConnected) { MessageBox.Show("连接成功!", "连接提示", MessageBoxButtons.OK); } else { MessageBox.Show("连接失败!", "连接提示", MessageBoxButtons.OK); } } 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是否连接; } else { 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()); } else if (valuetype == "DBW") { short test3 = ((ushort)plc.Read(txt_read_addr.Text.ToUpper())).ConvertToShort(); ShowMsg(txt_read_addr.Text + ":" + test3.ToString()); } else if (valuetype == "DBD") { double test5 = ((uint)plc.Read(txt_read_addr.Text.ToUpper())).ConvertToFloat(); ShowMsg(txt_read_addr.Text + ":" + test5.ToString()); } else { MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK); } } catch (Exception Ex) { MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK); } } } #endregion
#region 将数据写入PLC private void Btn_write_Click(object sender, EventArgs e) { if (plc.IsConnected == false) { MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK); } else { 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)); } else if (valuetype == "DBW") { var value = short.Parse(txt_value.Text); plc.Write(txt_write_addr.Text.ToUpper(), value); } else if (valuetype == "DBD") { double value = double.Parse(txt_value.Text); plc.Write(txt_write_addr.Text.ToUpper(), value); } else { MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK); } } catch (Exception Ex) { MessageBox.Show("请检查输入的“地址”或“值”是否错误!", "输入提示", MessageBoxButtons.OK); } }
} #endregion private void Btn_clear_Click(object sender, EventArgs e) { txt_result.Text = null;//清空“结果”文本框 } private void ShowMsg(string v) { txt_result.AppendText(v + "\r\n");//将读取的PLC数据追加到“结果”文本框中 } private void Txt_result_TextChanged(object sender, EventArgs e) { } } }
|
|