﻿//============================================================-
//	MODBUSCS.CS
//	Class file for libmodbus
//												CONTEC.Co.,Ltd.
//============================================================-
using System;
using System.Runtime.InteropServices;

namespace ModbusCs
{
    public enum ModbusConst
    {
        //-------------------------------------------------
        //	Modbus function codes
        //-------------------------------------------------
        MODBUS_FC_READ_COILS = 0x01,
        MODBUS_FC_READ_DISCRETE_INPUTS = 0x02,
        MODBUS_FC_READ_HOLDING_REGISTERS = 0x03,
        MODBUS_FC_READ_INPUT_REGISTERS = 0x04,
        MODBUS_FC_WRITE_SINGLE_COIL = 0x05,
        MODBUS_FC_WRITE_SINGLE_REGISTER = 0x06,
        MODBUS_FC_READ_EXCEPTION_STATUS = 0x07,
        MODBUS_FC_WRITE_MULTIPLE_COILS = 0x0F,
        MODBUS_FC_WRITE_MULTIPLE_REGISTERS = 0x10,
        MODBUS_FC_REPORT_SLAVE_ID = 0x11,
        MODBUS_FC_MASK_WRITE_REGISTER = 0x16,
        MODBUS_FC_WRITE_AND_READ_REGISTERS = 0x17,
        //-------------------------------------------------
        //	Parameters
        //-------------------------------------------------
        //  Modbus Broadcast Address
        MODBUS_BROADCAST_ADDRESS = 0,
        //  Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
        MODBUS_MAX_READ_BITS = 2000,
        //  Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
        MODBUS_MAX_WRITE_BITS = 1968,
        //  Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
        MODBUS_MAX_READ_REGISTERS = 125,
        //  Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
        MODBUS_MAX_WRITE_REGISTERS = 123,
        //  Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
        MODBUS_MAX_WR_WRITE_REGISTERS = 121,
        MODBUS_MAX_WR_READ_REGISTERS = 125,
        //  The size of the MODBUS PDU
        MODBUS_MAX_PDU_LENGTH = 253,
        //  The size of the MODBUS ADU
        MODBUS_MAX_ADU_LENGTH = 260,
    }

