1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using PlcSiemens.O;
- using PlcSiemens.Protocol.Common;
- using ServiceCenter;
- using ServiceCenter.Virtual_PLC;
- using WCS.Core;
- namespace WCS.Service.PLCAccessors
- {
- public class SiemensS7PLC : IPLCAccessor
- {
- private SimenssPlc plc;
- public SiemensS7PLC(string ip, int port, int rack, int slot)
- {
- plc = new SimenssPlc(ip, rack, slot);
- plc.Connect();
- }
- public byte[] ReadBytes(ushort db, ushort address, ushort length)
- {
- if (ServiceHub.Any(SystemMode.虚拟plc))
- {
- return PlcData.Read(new PLCData { IP = plc.IP, DB = db, Length = length, DataLength = length });
- }
- if (!plc.Connected)
- plc.Connect();
- var res = plc.ReadArea(AreaType.DB, db, address, length, DataType.Byte);
- if (res == null)
- throw new Exception("读取DB块数据失败");
- return res.Data;
- }
- public void WriteBytes(ushort db, ushort address, byte[] data)
- {
- if (ServiceHub.Any(SystemMode.虚拟plc))
- {
- PlcData.Write(new PLCData { IP = plc.IP, DB = db }, address, data);
- }
- else
- {
- if (!plc.Connected)
- plc.Connect();
- var res = plc.WriteArea(AreaType.DB, db, address, (ushort)data.Length, DataType.Byte, data);
- if (!res)
- throw new Exception("写入DB块数据失败");
- }
- }
- }
- }
|