本文记录源自:B站视频
实验结果:跟视频做下来是没有问题的。能运行。
自己补充做了视频中未实现的读取和写入数据部分【欢迎小伙伴指正不对的地方】
在练习完基本的C#和.NET使用后,做下了记录|C#连接PLC通讯
现在打算跟B站视频做一个简单的C#项目。这篇文章做这个记录。
实体类
EquipState类:
namespace thinger.ProjectDemo { /// /// 生产设备状态实体类 /// internal class EquipState { public bool Start { get; set; }//设备启动 public bool Stop { get; set; }//停止 public bool Task1 { get; set; }//任务1 public bool Task2 { get; set; } public bool Task3 { get; set; } public bool RunState { get; set; }//运行状态:1,正常 0 报警 public ushort RunModel { get; set; }//运行模式:1,普通 2,加速 3,满载 public float SiteData1 { get; set; }//位置数据1 public float SiteData2 { get; set; }//位置数据2 public float SiteData3 { get; set; }//位置数据3 public float SpeedData1 { get; set; }//速度数据1 public float SpeedData2 { get; set; }//速度数据2 } }
操作类
EquipControlMananger类:
namespace thinger.ProjectDemo.Manager { /// /// 设备操作类 /// internal class EquipControlManager { //先对Plc对象进行声明 private Plc s7NetPlc = null; /// /// 连接PLC /// /// /// /// public string ConnectPLC(string ipAddress, string cpuType) { try { //CpuType是个enum,枚举类型,因此,需要对string类型进行转换 CpuType currentCpuType = (CpuType)Enum.Parse(typeof(CpuType), cpuType, true); s7NetPlc = new Plc(currentCpuType, ipAddress, 0, 0); s7NetPlc.Open(); return "PLC连接成功"; } catch (Exception ex) { return $"PLC连接失败,{ex.Message}"; } } /// /// 关闭PLC /// public void ClosePLC() { s7NetPlc.Close(); } /// /// 按照指定地址,写入数据【位、字节、字、双字】 /// /// /// /// public string WriteDataToPLC(string varAddress, object varValue) { lock (this) { try { this.s7NetPlc.Write(varAddress, varValue); return "写入数据成功"; } catch (Exception ex) { return $"写入数据失败,{ex.Message}"; } } } } }
Main函数
namespace thinger.ProjectDemo { public partial class FrmMain : Form { private EquipControlManager controlManger=new EquipControlManager(); public FrmMain() { InitializeComponent(); } /// /// 打开和关闭PLC /// /// /// private void btnPLCConnect_Click(object sender, EventArgs e) { //Tag记录的是之前的开关状态,只有off和on //如果之前off,那么改为on if (this.btnPLCConnect.Tag.ToString().Equals("off")) { string connectManager = controlManger.ConnectPLC("192.168.0.10", this.cbbPLCType.Text); this.btnPLCConnect.Tag = "on"; this.btnPLCConnect.Image = Image.FromFile("images/plcOpen.png"); MessageBox.Show(connectManager); } else { controlManger.ClosePLC(); this.btnPLCConnect.Tag="off"; this.btnPLCConnect.Image = Image.FromFile("images/plcClose.png"); } } } }
Main函数
/// /// 操作设备 /// /// /// private void btnSwitch_Click(object sender, EventArgs e) { if (this.btnPLCConnect.Tag.ToString().Equals("off")) { MessageBox.Show("请先连接PLC", "操作提示"); return; } //获取Tag中的地址值 string[] tags = this.btnSwitch.Tag.ToString().Split('-'); if (tags[0].Equals("off")) { //打开冷却泵 this.pictureBox1.Image = Image.FromFile("images/电机.gif"); this.btnSwitch.Image = Image.FromFile("images/btnOn.png"); this.btnSwitch.Tag = $"on-{tags[1]}"; //修改PLC中的值 controlManger.WriteDataToPLC(tags[1], true); } else { this.pictureBox1.Image = Image.FromFile("images/电机.png"); this.btnSwitch.Image = Image.FromFile("images/btnOff.png"); this.btnSwitch.Tag = $"off-{tags[1]}"; //修改PLC中的值 controlManger.WriteDataToPLC(tags[1], false); } }
接着前面的Step5进行的
操作类
public string WriteDataToPLC(int db, string varAddress, object varValue) { int varAd = int.Parse(varAddress); float varVal = float.Parse(varValue.ToString()); lock (this) { try { s7NetPlc.Write(DataType.DataBlock, db, varAd,varVal); return "写入数据成功"; }catch(Exception ex) { return $"写入数据失败,{ex.Message}"; } } } public string ReadDataFromPLC(int db, string varAddress) { int varAd = int.Parse(varAddress); try { return s7NetPlc.Read(DataType.DataBlock, db, varAd, VarType.Real, 1).ToString(); }catch(Exception ex) { return $"写入数据失败,{ex.Message}"; } }
Main函数
private void writeDB0_Click(object sender, EventArgs e) { string writeMessage = controlManger.WriteDataToPLC(1,writeDB0.Tag.ToString(),textBox_writeDB0.Text); textBox_writeDB0.Text = ""; MessageBox.Show(writeMessage); } private void readDB0_Click(object sender, EventArgs e) { string readMessage = controlManger.ReadDataFromPLC(1,readDB0.Tag.ToString()); if (readMessage.Contains("失败")) { MessageBox.Show(readMessage); return; } textBox_readDB0.Text = readMessage; }
读取一个浮点:
写入一个浮点数