using System;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using EasyModbus;

namespace StandardScene.Utils
{

    public class ModbusRtu
    {

        public bool IsDebug { get; set; }
        public byte[] ReceiveAfterSend;
        public ModbusClient modbusRtu;
        //modbusRtu
        public void StartRtu(string com, int baudRate, Parity parity = Parity.None, StopBits stopBits = StopBits.One, int timeOut = 500)
        {
            modbusRtu = new ModbusClient(com);
            modbusRtu.Baudrate = baudRate;
            modbusRtu.Parity = parity;
            modbusRtu.StopBits = stopBits;
            modbusRtu.ConnectionTimeout = timeOut;
            modbusRtu.Connect();
        }
        //modbusTcp
        public void StartTcpRtu(string ip, int port)
        {
            modbusRtu = new ModbusClient(ip, port);
            modbusRtu.Connect(ip, port);
        }

        public void Close()
        {
            modbusRtu.Disconnect();
        }

        #region ReadData     
        public bool[] ReadCoilBuffer_01(byte slaveAddress, ushort startAddress, ushort numberOfPoints)//01 读取单个线圈
        {
            modbusRtu.UnitIdentifier = slaveAddress;
            var data = modbusRtu.ReadCoils(startAddress, numberOfPoints);
            return data;
        }
        public bool[] ReadDiscreteInputs_02(byte slaveAddress, ushort startAddress, ushort numberOfPoints)//02 读取输入线圈/离散量线圈
        {
            modbusRtu.UnitIdentifier = slaveAddress;
            var data = modbusRtu.ReadDiscreteInputs(startAddress, numberOfPoints);
            return data;
        }
        public int[] ReadRegisterBuffer_03(byte slaveAddress, ushort startAddress, ushort numberOfPoints)//03 读取保持寄存器
        {
            modbusRtu.UnitIdentifier = slaveAddress;
            var data = modbusRtu.ReadHoldingRegisters(startAddress, numberOfPoints);

            return data;
        }
        public byte[] ReadRegisterBuffer_03_Byte(byte slaveAddress, ushort startAddress, ushort numberOfPoints)//03 读取保持寄存器
        {
            modbusRtu.UnitIdentifier = slaveAddress;
            var data = modbusRtu.ReadHoldingRegisters(startAddress, numberOfPoints);
            byte[] values = new byte[] { };
            foreach (var buff in data)
            {
                values = values.Concat(BitConverter.GetBytes((Int16)buff).AsEnumerable().Reverse()).ToArray();
            }
            return values;
        }


        public byte[] ReadInputBuffer_04(byte slaveAddress, ushort startAddress, ushort numberOfPoints)//04 读取输入寄存器
        {

            modbusRtu.UnitIdentifier = slaveAddress;
            var data = modbusRtu.ReadInputRegisters(startAddress, numberOfPoints);
            byte[] values = new byte[] { };
            foreach (var buff in data)
            {
                values = values.Concat(BitConverter.GetBytes((Int16)buff).AsEnumerable().Reverse()).ToArray();
            }
            return values;
        }
        #endregion

        #region Write
        public void WriteSingleCoil_05(byte slaveAddress, ushort startAddress, bool Buffer)//05 写单个线圈
        {
            ReceiveAfterSend = null;
            modbusRtu.UnitIdentifier = slaveAddress;
            modbusRtu.WriteSingleCoil(startAddress, Buffer);
            ReceiveAfterSend = modbusRtu.receiveData;
        }
        public void WriteSingleRegister_06(byte slaveAddress, ushort startAddress, int Buffer)//06 写单寄存器
        {
            ReceiveAfterSend = null;
            modbusRtu.UnitIdentifier = slaveAddress;
            modbusRtu.WriteSingleRegister(startAddress, Buffer);
            ReceiveAfterSend = modbusRtu.receiveData;
        }
        public void WriteMultipleCoils_15(byte slaveAddress, ushort startAddress, bool[] Buffer)//15写一组线圈
        {
            ReceiveAfterSend = null;
            modbusRtu.UnitIdentifier = slaveAddress;
            modbusRtu.WriteMultipleCoils(startAddress, Buffer);
            ReceiveAfterSend = modbusRtu.receiveData;
        }
        public void WriteMultipleRegisters_16(byte slaveAddress, ushort startAddress, int[] Buffers)//16 写一组保持寄存器
        {
            ReceiveAfterSend = null;
            modbusRtu.UnitIdentifier = slaveAddress;
            modbusRtu.WriteMultipleRegisters(startAddress, Buffers);
            ReceiveAfterSend = modbusRtu.receiveData;

        }
        #endregion


    }
}
