#include "mbs.h" #include "log.h" #include "mb.h" #include "mbctx.h" #include "plt.h" #include "port.h" enum mbs_idx_t { MBSIDX_CTN = 0, MBSIDX_EMA = 1, }; // 0:cess2000 modbus tcp slave fmodbus_t *MB[8]; /* ----------------------- Defines ------------------------------------------*/ /* ctn modbus tcp slave*/ #define REG_HOLDING_START_CESS2000 0x0000 #define REG_HOLDING_NREGS_CESS2000 0x5400 // static USHORT usRegHoldingStart_CTN[CTN_NBR_MAX + 1]; static USHORT usRegHoldingBuf_CESS2000[REG_HOLDING_NREGS_CESS2000] = {0}; #define REG_INPUT_START_ZH200 0x0000 #define REG_INPUT_REG_ZH200 0x5400 static USHORT usRegInputReg_ZH200[REG_INPUT_REG_ZH200] = {0}; #define REG_HOLDING_START_EMA 0x0000 #define REG_HOLDING_NREGS_EMA 0x5400 // static USHORT usRegHoldingStart_CTN[CTN_NBR_MAX + 1]; static USHORT usRegHoldingBuf_EMA[REG_HOLDING_NREGS_EMA] = {0}; static enum ThreadState { STOPPED, RUNNING, SHUTDOWN } ePollThreadState; static pthread_mutex_t xLock = PTHREAD_MUTEX_INITIALIZER; // static BOOL bDoExit; void *pvPollingThread(void *pvParameter) { fmodbus_t *ctx = (fmodbus_t *)pvParameter; // struct chan_t* chan = &STA.chan[ctx->chanidx]; log_dbg("%s, ++, mbsidx:%d", __func__, ctx->mbsidx); if (eMBEnable(ctx) == MB_ENOERR) { do { if (eMBPoll(ctx) != MB_ENOERR) break; } while (TRUE); } (void)eMBDisable(ctx); log_dbg("%s, --, mbsidx:%d", __func__, ctx->mbsidx); return NULL; } static void onReadInputBuf_CESS2000(fmodbus_t *ctx) { // struct chan_t* chan = &STA.chan[ctx->chanidx]; // int mbsidx = chan->mbsidx; // int mbsdevidx = chan->mbsdevidx; struct cess2000_t *dev = &cess2000[1]; short temp = 0; // struct bcu_t* bcu = &STA.catl280a[1].bcu[1]; // int bmuidx, moduleidx, cellidx, tempidx; // if( chan->dbg > 0 ){ // log_dbg("%s, chanidx:%d, mbsidx:%d, mbsdevm:%d, mbsdevidx:%d", // __func__, ctx->chanidx, chan->mbsidx, chan->mbsdevm, chan->mbsdevidx); // } usRegInputReg_ZH200[0 - REG_INPUT_START_ZH200] = ctn_get_cmd(); usRegInputReg_ZH200[1 - REG_INPUT_START_ZH200] = 0; } // process 03 static void prepareRegHoldingBuf_CESS2000(fmodbus_t *ctx, int iAddress) { struct cess2000_t *dev = &cess2000[1]; usRegHoldingBuf_CESS2000[252 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_tick(); usRegHoldingBuf_CESS2000[253 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_state(); usRegHoldingBuf_CESS2000[254 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_err(); usRegHoldingBuf_CESS2000[255 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_ap(); usRegHoldingBuf_CESS2000[256 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_soc(); usRegHoldingBuf_CESS2000[257 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_soh(); usRegHoldingBuf_CESS2000[258 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_chgable(); usRegHoldingBuf_CESS2000[259 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_dhgable(); usRegHoldingBuf_CESS2000[260 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_aps(); // 有功功率设定值 usRegHoldingBuf_CESS2000[261 - REG_HOLDING_START_CESS2000] = ctn_get_cmd(); usRegHoldingBuf_CESS2000[271 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_comm_st(); usRegHoldingBuf_CESS2000[272 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ap(); usRegHoldingBuf_CESS2000[273 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_igbt_temp(); usRegHoldingBuf_CESS2000[276 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_uab(); usRegHoldingBuf_CESS2000[277 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ubc(); usRegHoldingBuf_CESS2000[278 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_uac(); usRegHoldingBuf_CESS2000[279 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ia(); usRegHoldingBuf_CESS2000[280 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ib(); usRegHoldingBuf_CESS2000[281 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ic(); usRegHoldingBuf_CESS2000[282 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_dcv(); usRegHoldingBuf_CESS2000[283 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_dcc(); usRegHoldingBuf_CESS2000[301 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_comm_st(); usRegHoldingBuf_CESS2000[302 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_hv_stat(); usRegHoldingBuf_CESS2000[303 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_bat_v(); usRegHoldingBuf_CESS2000[304 - REG_HOLDING_START_CESS2000] = (int)pack_get_bat_c(); // log_dbg("%s, pack_get_bat_c:%.1f", __func__, pack_get_bat_c()); // log_dbg("%s, pack_get_bat_c:%d", __func__, (unsigned short)pack_get_bat_c()); // log_dbg("%s, pack_get_bat_c:%d", __func__, (unsigned short)(pack_get_bat_c())); // log_dbg("%s, pack_get_bat_c:%d", __func__, (short)(pack_get_bat_c())); usRegHoldingBuf_CESS2000[307 - REG_HOLDING_START_CESS2000] = (unsigned short)(pack_get_cell_v_max() * 1000); usRegHoldingBuf_CESS2000[308 - REG_HOLDING_START_CESS2000] = (unsigned short)(pack_get_cell_v_ave() * 1000); usRegHoldingBuf_CESS2000[309 - REG_HOLDING_START_CESS2000] = (unsigned short)(pack_get_cell_v_min() * 1000); usRegHoldingBuf_CESS2000[310 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_cell_t_max(); usRegHoldingBuf_CESS2000[311 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_cell_t_ave(); usRegHoldingBuf_CESS2000[312 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_cell_t_min(); usRegHoldingBuf_CESS2000[315 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_v_diff(); usRegHoldingBuf_CESS2000[316 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_t_diff(); usRegHoldingBuf_CESS2000[317 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_resistance_pos(); usRegHoldingBuf_CESS2000[318 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_resistance_neg(); usRegHoldingBuf_CESS2000[351 - REG_HOLDING_START_CESS2000] = (unsigned short)ac_get_comm_st(1); usRegHoldingBuf_CESS2000[352 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_outwater_temp(1) * 10); usRegHoldingBuf_CESS2000[353 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_rtnwater_temp(1) * 10); usRegHoldingBuf_CESS2000[354 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_outwater_press(1) * 10); usRegHoldingBuf_CESS2000[355 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_rtnwater_press(1) * 10); usRegHoldingBuf_CESS2000[356 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_env_temp(1) * 10); usRegHoldingBuf_CESS2000[371 - REG_HOLDING_START_CESS2000] = (unsigned short)meter_get_comm_st(meter_get_model()[1], 1); usRegHoldingBuf_CESS2000[372 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_pos_ae(meter_get_model()[1], 1)); usRegHoldingBuf_CESS2000[373 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_neg_ae(meter_get_model()[1], 1)); usRegHoldingBuf_CESS2000[374 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_com_ae(meter_get_model()[1], 1)); usRegHoldingBuf_CESS2000[375 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_com_ap(meter_get_model()[1], 1)); usRegHoldingBuf_CESS2000[382 - REG_HOLDING_START_CESS2000] = (unsigned short)(dehumi_get_comm_st(1)); usRegHoldingBuf_CESS2000[383 - REG_HOLDING_START_CESS2000] = (unsigned short)(dehumi_get_temp(1)); usRegHoldingBuf_CESS2000[384 - REG_HOLDING_START_CESS2000] = (unsigned short)(dehumi_get_humi(1)); usRegHoldingBuf_CESS2000[392 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_comm_st(DEVM_RH811AD053F, 1)); usRegHoldingBuf_CESS2000[393 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_temp(DEVM_RH811AD053F, 1)); usRegHoldingBuf_CESS2000[394 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_humi(DEVM_RH811AD053F, 1)); usRegHoldingBuf_CESS2000[397 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_comm_st(DEVM_CD1F3300, 1)); usRegHoldingBuf_CESS2000[398 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_co_density(DEVM_CD1F3300, 1)); usRegHoldingBuf_CESS2000[421 - REG_HOLDING_START_CESS2000] = (unsigned short)STA.version[1]; // 软件版本号 usRegHoldingBuf_CESS2000[422 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_cap() & 0xffff0000) >> 16; usRegHoldingBuf_CESS2000[423 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_cap() & 0x0000ffff); usRegHoldingBuf_CESS2000[424 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_pow() & 0xffff0000) >> 16; usRegHoldingBuf_CESS2000[425 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_pow() & 0x0000ffff); if (iAddress >= 1000 && iAddress < 1100) pcs_get_modbus_data((USHORT *)(&usRegHoldingBuf_CESS2000[1000])); if (iAddress >= 1100 && iAddress < 1200) pack_get_modbus_data((USHORT *)(&usRegHoldingBuf_CESS2000[1100])); if (iAddress >= 1200 && iAddress < 1300) ac_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1200])); if (iAddress >= 1300 && iAddress < 1400) evn_get_modbus_data(DEVM_RH811AD053F, 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1300])); // 温湿度 if (iAddress >= 1400 && iAddress < 1500) evn_get_modbus_data(DEVM_CD1F3300, 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1400])); // 一氧化碳 if (iAddress >= 1500 && iAddress < 1600) dehumi_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1500])); // 除湿机 if (iAddress >= 1600 && iAddress < 1700) meter_get_modbus_data(DEVM_ZH200, 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1600])); // 辅助电表 if (iAddress >= 1700 && iAddress < 1800) fe_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1700])); // 消防 for (int i = 0; i < 5; i++) { if (iAddress >= 1800 + i * 20 && iAddress < 1800 + i * 20 + 20) fa_get_modbus_data(i + 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1800 + i * 20])); // 消防 } if (iAddress >= 1900 && iAddress < 2000) dido_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1900])); // IO int pnbr = pack_get_pack_nbr(); int cnbr = pack_get_cell_nbr(); usRegHoldingBuf_CESS2000[2000 - REG_HOLDING_START_CESS2000] = pnbr; usRegHoldingBuf_CESS2000[2001 - REG_HOLDING_START_CESS2000] = cnbr; unsigned short *cellv = pack_get_cell_v(); unsigned short *cellt = pack_get_cell_t(); memcpy(&usRegHoldingBuf_CESS2000[2005 - REG_HOLDING_START_CESS2000], cellv, pnbr * cnbr * sizeof(cellv[0])); memcpy(&usRegHoldingBuf_CESS2000[2505 - REG_HOLDING_START_CESS2000], cellt, pnbr * cnbr * sizeof(cellt[0])); } // 06 static void onWriteRegHoldingBuf_CESS2000(fmodbus_t *ctx) { // log_dbg("%s, ++ register write", __func__); if (usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000] != 0) { log_info("%s,get cmd(%d) form host", __func__, usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000]); ctn_set_cmd(usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000]); usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000] = 0; } if ((short)usRegHoldingBuf_CESS2000[13 - REG_HOLDING_START_CESS2000] != ctn_get_aps()) { log_info("%s,get new aps(%d) form host with stamp(%d)", __func__, (short)usRegHoldingBuf_CESS2000[13 - REG_HOLDING_START_CESS2000], usRegHoldingBuf_CESS2000[14 - REG_HOLDING_START_CESS2000]); ctn_set_aps((short)usRegHoldingBuf_CESS2000[13 - REG_HOLDING_START_CESS2000]); } dido_fire_confirm(1, usRegHoldingBuf_CESS2000[1910 - REG_HOLDING_START_CESS2000]); dido_fan_switch(1, usRegHoldingBuf_CESS2000[1911 - REG_HOLDING_START_CESS2000]); dido_sys_alarm(1, usRegHoldingBuf_CESS2000[1912 - REG_HOLDING_START_CESS2000]); if (ctn_get_bsytikchk_en() != 1) { ctn_enable_bsytikchk(); } else { // log_dbg("%s, update bsytik, %d", __func__, usRegHoldingBuf_CESS2000[200 - REG_HOLDING_START_CESS2000]); ctn_set_bsytikchk_from_host(usRegHoldingBuf_CESS2000[200 - REG_HOLDING_START_CESS2000]); } // log_dbg("%s, --", __func__); } static void prepareRegHoldingBuf_EMA(fmodbus_t *ctx) { float temp_f = 0; unsigned short temp_u16 = 0; signed short temp_s16 = 0; signed int temp_s32 = 0; unsigned short *ptemp = NULL; // log_info("%s,running!!!!!",__func__); if (ems_get_mode() == EMSMOD_NONE) { usRegHoldingBuf_EMA[1000 - REG_HOLDING_START_EMA] = 0; usRegHoldingBuf_EMA[1001 - REG_HOLDING_START_EMA] = 0; } else if (ems_get_mode() == EMSMOD_PCURV) { usRegHoldingBuf_EMA[1000 - REG_HOLDING_START_EMA] = 0; usRegHoldingBuf_EMA[1001 - REG_HOLDING_START_EMA] = 1; } temp_f = 600.0; usRegHoldingBuf_EMA[1002 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1003 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); usRegHoldingBuf_EMA[1004 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1005 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); temp_f = ctn_get_soc(); usRegHoldingBuf_EMA[1006 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1007 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); // cell fault state-------reserve temp_f = 2000.0; usRegHoldingBuf_EMA[1010 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1011 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); temp_f = 1800; usRegHoldingBuf_EMA[1012 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1013 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); temp_f = pcs_get_chg_e_total(); usRegHoldingBuf_EMA[1014 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1015 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); temp_f = pcs_get_dhg_e_total(); usRegHoldingBuf_EMA[1016 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1017 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); usRegHoldingBuf_EMA[1018 - REG_HOLDING_START_EMA] = 0; usRegHoldingBuf_EMA[1019 - REG_HOLDING_START_EMA] = ctn_get_tick(); temp_s32 = ctn_cal_get_cycle(); usRegHoldingBuf_EMA[1020 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32 >> 16; usRegHoldingBuf_EMA[1021 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32; usRegHoldingBuf_EMA[1022 - REG_HOLDING_START_EMA] = 0; usRegHoldingBuf_EMA[1023 - REG_HOLDING_START_EMA] = ctn_get_state(); temp_s32 = ctn_get_ap(); usRegHoldingBuf_EMA[1024 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32 >> 16; usRegHoldingBuf_EMA[1025 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32; temp_f = pcs_get_grid_freq(); usRegHoldingBuf_EMA[1026 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16; usRegHoldingBuf_EMA[1027 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)); usRegHoldingBuf_EMA[1028 - REG_HOLDING_START_EMA] = 0; usRegHoldingBuf_EMA[1029 - REG_HOLDING_START_EMA] = ctn_get_err(); } static void procRegHoldingBuf_EMA(fmodbus_t *ctx) { unsigned int temp = 0; int aps = 0; temp = usRegHoldingBuf_EMA[2000 - REG_HOLDING_START_EMA]; // log_info("%s,temp(%d)",__func__,temp); temp = (temp << 16) + usRegHoldingBuf_EMA[2001 - REG_HOLDING_START_EMA]; // log_info("%s,temp(%d)",__func__,temp); aps = *((float *)(&temp)); // if(aps != 0){ log_info("%s,get aps(%d),tepm(%d),2000(%d),2001(%d)form host", __func__, aps, temp, usRegHoldingBuf_EMA[2000 - REG_HOLDING_START_EMA], usRegHoldingBuf_EMA[2001 - REG_HOLDING_START_EMA]); if (ems_get_mode() == EMSMOD_NONE) ctn_set_aps(aps); //} } eMBErrorCode eMBRegInputCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs) { eMBErrorCode eStatus = MB_ENOERR; int iRegIndex; // struct chan_t* chan = &STA.chan[ctx->chanidx]; // if (chan->mbsidx >= 1 && chan->mbsidx <= PCS_NBR_MAX) if (ctx->mbsidx == MBSIDX_CTN) { // if ((usAddress >= REG_INPUT_START_PCS) && (usAddress + usNRegs <= REG_INPUT_START_PCS + REG_INPUT_NREGS_PCS)) // if ((usAddress >= REG_HOLDING_START_CESS2000) && (usAddress + usNRegs <= REG_HOLDING_START_CESS2000 + REG_HOLDING_NREGS_CESS2000)) if ((usAddress >= REG_INPUT_START_ZH200) && (usAddress + usNRegs <= REG_INPUT_START_ZH200 + REG_INPUT_REG_ZH200)) { // iRegIndex = (int)(usAddress - usRegInputStart_PCS[chan->mbsidx]); onReadInputBuf_CESS2000(ctx); iRegIndex = (int)(usAddress - REG_INPUT_START_ZH200); while (usNRegs > 0) { *pucRegBuffer++ = (UCHAR)(usRegInputReg_ZH200[iRegIndex] >> 8); *pucRegBuffer++ = (UCHAR)(usRegInputReg_ZH200[iRegIndex] & 0xFF); iRegIndex++; usNRegs--; } } else { eStatus = MB_ENOREG; } return eStatus; } return MB_ENOREG; } eMBErrorCode eMBRegHoldingCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode) { eMBErrorCode eStatus = MB_ENOERR; int iRegIndex; int i = 0; // struct chan_t* chan = &STA.chan[ctx->chanidx]; // if( chan->dbg > 0 ){ // log_dbg("%s, chanidx:%d, mbsidx:%d, mbsdevm:%d, mbsdevidx:%d, usAddress:%d, usNRegs:%d, eMode:%d", // __func__, ctx->chanidx, chan->mbsidx, chan->mbsdevm, chan->mbsdevidx, // usAddress, usNRegs, eMode); // } // if( chan->en == 0 ){ // return MB_EIO; // } if (ctx->mbsidx == MBSIDX_CTN) { // ctn modbus tcp slave if ((usAddress >= REG_HOLDING_START_CESS2000) && (usAddress + usNRegs <= REG_HOLDING_START_CESS2000 + REG_HOLDING_NREGS_CESS2000)) { iRegIndex = (int)(usAddress - REG_HOLDING_START_CESS2000); switch (eMode) { /* Pass current register values to the protocol stack. */ case MB_REG_READ: prepareRegHoldingBuf_CESS2000(ctx, usAddress); while (usNRegs > 0) { *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_CESS2000[iRegIndex] >> 8); *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_CESS2000[iRegIndex] & 0xFF); iRegIndex++; usNRegs--; } break; /* Update current register values with new values from the * protocol stack. */ case MB_REG_WRITE: while (usNRegs > 0) { usRegHoldingBuf_CESS2000[iRegIndex] = *pucRegBuffer++ << 8; usRegHoldingBuf_CESS2000[iRegIndex] |= *pucRegBuffer++; iRegIndex++; usNRegs--; } onWriteRegHoldingBuf_CESS2000(ctx); break; } } else { eStatus = MB_ENOREG; } return eStatus; } else if (ctx->mbsidx == MBSIDX_EMA) { // ctn modbus tcp slave if ((usAddress >= REG_HOLDING_START_EMA) && (usAddress + usNRegs <= REG_HOLDING_START_EMA + REG_HOLDING_NREGS_EMA)) { iRegIndex = (int)(usAddress - REG_HOLDING_START_EMA); // log_info("%s,%d",__func__,iRegIndex); switch (eMode) { /* Pass current register values to the protocol stack. */ case MB_REG_READ: prepareRegHoldingBuf_EMA(ctx); while (usNRegs > 0) { *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_EMA[iRegIndex] >> 8); *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_EMA[iRegIndex] & 0xFF); iRegIndex++; usNRegs--; } break; /* Update current register values with new values from the * protocol stack. */ case MB_REG_WRITE: while (usNRegs > 0) { usRegHoldingBuf_EMA[iRegIndex] = *pucRegBuffer++ << 8; usRegHoldingBuf_EMA[iRegIndex] |= *pucRegBuffer++; iRegIndex++; usNRegs--; } procRegHoldingBuf_EMA(ctx); break; } } else { eStatus = MB_ENOREG; } return eStatus; } } eMBErrorCode eMBRegCoilsCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode) { return MB_ENOREG; } eMBErrorCode eMBRegDiscreteCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete) { return MB_ENOREG; } int mbs_start_ctn(int port) { int ret = 0; const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC}; pthread_t xthrd; if (eMBTCPInit(&MB[MBSIDX_CTN], port) != MB_ENOERR) { log_dbg("%s, eMBTCPInit fail", __func__); ret = -1; } else if (eMBSetSlaveID(MB[MBSIDX_CTN], 0x34, TRUE, ucSlaveID, 3) != MB_ENOERR) { log_dbg("%s, eMBSetSlaveID fail", __func__); ret = -2; } else { MB[MBSIDX_CTN]->mbsidx = MBSIDX_CTN; if (pthread_create(&xthrd, NULL, pvPollingThread, MB[MBSIDX_CTN]) != 0) { log_dbg("%s, pthread_create fail", __func__); ret = -3; } else { log_dbg("%s, start ok", __func__); } } return ret; } int mbs_start_EMA(int port) { int ret = 0; const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC}; pthread_t xthrd; if (eMBTCPInit(&MB[MBSIDX_EMA], port) != MB_ENOERR) { log_dbg("%s, eMBTCPInit fail", __func__); ret = -1; } else if (eMBSetSlaveID(MB[MBSIDX_EMA], 0x34, TRUE, ucSlaveID, 3) != MB_ENOERR) { log_dbg("%s, eMBSetSlaveID fail", __func__); ret = -2; } else { MB[MBSIDX_EMA]->mbsidx = MBSIDX_EMA; if (pthread_create(&xthrd, NULL, pvPollingThread, MB[MBSIDX_EMA]) != 0) { log_dbg("%s, pthread_create fail", __func__); ret = -3; } else { log_dbg("%s, port:%d,start ok", __func__, port); } } return ret; }