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);
}
写入操作
OperateResult result = plc.Write("D6000", 1);
if (result.IsSuccess)
{
return BaseResult.Successed;
}