程序员开发实例大全宝库

网站首页 > 编程文章 正文

[西门子PLC] C#通过modbus tcp与S7-200 smart通信测试

zazugpt 2025-05-02 14:43:12 编程文章 10 ℃ 0 评论

一、编写S7-200 Smart的MODBUS TCP SERVER程序段

首先调用库文件

其中M0.0为使能服务器通讯,IP_Port为端口,最大输入输出为256,最大模拟量为56,寄存器100个(对应地址:40001~40099),PLC寄存器从VB0开始。

二、编写C#程序

通过NUget添加驱动EasyModbusTCP

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;

using EasyModbus;//调用驱动

//创建对象

ModbusClient modbusClient = new ModbusClient();

private void 连接_Click(object sender, EventArgs e)

{

modbusClient.IPAddress = txb_IP.Text; // 服务器IP地址

modbusClient.Port = int.Parse(txb_port.Text); // 端口号

modbusClient.Connect(); // 建立连接

if (modbusClient.Connected)

{

MessageBox.Show("与PLC连接成功!");

int[] registers =
modbusClient.ReadHoldingRegisters(0, 100); // 从地址0开始读取100个保持寄存器的数据

D_40099.Text = registers[99].ToString();//读取第100个数据

updatetimer.Enabled = true;

连接.ForeColor = Color.Green;

断开连接.ForeColor = Color.Gray;

}

else

{

MessageBox.Show("与PLC连接失败!");

}

}

测试效果:


C#通过modbus tcp与S7-200 smart通信测试

http://bbs.plcjs.com/forum.php?mod=viewthread&tid=502499&fromuid=17

(出处: PLC论坛-全力打造可编程控制器专业技术论坛)

#非标自动化#

#西门子PLC#

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表