C# 与三菱PLC MC协议通讯

1. 引用HslCommunication

Nuget包里边添加 HslCommunication包的引用

2.创建PLC连接对象,并通过 IP,端口进行连接

//PLC连接对象 
var plc = new MelsecMcNet(_ip, _port); 
 plc.ConnectTimeOut = 3000;

 var res = plc.ConnectServer();//连接PLC
 isConnect = res.IsSuccess;

3.读写操作

  //// 读取操作
  var D2200_D2201 = plc.Read("D2200", 4);
  if (D2200_D2201.IsSuccess)
  {
      PLCDataInfo.D2200 = plc.ByteTransform.TransInt16(new byte[] { D2200_D2201.Content[0], D2200_D2201.Content[1] }, 0);
      PLCDataInfo.D2201 = plc.ByteTransform.TransInt16(new byte[] { D2200_D2201.Content[2], D2200_D2201.Content[3] }, 0);
  }

//X Y的读取 默认16进制,8进制的前面补0
PLCDataInfo.X13 = plc.ReadBool("X013").Content;
PLCDataInfo.X14 = plc.ReadBool("X014").Content;

PLCDataInfo.Y34 = plc.ReadBool("Y034").Content;
PLCDataInfo.Y50 = plc.ReadBool("Y050").Content;

//M 的读取 返回bool类型
 var M700 = plc.ReadBool("M700");
 PLCDataInfo.M700 = M700.Content ? 1 : 0;

 //// D寄存器 单地址 读取操作
 PLCDataInfo.D2201 = plc.ReadInt16("D2201").Content;
 PLCDataInfo.D2202 = plc.ReadInt16("D2202").Content;
 PLCDataInfo.D2203 = plc.ReadInt16("D2203").Content;
 PLCDataInfo.D2204 = plc.ReadInt16("D2204").Content;
 //// 写入操作
 OperateResult result = plc.Write("D6000", 1);

 if (result.IsSuccess)
 {
     return BaseResult.Successed;
 }

我写的太过于简洁,项目用不到那么多功能,推荐看下一位大神写的,希望能帮助到大家

https://www.cnblogs.com/woxihuadabai/articles/8037872.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Rotion_深

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值