#pragma once
#include <stdio.h>
#include <errno.h>
#include <string>
#include <string.h>
#include "modbus.h"

namespace modbus
{
    class CLibmodbusWrapper
    {
    public:
        CLibmodbusWrapper();
        virtual ~CLibmodbusWrapper();
        int initialize(const char *ip, int port, int slave_id);
        void deinitialize();

    public:
        int read_registers(int addr, int nb, uint16_t *dest);
        int write_registers(int addr, int nb, uint16_t *dest);

        int read_register(int addr, uint16_t *dest);
        int write_register(int addr, uint16_t *dest);

        inline bool is_connected() { return (0 == _ctx) ? false : true; };
        inline std::string getIP() { return _ip; };
        inline int getPort() { return _port; };
        inline int getSlave() { return _slave_id; };

    private:
        modbus_t* _ctx;
    private:
        std::string _ip;
        int _port;
        int _slave_id;
    };
};
