using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RainTest.Helper
{
    public class SiPLC
    {
        private SiemensS7Net PLC = new SiemensS7Net(SiemensPLCS.S200Smart);
        private Random dom = new Random();
        public string IP { get; set; }
        public bool Open()
        {
            PLC.IpAddress = IP;
            return PLC.ConnectServer().IsSuccess;
        }
        public float ReadFloat(string address)
        {
            //var test = PLC.ReadFloat("M0.7");
            return PLC.ReadFloat(address).Content;
        }
        /// <summary>
        /// 用这个方法试试
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public bool ReadBool(string address)
        {
            return PLC.ReadBool(address).Content;
        }
        public bool ReadWord(string address)
        {
            return PLC.ReadBool(address).Content;
        }
        public HslCommunication.OperateResult WriteFloat(string address,float value)
        {
            return PLC.Write(address,value);
        }
        public HslCommunication.OperateResult WriteFloat1(string address, float value)
        {
            float test = value / 1000;
            return PLC.Write(address, test);
        }
        public HslCommunication.OperateResult WriteFloat(string address, bool value)
        {
            return PLC.Write(address, value);
        }
        public bool Close()
        {
            try 
            {
                return PLC.ConnectClose().IsSuccess;
            }
            catch(Exception ex)
            { 
            }
            return true;
        }
    }
}
