C# MODBUSTCP通讯汇川PLC示例

  • 使用MODBUS协议读取PLC数据的示例代码:

  •  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; namespace PLC_Communication { class Program { static void Main(string[] args) { //创建一个TCP客户端 TcpClient client = new TcpClient(); //连接PLC client.Connect("192.168.0.1", 502); //确认连接状态 if (client.Connected) { Console.WriteLine("PLC连接成功!"); //读取PLC数据 byte[] buffer = new byte[12] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x64, 0x00, 0x01 }; NetworkStream stream = client.GetStream(); stream.Write(buffer, 0, buffer.Length); byte[] response = new byte[1024]; int count = stream.Read(response, 0, response.Length); for (int i = 0; i < count; i++) { Console.Write(response[i].ToString("X2") + " "); } } else { Console.WriteLine("PLC连接失败!"); } //关闭连接 client.Close(); } } } ``` 其中,`buffer`数组是MODBUS读取数据的命令,`0x00, 0x01`表示PLC的站号,`0x03`表示读取Holding Register,`0x00, 0x64`表示读取起始地址为100的寄存器,`0x00, 0x01`表示读取一个寄存器的数据。读取到的数据会存储在`response`数组中。 以上是一个简单的C#通讯汇川PLC EASY系列的例子,具体的实现方式需要根据实际情况进行调整和修改。

     

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

11eleven

你的鼓励是我创作的动力 !

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值