|
|
|
當前的位置 >> 返回首頁 >> 解決方案 |
|
西安軟件外包讀寫PLC數(shù)據(jù) |
|
發(fā)布者:西安軟件公司 發(fā)布時間:2022/8/19 閱讀:次 |
|
西安軟件外包采用讀寫PLC數(shù)據(jù) 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數(shù)據(jù) { 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數(shù)據(jù) 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文本框中的數(shù)據(jù)轉(zhuǎn)為大寫字母,并用“.”拆分后存放到arr數(shù)組中 string valuetype = arr[1].Substring(0, 3);//取數(shù)組中的第二個元素的前三位,用以確認讀取的PLC數(shù)據(jù)類型 //PLC數(shù)據(jù)類型:DBX(位,bool)DBB(字節(jié),byte)DBW(字,word)DBD(雙字,dword) //以下是按不同的數(shù)據(jù)類型,對PLC數(shù)據(jù)進行讀取 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 將數(shù)據(jù)寫入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;//清空“結(jié)果”文本框 } private void ShowMsg(string v) { txt_result.AppendText(v + "\r\n");//將讀取的PLC數(shù)據(jù)追加到“結(jié)果”文本框中 } private void Txt_result_TextChanged(object sender, EventArgs e) { } } }
|
|