using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace FLY.Modbus.WithThread
{
    /// <summary>
    /// 同一时刻只能有一个通信，当同时执行 Do_xx, 这些指令会被hold住
    /// 如果连接断开，或者异常，这些 Do_xx,全部返回 false
    /// </summary>
    public interface IModbusClient
    {
        /// <summary>
        /// 通信超时
        /// </summary>
        TimeSpan Timeout { get; set; }

        /// <summary>
        /// socket 错误信息
        /// </summary>
        string ErrMsg { get; set; }

        /// <summary>
        /// 连接成功
        /// </summary>
        bool IsConnected { get; set; }

        /// <summary>
        /// 远端地址
        /// </summary>
        IPEndPoint RemoteEP { get; set; }

        /// <summary>
        /// 建立连接
        /// </summary>
        bool Connect();

        /// <summary>
        /// 停止
        /// </summary>
        void Close();

        /// <summary>
        /// 读多个 COIL
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="cnt"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        bool Do_01(int addr, int cnt, out IEnumerable<bool> values);


        /// <summary>
        /// 读多个Holding REGs
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="cnt"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        bool Do_03(int addr, int cnt, out IEnumerable<UInt16> values);

        /// <summary>
        /// Write Single Coil
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        bool Do_05(int addr, bool data);


        /// <summary>
        /// 写多个coil
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        bool Do_0F(int addr, IEnumerable<bool> datas);


        /// <summary>
        /// 写多个REG
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        bool Do_10(int addr, IEnumerable<UInt16> datas);
    }
}
