| 123456789101112131415161718192021222324252627282930313233343536373839 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using WCS.Core;using WCS.Entity;using HslCommunication.Profinet.Melsec; namespace WCS.Service.PLCAccessors{     public class MelsecPLC : IPLCAccessor    {        MelsecMcNet plc;        public MelsecPLC(string ip,int port)        {            plc = new MelsecMcNet(ip, port);            plc.ConnectTimeOut = 3000;            //plc.ReceiveTimeOut = 500;            plc.ConnectServer();                  }        public byte[] ReadBytes(ushort db, ushort address, ushort length)        {            var res = plc.Read("D" + db, length);            if (res.IsSuccess)                return res.Content;            throw new Exception("读取PLC数据失败:" + res.Message);        }        public void WriteBytes(ushort db, ushort address, byte[] data)        {            var start = db + address / 2;            var res = plc.Write("D" + start, data);            if (!res.IsSuccess)                throw new Exception("写入PLC数据失败:" + res.Message);        }    }}
 |