#include "modbusrtu.h"
#include <QTimer>
#include <QEvent>
#include <QDebug>
#include "modbus-private.h"
#include <QMessageBox>
ModbusRTU::ModbusRTU(QObject *parent) : QThread(parent)
{
    init();
}

ModbusRTU::~ModbusRTU()
{
    modbus_close(m_ModbusRTUDevice);
    modbus_free(m_ModbusRTUDevice);
}

void ModbusRTU::init()
{

}

void ModbusRTU::run()
{
    qDebug() << "进入线程：" << QThread::currentThread();
    while(true)
    {
        // qDebug() << "11111111111111111111111111111111";
        QThread::msleep(200);
        uint16_t base1_45[45];
        uint16_t base57_78[22];
        uint16_t base83_106[24];
        uint16_t base47[1];
        uint16_t base49[1];
        uint16_t base51[1];
        uint16_t base53[1];
        uint16_t base55[1];
        QList<int> base_list_47_49_51_53_55;
        uint16_t sgl_vol[112];    // Buffer to store the registers
        uint16_t sgl_tempt[112];

    label:    int ret_base1_45 = modbus_read_input_registers(m_ModbusRTUDevice, 1, 45, base1_45);
        if(MODBUS_ERR == ret_base1_45)
        {
            qDebug() << "modbus no message";
            goto label;
        }
        else
        {
            // qDebug() << "2222222222222222222222222222222";
            QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
            emit sig_showModbusData_base_info1_45(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->sendMsg);
            QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
            emit sig_showModbusData_base_info1_45(recvArray, TypeRecvData);
            free(m_ModbusRTUDevice->recvMsg);
            // qDebug() << "in modbus:" << base1_45;
        }

        int ret_base47 = modbus_read_input_registers(m_ModbusRTUDevice, 47, 1, base47);
        int ret_base49 = modbus_read_input_registers(m_ModbusRTUDevice, 49, 1, base49);
        int ret_base51 = modbus_read_input_registers(m_ModbusRTUDevice, 51, 1, base51);
        int ret_base53 = modbus_read_input_registers(m_ModbusRTUDevice, 53, 1, base53);
        int ret_base55 = modbus_read_input_registers(m_ModbusRTUDevice, 55, 1, base55);
        if (ret_base47 == -1 | ret_base49 == -1 | ret_base51 == -1 | ret_base53 == -1 | ret_base55 == -1)
        {
            return;
        }
        else
        {
            QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
            emit sig_showModbusData_base_info47_49_51_53_55(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->sendMsg);
            // QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
            QByteArray recvArray;
            recvArray.append(reinterpret_cast<char*>(&base47), sizeof(base47));
            recvArray.append(reinterpret_cast<char*>(&base49), sizeof(base49));
            recvArray.append(reinterpret_cast<char*>(&base51), sizeof(base51));
            recvArray.append(reinterpret_cast<char*>(&base53), sizeof(base53));
            recvArray.append(reinterpret_cast<char*>(&base55), sizeof(base55));

            for (int i = 0; i < recvArray.size(); i += 2) {
                uint16_t value;
                memcpy(&value, recvArray.data() + i, 2);
                base_list_47_49_51_53_55.append(value);
                // base47_49_51_53_55[i] = value;
                // qDebug() << "Value: " << value;
            }

            emit sig_showModbusData_base_info_list_47_49_51_53_55(base_list_47_49_51_53_55, TypeRecvData);
            // emit sig_showModbusData_base_info47_49_51_53_55(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->recvMsg);
        }

        int ret_base57_78 = modbus_read_input_registers(m_ModbusRTUDevice, 57, 22, base57_78);
        if(MODBUS_ERR == ret_base57_78)
        {
            // qDebug() << "modbus no message";
            return;
        }
        else
        {
            // qDebug() << "4444444444444444444444444444";
            QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
            emit sig_showModbusData_base_info57_78(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->sendMsg);
            QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
            emit sig_showModbusData_base_info57_78(recvArray, TypeRecvData);
            free(m_ModbusRTUDevice->recvMsg);
        }

        int ret_base83_106 = modbus_read_input_registers(m_ModbusRTUDevice, 83, 24, base83_106);
        if(MODBUS_ERR == ret_base83_106)
        {
            // qDebug() << "modbus no message";
            goto label;
        }
        else
        {
            // qDebug() << "5555555555555555555555555555";
            QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
            emit sig_showModbusData_base_info83_106(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->sendMsg);
            QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
            emit sig_showModbusData_base_info83_106(recvArray, TypeRecvData);
            free(m_ModbusRTUDevice->recvMsg);
        }

        int ret_vol = modbus_read_input_registers(m_ModbusRTUDevice, 1001, 112, sgl_vol);
        if(MODBUS_ERR == ret_vol)
        {
            // qDebug() << "modbus no message";
            goto label;

        }
        else
        {
            QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
            emit sig_showModbusData(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->sendMsg);
            QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
            emit sig_showModbusData(recvArray, TypeRecvData);
            free(m_ModbusRTUDevice->recvMsg);
        }

        int ret_tempt = modbus_read_input_registers(m_ModbusRTUDevice, 1601, 112, sgl_tempt);
        if(MODBUS_ERR == ret_tempt)
        {
            // qDebug() << "modbus no message";
            goto label;
        }
        else
        {
            QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
            emit sig_showModbusData_sgl_tempt(sendArray, TypeSendData);
            free(m_ModbusRTUDevice->sendMsg);
            QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
            emit sig_showModbusData_sgl_tempt(recvArray, TypeRecvData);
            free(m_ModbusRTUDevice->recvMsg);
        }


    }
}


