#include <QMutex>
#include "modbus.h"
#include "common.h"
class PLCDevice
{
public:
    QMutex g_mutex; //访问Modbus的互斥锁
    modbus_t* g_modbus = NULL;		//Modbus

    bool g_plc_ok;
    
    //[0]:1=连接,0=断开
    uint8_t g_plc_status;
#ifdef __ModebusClient
    int m_modbusSocket;
    bool m_initialized;
    UINT8 m_errCount=0;
    modbus_mapping_t* mapping{ nullptr };
    /*Mapping*/
    int m_numBits{ 500 };
    int m_numInputBits{ 500 };
    int m_numRegisters{ 500 };
    int m_numInputRegisters{ 500 };
    void recieveMessages();
    std::thread *modSerThread;
#endif
    static bool init_plc(PLCDevice* PLCptr);
    static bool init_plc_tcp(PLCDevice* PLCptr);

    int write_bit_2_plc(int addr, int value);
    int read_bits_from_plc(int addr, int nb, uint8_t* value);
    int write_short_2_plc(int addr, int nb, uint16_t* value);
    int read_short_from_plc(int addr, int nb, uint16_t* value);
    int write_2_plc(int addr, int nb, uint16_t* value);
    bool disconnect_plc(void);
};
