using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneralGommunication
{
    public interface IModbusBase : INotifyPropertyChanged
    {
        /// <summary>
        /// 通讯中
        /// </summary>
        bool IsInComm { get; }

        /// <summary>
        /// 指令成功回复时，回复的耗时
        /// </summary>
        TimeSpan Elapsed { get; }

        /// <summary>
        /// 10s 内最大回复的耗时
        /// </summary>
        int MaxElapsedms10s { get; }

        ErrorType Error { get; }

        bool IsError { get; }

        string ErrMsg { get; }

        bool IsCommConnected { get; }
        string CommErrMsg { get; }

        /// <summary>
        /// 接收的数据
        /// </summary>
        byte[] RXDs { get; }

        /// <summary>
        /// 数据包间隔时间，必须确保 接收包 与 发送包之间等待 3.5T以上时间
        /// </summary>
        int PackIntervalms { get; }

        void Init(IGeneralComm serial);

        #region  功能

        /// <summary>
        /// 读多个 COIL
        /// </summary>
        /// <param name="device_no">设备号</param>
        /// <param name="addr">参数首地址</param>
        /// <param name="cnt">线圈个数</param>
        /// <param name="values">输出线圈状态</param>
        /// <returns></returns>
        bool Do_01(byte device_no, 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(byte device_no, int addr, int cnt, out List<UInt16> values);


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


        /// <summary>
        /// Write Single REG
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        bool Do_06(byte device_no, int addr, UInt16 dat);
        /// <summary>
        /// 写多个coil
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        bool Do_0F(byte device_no, int addr, List<bool> datas);


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

        #endregion
    }

    public enum ErrorType
    {
        /// <summary>
        /// 默认值
        /// </summary>
        Idle,
        /// <summary>
        /// 超时
        /// </summary>
        TimeOut,
        /// <summary>
        /// 未知错误
        /// </summary>
        Unknown,
        /// <summary>
        /// 校验出错
        /// </summary>
        CRCError,
        /// <summary>
        /// 成功
        /// </summary>
        OK,
        /// <summary>
        /// 线路忙
        /// </summary>
        Busy,
        /// <summary>
        /// 连接断开
        /// </summary>
        Disconnected,
    }
}