    public class Modbus
    {
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_set_slave(IntPtr ctx, int slave);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_get_slave(IntPtr ctx);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_get_response_timeout(IntPtr ctx, ref uint to_sec, ref uint to_usec);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_set_response_timeout(IntPtr ctx, uint to_sec, uint to_usec);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_get_byte_timeout(IntPtr ctx, ref uint to_sec, ref uint to_usec);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_set_byte_timeout(IntPtr ctx, uint to_sec, uint to_usec);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_connect(IntPtr ctx);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_close(IntPtr ctx);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_free(IntPtr ctx);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_flush(IntPtr ctx);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern IntPtr modbus_strerror(int errnum);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_read_errno();
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_read_bits(IntPtr ctx, int addr, int nb, [MarshalAs(UnmanagedType.LPArray)] byte[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_read_input_bits(IntPtr ctx, int addr, int nb, [MarshalAs(UnmanagedType.LPArray)] byte[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_read_registers(IntPtr ctx, int addr, int nb, [MarshalAs(UnmanagedType.LPArray)] ushort[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_read_input_registers(IntPtr ctx, int addr, int nb, [MarshalAs(UnmanagedType.LPArray)] ushort[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_write_bit(IntPtr ctx, int coil_addr, int status);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_write_register(IntPtr ctx, int reg_addr, int value);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_write_bits(IntPtr ctx, int addr, int nb, [MarshalAs(UnmanagedType.LPArray)] byte[] data);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern int modbus_write_registers(IntPtr ctx, int addr, int nb, [MarshalAs(UnmanagedType.LPArray)] ushort[] data);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_set_bits_from_byte([MarshalAs(UnmanagedType.LPArray)] byte[] dest, int idx, byte value);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_set_bits_from_bytes([MarshalAs(UnmanagedType.LPArray)] byte[] dest, int idx, int nb_bits, [MarshalAs(UnmanagedType.LPArray)] byte[] tab_byte);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern byte modbus_get_byte_from_bits([MarshalAs(UnmanagedType.LPArray)] byte[] src, int idx, int nb_bits);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern float modbus_get_float_abcd([MarshalAs(UnmanagedType.LPArray)] ushort[] src);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern float modbus_get_float_dcba([MarshalAs(UnmanagedType.LPArray)] ushort[] src);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern float modbus_get_float_badc([MarshalAs(UnmanagedType.LPArray)] ushort[] src);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern float modbus_get_float_cdab([MarshalAs(UnmanagedType.LPArray)] ushort[] src);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_set_float_abcd(float f, [MarshalAs(UnmanagedType.LPArray)] ushort[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_set_float_dcba(float f, [MarshalAs(UnmanagedType.LPArray)] ushort[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_set_float_badc(float f, [MarshalAs(UnmanagedType.LPArray)] ushort[] dest);
        [DllImport("modbus.dll", CallingConvention = CallingConvention.Cdecl)] static extern void modbus_set_float_cdab(float f, [MarshalAs(UnmanagedType.LPArray)] ushort[] dest);

        public int SetSlave(IntPtr ctx, int slave)
        {
            int ret = modbus_set_slave(ctx, slave);
            return ret;
        }
        public int GetSlave(IntPtr ctx)
        {
            int ret = modbus_get_slave(ctx);
            return ret;
        }
        public void GetResponseTimeout(IntPtr ctx, ref uint to_sec, ref uint to_usec)
        {
            modbus_get_response_timeout(ctx, ref to_sec, ref to_usec);
        }
        public void SetResponseTimeout(IntPtr ctx, uint to_sec, uint to_usec)
        {
            modbus_set_response_timeout(ctx, to_sec, to_usec);
        }
        public void GetByteTimeout(IntPtr ctx, ref uint to_sec, ref uint to_usec)
        {
            modbus_get_byte_timeout(ctx, ref to_sec, ref to_usec);
        }
        public void SetByteTimeout(IntPtr ctx, uint to_sec, uint to_usec)
        {
            modbus_set_byte_timeout(ctx, to_sec, to_usec);
        }
        public int Connect(IntPtr ctx)
        {
            int ret = modbus_connect(ctx);
            return ret;
        }
        public void Close(IntPtr ctx)
        {
            modbus_close(ctx);
        }
        public void Free(IntPtr ctx)
        {
            modbus_free(ctx);
        }
        public void Flush(IntPtr ctx)
        {
            modbus_flush(ctx);
        }
        public string StrError(int errnum)
        {
            string error = Marshal.PtrToStringAnsi(modbus_strerror(errnum)) ?? string.Empty;
            return error;
        }
        public int ReadErrno()
        {
            int errnum = modbus_read_errno();
            return errnum;
        }
        public int ReadBits(IntPtr ctx, int addr, int nb, byte[] dest)
        {
            int ret = modbus_read_bits(ctx, addr, nb, dest);
            return ret;
        }
        public int ReadInputBits(IntPtr ctx, int addr, int nb, byte[] dest)
        {
            int ret = modbus_read_input_bits(ctx, addr, nb, dest);
            return ret;
        }
        public int ReadRegisters(IntPtr ctx, int addr, int nb, ushort[] dest)
        {
            int ret = modbus_read_registers(ctx, addr, nb, dest);
            return ret;
        }
        public int ReadInputRegisters(IntPtr ctx, int addr, int nb, ushort[] dest)
        {
            int ret = modbus_read_input_registers(ctx, addr, nb, dest);
            return ret;
        }
        public int WriteBit(IntPtr ctx, int coil_addr, int status)
        {
            int ret = modbus_write_bit(ctx, coil_addr, status);
            return ret;
        }
        public int WriteRegister(IntPtr ctx, int reg_addr, ushort value)
        {
            int ret = modbus_write_register(ctx, reg_addr, value);
            return ret;
        }
        public int WriteBits(IntPtr ctx, int addr, int nb, byte[] data)
        {
            int ret = modbus_write_bits(ctx, addr, nb, data);
            return ret;
        }
        public int WriteRegisters(IntPtr ctx, int addr, int nb, ushort[] data)
        {
            int ret = modbus_write_registers(ctx, addr, nb, data);
            return ret;
        }
        public void SetBitsFromByte(byte[] dest, int idx, byte value)
        {
            modbus_set_bits_from_byte(dest, idx, value);
        }
        public void SetBitsFromBytes(byte[] dest, int idx, int nb_bits, byte[] tab_byte)
        {
            modbus_set_bits_from_bytes(dest, idx, nb_bits, tab_byte);
        }
        public byte GetByteFromBits(byte[] src, int idx, int nb_bits)
        {
            byte ret = modbus_get_byte_from_bits(src, idx, nb_bits);
            return ret;
        }
        public float GetFloatABCD(ushort[] src)
        {
            float ret = modbus_get_float_abcd(src);
            return ret;
        }
        public float GetFloatDCBA(ushort[] src)
        {
            float ret = modbus_get_float_dcba(src);
            return ret;
        }
        public float GetFloatBADC(ushort[] src)
        {
            float ret = modbus_get_float_badc(src);
            return ret;
        }
        public float GetFloatCDAB(ushort[] src)
        {
            float ret = modbus_get_float_cdab(src);
            return ret;
        }
        public void SetFloatABCD(float f, ushort[] dest)
        {
            modbus_set_float_abcd(f, dest);
        }
        public void SetFloatDCBA(float f, ushort[] dest)
        {
            modbus_set_float_dcba(f, dest);
        }
        public void SetFloatBADC(float f, ushort[] dest)
        {
            modbus_set_float_badc(f, dest);
        }
        public void SetFloatCDAB(float f, ushort[] dest)
        {
            modbus_set_float_cdab(f, dest);
        }
    }

    // Description of common functions

}
