> 因订阅号粉丝的要求,需要设计一个路灯时控程序,先抛砖引玉,用西门子S7-200 smart结合C#平台来演示。 首先我们需要获得CPU的时钟: 利用READ_RTC来获得,需要初始化PLC的时钟 连接PLC后,读取PC时钟,然后点击设置。
此时我们可以监控年月日时分秒
在设置开关灯时间,
C#代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using S7.Net; namespace S7_200Smart路灯控制 { public partial class Form1 : Form { public bool lamp1; public bool lamp2; Plc plc;//声明plc连接对象 public string date_time; public Form1() { InitializeComponent(); } private void Open_PLC_Click(object sender, EventArgs e) { string Ip = txtIp.Text;//plcIp地址 plc = new Plc(CpuType.S71200, Ip, 0, 1);//创建plc实例 plc.Open();//连接plc if (plc.IsConnected)//检查plc是否连接上 { MessageBox.Show("与PLC连接成功!"); timer1.Enabled = true; this.Read_set_data.PerformClick(); } else { MessageBox.Show("与PLC连接失败!"); } } private void Close_PLC_Click(object sender, EventArgs e) { plc.Close();//断开PLC }
private void timer1_Tick(object sender, EventArgs e) { date_time = DateTime.Now.ToString();
if(lamp1) { p2.Visible = true; p1.Visible = false; } if (lamp2) } p1.Visible = true; p2.Visible = false; } if (plc.IsConnected)//检查plc是否连接上 { this.Text = "路灯控制V1.0--PLC通讯已连接" + " " + date_time + " Designed by Honeytree"; } if (plc.IsConnected == false) { this.Text = "路灯控制V1.0--PLC通讯未连接" + " " + date_time + " Designed by Honeytree"; } if (plc.IsConnected == false) { // MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK); } else { try { string y1; string m1; string d1; string h1; string min1; string sec1; //年 int intValue = ((byte)plc.Read("DB1.DBB0")); string hexValue = intValue.ToString("X"); y1 = hexValue; //月 int intValue1 = ((byte)plc.Read("DB1.DBB1")); string hexValue1 = intValue1.ToString("X"); m1 = hexValue1; //日 int intValue2 = ((byte)plc.Read("DB1.DBB2")); string hexValue2 = intValue2.ToString("X"); d1 = hexValue2; //时 int intValue5 = ((byte)plc.Read("DB1.DBB3")); string hexValue5 = intValue5.ToString("X"); h1 = hexValue5; byte v1 = byte.Parse(h1); plc.Write("DB1.DBB200", v1); //分 int intValue3 = ((byte)plc.Read("DB1.DBB4")); string hexValue3 = intValue3.ToString("X"); min1 = hexValue3; byte v2 = byte.Parse(min1); plc.Write("DB1.DBB201", v2); //秒 int intValue4 = ((byte)plc.Read("DB1.DBB5")); string hexValue4 = intValue4.ToString("X"); sec1 = hexValue4; PLC_date.Text = "20" + y1 + "年" + m1+"月" + d1+"日" + h1 + "时" + min1 + "分" + sec1 + "秒"; //灯控 lamp1= ((bool)plc.Read("M0.0"));
lamp2 = ((bool)plc.Read("M0.1")); } catch (Exception Ex) { MessageBox.Show("请检查输入的“地址”或“值”是否错误!" + Ex, "输入提示", MessageBoxButtons.OK); } } }
private void Set_ACK_Click(object sender, EventArgs e) { if (plc.IsConnected == false) { MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK); } else { try { byte value = byte.Parse(start_hour.Text); plc.Write("DB1.DBB20", value); byte value1 = byte.Parse(stop_hour.Text); plc.Write("DB1.DBB22", value1); } catch (Exception Ex) { MessageBox.Show("请检查输入的“地址”或“值”是否错误!" + Ex, "输入提示", MessageBoxButtons.OK); } } } private void Read_set_data_Click(object sender, EventArgs e) { int hour1 = ((byte)plc.Read("DB1.DBB20")); start_hour.Text = hour1.ToString(); int min1 = ((byte)plc.Read("DB1.DBB22")); stop_hour.Text = min1.ToString(); } } } 测试效果:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |