[西门子PLC] 西门子PLC实现开关灯时间,C#设计测试系统
因订阅号粉丝的要求,需要设计一个路灯时控程序,先抛砖引玉,用西门子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();
}
}
}
测试效果:
西门子PLC实现开关灯时间,C#设计测试系统
http://bbs.plcjs.com/forum.php?mod=viewthread&tid=502495&fromuid=17
(出处: PLC论坛-全力打造可编程控制器专业技术论坛)
相关文章
- Linux服务器硬件信息查询与日常运维命令总结
- Linux服务器带宽跑不满?用ethtool调优网卡参数,性能提升30%
- 如何在 Rocky Linux 中查看网卡流量?跟着小编学习iftop安装和使用
- Linux查看网卡速率_linux查看网卡当前速率
- 五一我要看七天小说!免费开源的轻量化书库talebook搭建流程。
- 我是如何用这3个小工具,助力小姐姐提升100%开发效率的
- html5和css3的常用参考网_基于html5和css3的网页制作
- 超详细的网络抓包神器 tcpdump 使用指南
- Vue 技术栈(全家桶)_vue全栈项目教程
- 学习ES6- 入门Vue(大量源代码及笔记,带你起飞)