/****************************************************************************
** File name    :  HTModbus.cpp
** Description  :  define ISO Modbus Protocol api
** Create date  :  2019.09.01
** author by    :  Liuyx
** Version info :  V1.0.01
** Copyright By:  xi'an huatek, Inc Co., Ltd
** Update record:
** DATE         AUTHER   DESC
** -------------------------------------------------------------------------
** 2019.09.01   Liuyx    first build
****************************************************************************/
#include "HTGlobal.h"
static const char *_FILE_ = "HTModbus.cpp";



//***********************************************************************
// Function Name :  createModbus
// Description   :  createModbus Modbus Event
// Param In      :  int slaveid
//     slaveid   :  slave id
// Param Out     :  None
//   param name  :  param description
// Return Code   :  
//        !NULL  :  Successed
//         NULL  :  Failed
// Create by     :  Liuyx
// Create date   :  2020:7:16 17:17
// History modify:  Liuyx
// Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
//-----------------------------------------------------------------------
// Modify by     :  Date            : Context
// Liuyx  :  2020/07/16          : created
//***********************************************************************
static modbus_t *createModbus(int slaveid)
{
    modbus_t *pCtx = NULL;

    if (stringncasecmp(g_TConfig.getModbusMode(), "RTU", 3) == 0)
    {
        pCtx = modbus_new_rtu(g_TConfig.getModbusRtuPort(), g_TConfig.getModbusBaud(),
            g_TConfig.getModbusParity(), g_TConfig.getModbusDataBit(),
            g_TConfig.getModbusStopBit());
        vPrtLogMsg(LOG_DEBUG, RET_OK, "Connect Modbus to: Port=%s,Baud=%d,Parity=%c,DataBit=%d,StopBit=%d",
            g_TConfig.getModbusRtuPort(), g_TConfig.getModbusBaud(),
            g_TConfig.getModbusParity(), g_TConfig.getModbusDataBit(),
            g_TConfig.getModbusStopBit());
    }
    else if (stringncasecmp(g_TConfig.getModbusMode(), "TCP", 3) == 0)
    {
        vPrtLogMsg(LOG_ERROR, errno, "modbus communication mode:[%s] not support!", g_TConfig.getModbusMode());
        return NULL;
        //pCtx = modbus_new_tcp(g_TConfig.getModbusTcpAddr(), g_TConfig.getModbusTcpPort());
    }
    else if (stringncasecmp(g_TConfig.getModbusMode(), "ASC", 3) == 0)
    {
        vPrtLogMsg(LOG_ERROR, errno, "modbus communication mode:[%s] not support!", g_TConfig.getModbusMode());
        return NULL;
    }
    else
    {
        vPrtLogMsg(LOG_ERROR, errno, "modbus communication mode:[%s] invalid!", g_TConfig.getModbusMode());
        return NULL;
    }
    if (pCtx == NULL) 
    {
        vPrtLogMsg(LOG_ERROR, errno, "modbus communication open:[%s] failed!", g_TConfig.getModbusMode());
        return NULL;
    }
    
    modbus_set_debug(pCtx, g_TConfig.isLogDebug());
    modbus_set_slave(pCtx, slaveid);  // 设置从设备地址ID=0x11
    //modbus_rtu_set_serial_mode(pCtx, MODBUS_RTU_RS232);
    //modbus_rtu_set_rts(pCtx, MODBUS_RTU_RTS_UP);

    modbus_set_error_recovery(pCtx, modbus_error_recovery_mode(MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL));
    int umic = modbus_rtu_get_rts_delay(pCtx);

    if (modbus_connect(pCtx) == -1) {
        vPrtLogMsg(LOG_ERROR, errno, "Modbus Connection failed:%s", modbus_strerror(errno));
        modbus_free(pCtx);
        return NULL;
    }
    return pCtx;
}

//***********************************************************************
// Function Name :  thread_modbus_service_proc
// Description   :  modbus 服务线程
// Param In      :  void * arg
//   param name  :  param description
// Param Out     :  
//   param name  :  param description
// Return Code   :  void *
//        RET_OK :  Successed
//      RET_FAIL :  Failed
// Create by     :  
// Create date   :  2020:7:16 17:18
// History modify:  
// Copyright (C), 2012-2020, Xi'an Huatek. Co., Ltd.
//-----------------------------------------------------------------------
// Modify by     :  Date            : Context
//   :  2020/07/16          : 
//***********************************************************************
void *thread_modbus_service_proc(void *arg)
{
    vPrtLogMsg(LOG_DEBUG, 0, "thread_modbus_service_proc = %d startup...", GETTID());
    int  SlaveID = 11;
    char szTmp[MAX_PATH] = { 0 };
    int CommMode = 0;
    modbus_t *pCtx = NULL;
    
    // 初始化modbus通信环境
    if (NULL == (pCtx = createModbus(SlaveID))) return NULL;

    uint8_t szBuf[MODBUS_MAX_READ_BITS] = { 0 };
    int nb_points = MODBUS_MAX_READ_BITS;
    int rc = 0;
    while (g_Running)
    {
        rc = modbus_read_bits(pCtx, 0x11, 3,szBuf);
        //rc = modbus_read_registers(pCtx, 0x00, 3, tab_reg);
        if (rc == -1) continue; 
        printf("Recv Len = %d\n", rc);
        _SLEEP(100);
    }
    return NULL;
}