void ModbusRTU::write_regs_slot(int addr,const uint16_t data)
{
    int rc = write_single_register(addr, data);
    if(MODBUS_ERR == rc) {
        qDebug() << "######modbus no message";
        return;
    }
}

void ModbusRTU::read_regs_slot(int addr, int nb, uint16_t *data, int row)
{
    int rc = read_holding_register(addr, nb, data);
    if(MODBUS_ERR == rc) {
        // qDebug() << "modbus no message";
        return;
    }
    else {
        QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
        emit sig_showReadHoldingRegs(recvArray, TypeRecvData, row, nb);
        free(m_ModbusRTUDevice->recvMsg);
    }
}

bool ModbusRTU::modbusRTUConnect(const char *comPort, int baudRate, char parity, int dataBit, int stopBit)
{
    qDebug() << "comPort:" << comPort << "baudRate:" << baudRate << "parity:" << parity << "dataBit" << dataBit << "stopBit:" << stopBit;
    m_ModbusRTUDevice = modbus_new_rtu(comPort, baudRate, parity, dataBit, stopBit);
    if(m_ModbusRTUDevice == NULL) {
        qDebug() << "create modbus device failed";
        return false;
    }
    else {
        qDebug() << "create modbus device success";
    }

    if (MODBUS_ERR == modbus_set_slave(m_ModbusRTUDevice, 1)
        || MODBUS_ERR == modbus_connect(m_ModbusRTUDevice)) {
        modbus_free(m_ModbusRTUDevice);
        qDebug() << "modbus disconnect";
        return false;
    }
    else {
        qDebug() << "modbus connect";
        modbus_set_debug(m_ModbusRTUDevice, false);
        return true;
    }
}

void ModbusRTU::modbusRTUDisconnect()
{
    modbus_close(m_ModbusRTUDevice);
}

int ModbusRTU::read_coil_status(int addr, int nb, uint8_t *dest)
{
    return modbus_read_bits(m_ModbusRTUDevice, addr, nb, dest);
}

int ModbusRTU::read_input_status(int addr, int nb, uint8_t *dest)
{
    return modbus_read_input_bits(m_ModbusRTUDevice, addr, nb, dest);
}

int ModbusRTU::read_holding_register(int addr, int nb, uint16_t *dest)
{
    return modbus_read_registers(m_ModbusRTUDevice, addr, nb, dest);
}

int ModbusRTU::read_input_register(int addr, int nb, uint16_t *dest)
{
    return modbus_read_input_registers(m_ModbusRTUDevice, addr, nb, dest);
}

int ModbusRTU::write_single_coil(int coil_addr, int status)
{
    return modbus_write_bit(m_ModbusRTUDevice, coil_addr, status);
}

int ModbusRTU::write_single_register(int reg_addr, const uint16_t value)
{
    return modbus_write_register(m_ModbusRTUDevice, reg_addr, value);
}

int ModbusRTU::write_multiple_coil(int addr, int nb, const uint8_t *data)
{
    return modbus_write_bits(m_ModbusRTUDevice, addr, nb, data);
}

int ModbusRTU::write_multiple_registers(int addr, int nb, const uint16_t *data)
{
    return modbus_write_registers(m_ModbusRTUDevice, addr, nb, data);
}
