【C#】上位机与西门子200SMART通讯测试
using System.Windows.Forms;
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 S7.Net;
namespace HZCsharpProject01
{
public partial class FormCommucation : Form
{
public FormCommucation()
{
InitializeComponent();
}
Plc plc;//声明plc连接对象
private void btnConnect_Click(object sender, EventArgs e)
{
string TypeName = txtPLCType.Text;//PLC型号名称
string Ip = txtIp.Text;//plcIp地址
switch (TypeName)
{
case "200":
case "1200":
/*S7.Net.dll类库中没有S7-200Smart类型,想跟S7-200Smart通讯选择S71200即可*/ 最新S7.NET支持smart200了
plc = new Plc(CpuType.S71200, Ip, 0, 1);//创建plc实例
break;
case "300":
plc = new Plc(CpuType.S7300, Ip, 0, 1);//创建plc实例
break;
case "400":
plc = new Plc(CpuType.S7400, Ip, 0, 1);//创建plc实例
break;
case "1500":
plc = new Plc(CpuType.S71500, Ip, 0, 1);//创建plc实例
break;
}
plc.Open();//连接plc
if (plc.IsConnected)//检查plc是否连接上
{
MessageBox.Show("与PLC连接成功!");
//连接成功
}
else
{
MessageBox.Show("与PLC连接失败!");
//连接失败
}
}
private void btnWrite_Click(object sender, EventArgs e)
{
//string address = txtAddress.Text;//寄存地址
//string writedata = txtWriteData.Text;//写入数据
//plc.Write(address, writedata);
if (plc.IsConnected == false)
{
MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK);
}
else
{
try
{
string[] arr = (txtAddress .Text.ToUpper()).Split('.');
string valuetype = arr[1].Substring(0, 3);
if (valuetype == "DBX")
{
plc.Write(txtAddress .Text.ToUpper(), Convert.ToBoolean(txtWriteData .Text));
}
else if (valuetype == "DBW")
{
var value = short.Parse(txtWriteData .Text);
plc.Write(txtAddress .Text.ToUpper(), value);
}
else if (valuetype == "DBD")
{
float value = float .Parse(txtWriteData.Text);
//double value = double.Parse(txtWriteData.Text);
plc.Write(txtAddress.Text.ToUpper(), value);
}
else
{
MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK);
}
}
catch (Exception Ex)
{
MessageBox.Show("请检查输入的“地址”或“值”是否错误!"+Ex, "输入提示", MessageBoxButtons.OK);
}
}
}
private void btnReadSingleData_Click(object sender, EventArgs e)
{
//string address = txtAddress2.Text;//寄存地址
//var bytes = plc.Read(address);
//txtReadSingleData.Text = bytes.ToString();
if (plc.IsConnected == false)
{
MessageBox.Show("未连接PLC!", "连接提示", MessageBoxButtons.OK);//检查PLC是否连接;
}
else
{
try
{
string[] arr = (txtAddress2 .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(txtAddress2 .Text.ToUpper());
txtReadSingleData.Text=txtAddress2.Text + ":" + test1.ToString();
}
else if (valuetype == "DBW")
{
short test3 = ((ushort)plc.Read(txtAddress2 .Text.ToUpper())).ConvertToShort();
txtReadSingleData.Text = txtAddress2 .Text + ":" + test3.ToString();
}
else if (valuetype == "DBD")
{
double test5 = ((uint)plc.Read(txtAddress2 .Text.ToUpper())).ConvertToFloat();
txtReadSingleData.Text = txtAddress2 .Text + ":" + test5.ToString();
}
else
{
MessageBox.Show("请检查地址是否输入错误!", "输入提示", MessageBoxButtons.OK);
}
}
catch (Exception Ex)
{
MessageBox.Show("请检查地址是否输入错误!"+Ex , "输入提示", MessageBoxButtons.OK);
}
}
}
private void btnDisConnect_Click(object sender, EventArgs e)
{
plc.Close();//断开PLC
}
}
}
我用的是VS2015版本,框架是4.6。NUGET下载S7.DLL时要下载以前的版本,最新版本框架都是4.7以上了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构