mbs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #include "mbs.h"
  2. #include "log.h"
  3. #include "mb.h"
  4. #include "mbctx.h"
  5. #include "plt.h"
  6. #include "port.h"
  7. enum mbs_idx_t
  8. {
  9. MBSIDX_CTN = 0,
  10. MBSIDX_EMA = 1,
  11. };
  12. // 0:cess2000 modbus tcp slave
  13. fmodbus_t *MB[8];
  14. /* ----------------------- Defines ------------------------------------------*/
  15. /* ctn modbus tcp slave*/
  16. #define REG_HOLDING_START_CESS2000 0x0000
  17. #define REG_HOLDING_NREGS_CESS2000 0x5400
  18. // static USHORT usRegHoldingStart_CTN[CTN_NBR_MAX + 1];
  19. static USHORT usRegHoldingBuf_CESS2000[REG_HOLDING_NREGS_CESS2000] = {0};
  20. #define REG_INPUT_START_ZH200 0x0000
  21. #define REG_INPUT_REG_ZH200 0x5400
  22. static USHORT usRegInputReg_ZH200[REG_INPUT_REG_ZH200] = {0};
  23. #define REG_HOLDING_START_EMA 0x0000
  24. #define REG_HOLDING_NREGS_EMA 0x5400
  25. // static USHORT usRegHoldingStart_CTN[CTN_NBR_MAX + 1];
  26. static USHORT usRegHoldingBuf_EMA[REG_HOLDING_NREGS_EMA] = {0};
  27. static enum ThreadState {
  28. STOPPED,
  29. RUNNING,
  30. SHUTDOWN
  31. } ePollThreadState;
  32. static pthread_mutex_t xLock = PTHREAD_MUTEX_INITIALIZER;
  33. // static BOOL bDoExit;
  34. void *pvPollingThread(void *pvParameter)
  35. {
  36. fmodbus_t *ctx = (fmodbus_t *)pvParameter;
  37. // struct chan_t* chan = &STA.chan[ctx->chanidx];
  38. log_dbg("%s, ++, mbsidx:%d", __func__, ctx->mbsidx);
  39. if (eMBEnable(ctx) == MB_ENOERR)
  40. {
  41. do
  42. {
  43. if (eMBPoll(ctx) != MB_ENOERR)
  44. break;
  45. } while (TRUE);
  46. }
  47. (void)eMBDisable(ctx);
  48. log_dbg("%s, --, mbsidx:%d", __func__, ctx->mbsidx);
  49. return NULL;
  50. }
  51. static void onReadInputBuf_CESS2000(fmodbus_t *ctx)
  52. {
  53. // struct chan_t* chan = &STA.chan[ctx->chanidx];
  54. // int mbsidx = chan->mbsidx;
  55. // int mbsdevidx = chan->mbsdevidx;
  56. struct cess2000_t *dev = &cess2000[1];
  57. short temp = 0;
  58. // struct bcu_t* bcu = &STA.catl280a[1].bcu[1];
  59. // int bmuidx, moduleidx, cellidx, tempidx;
  60. // if( chan->dbg > 0 ){
  61. // log_dbg("%s, chanidx:%d, mbsidx:%d, mbsdevm:%d, mbsdevidx:%d",
  62. // __func__, ctx->chanidx, chan->mbsidx, chan->mbsdevm, chan->mbsdevidx);
  63. // }
  64. usRegInputReg_ZH200[0 - REG_INPUT_START_ZH200] = ctn_get_cmd();
  65. usRegInputReg_ZH200[1 - REG_INPUT_START_ZH200] = 0;
  66. }
  67. // process 03
  68. static void prepareRegHoldingBuf_CESS2000(fmodbus_t *ctx, int iAddress)
  69. {
  70. struct cess2000_t *dev = &cess2000[1];
  71. usRegHoldingBuf_CESS2000[252 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_tick();
  72. usRegHoldingBuf_CESS2000[253 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_state();
  73. usRegHoldingBuf_CESS2000[254 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_err();
  74. usRegHoldingBuf_CESS2000[255 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_ap();
  75. usRegHoldingBuf_CESS2000[256 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_soc();
  76. usRegHoldingBuf_CESS2000[257 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_soh();
  77. usRegHoldingBuf_CESS2000[258 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_chgable();
  78. usRegHoldingBuf_CESS2000[259 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_dhgable();
  79. usRegHoldingBuf_CESS2000[260 - REG_HOLDING_START_CESS2000] = (unsigned short)ctn_get_aps(); // 有功功率设定值
  80. usRegHoldingBuf_CESS2000[261 - REG_HOLDING_START_CESS2000] = ctn_get_cmd();
  81. usRegHoldingBuf_CESS2000[271 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_comm_st();
  82. usRegHoldingBuf_CESS2000[272 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ap();
  83. usRegHoldingBuf_CESS2000[273 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_igbt_temp();
  84. usRegHoldingBuf_CESS2000[276 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_uab();
  85. usRegHoldingBuf_CESS2000[277 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ubc();
  86. usRegHoldingBuf_CESS2000[278 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_uac();
  87. usRegHoldingBuf_CESS2000[279 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ia();
  88. usRegHoldingBuf_CESS2000[280 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ib();
  89. usRegHoldingBuf_CESS2000[281 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_ic();
  90. usRegHoldingBuf_CESS2000[282 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_dcv();
  91. usRegHoldingBuf_CESS2000[283 - REG_HOLDING_START_CESS2000] = (unsigned short)pcs_get_dcc();
  92. usRegHoldingBuf_CESS2000[301 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_comm_st();
  93. usRegHoldingBuf_CESS2000[302 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_hv_stat();
  94. usRegHoldingBuf_CESS2000[303 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_bat_v();
  95. usRegHoldingBuf_CESS2000[304 - REG_HOLDING_START_CESS2000] = (int)pack_get_bat_c();
  96. // log_dbg("%s, pack_get_bat_c:%.1f", __func__, pack_get_bat_c());
  97. // log_dbg("%s, pack_get_bat_c:%d", __func__, (unsigned short)pack_get_bat_c());
  98. // log_dbg("%s, pack_get_bat_c:%d", __func__, (unsigned short)(pack_get_bat_c()));
  99. // log_dbg("%s, pack_get_bat_c:%d", __func__, (short)(pack_get_bat_c()));
  100. usRegHoldingBuf_CESS2000[307 - REG_HOLDING_START_CESS2000] = (unsigned short)(pack_get_cell_v_max() * 1000);
  101. usRegHoldingBuf_CESS2000[308 - REG_HOLDING_START_CESS2000] = (unsigned short)(pack_get_cell_v_ave() * 1000);
  102. usRegHoldingBuf_CESS2000[309 - REG_HOLDING_START_CESS2000] = (unsigned short)(pack_get_cell_v_min() * 1000);
  103. usRegHoldingBuf_CESS2000[310 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_cell_t_max();
  104. usRegHoldingBuf_CESS2000[311 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_cell_t_ave();
  105. usRegHoldingBuf_CESS2000[312 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_cell_t_min();
  106. usRegHoldingBuf_CESS2000[315 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_v_diff();
  107. usRegHoldingBuf_CESS2000[316 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_t_diff();
  108. usRegHoldingBuf_CESS2000[317 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_resistance_pos();
  109. usRegHoldingBuf_CESS2000[318 - REG_HOLDING_START_CESS2000] = (unsigned short)pack_get_resistance_neg();
  110. usRegHoldingBuf_CESS2000[351 - REG_HOLDING_START_CESS2000] = (unsigned short)ac_get_comm_st(1);
  111. usRegHoldingBuf_CESS2000[352 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_outwater_temp(1) * 10);
  112. usRegHoldingBuf_CESS2000[353 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_rtnwater_temp(1) * 10);
  113. usRegHoldingBuf_CESS2000[354 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_outwater_press(1) * 10);
  114. usRegHoldingBuf_CESS2000[355 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_rtnwater_press(1) * 10);
  115. usRegHoldingBuf_CESS2000[356 - REG_HOLDING_START_CESS2000] = (unsigned short)(ac_get_env_temp(1) * 10);
  116. usRegHoldingBuf_CESS2000[371 - REG_HOLDING_START_CESS2000] = (unsigned short)meter_get_comm_st(meter_get_model()[1], 1);
  117. usRegHoldingBuf_CESS2000[372 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_pos_ae(meter_get_model()[1], 1));
  118. usRegHoldingBuf_CESS2000[373 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_neg_ae(meter_get_model()[1], 1));
  119. usRegHoldingBuf_CESS2000[374 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_com_ae(meter_get_model()[1], 1));
  120. usRegHoldingBuf_CESS2000[375 - REG_HOLDING_START_CESS2000] = (unsigned short)(meter_get_com_ap(meter_get_model()[1], 1));
  121. usRegHoldingBuf_CESS2000[382 - REG_HOLDING_START_CESS2000] = (unsigned short)(dehumi_get_comm_st(1));
  122. usRegHoldingBuf_CESS2000[383 - REG_HOLDING_START_CESS2000] = (unsigned short)(dehumi_get_temp(1));
  123. usRegHoldingBuf_CESS2000[384 - REG_HOLDING_START_CESS2000] = (unsigned short)(dehumi_get_humi(1));
  124. usRegHoldingBuf_CESS2000[392 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_comm_st(DEVM_RH811AD053F, 1));
  125. usRegHoldingBuf_CESS2000[393 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_temp(DEVM_RH811AD053F, 1));
  126. usRegHoldingBuf_CESS2000[394 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_humi(DEVM_RH811AD053F, 1));
  127. usRegHoldingBuf_CESS2000[397 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_comm_st(DEVM_CD1F3300, 1));
  128. usRegHoldingBuf_CESS2000[398 - REG_HOLDING_START_CESS2000] = (unsigned short)(env_get_co_density(DEVM_CD1F3300, 1));
  129. usRegHoldingBuf_CESS2000[421 - REG_HOLDING_START_CESS2000] = (unsigned short)STA.version[1]; // 软件版本号
  130. usRegHoldingBuf_CESS2000[422 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_cap() & 0xffff0000) >> 16;
  131. usRegHoldingBuf_CESS2000[423 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_cap() & 0x0000ffff);
  132. usRegHoldingBuf_CESS2000[424 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_pow() & 0xffff0000) >> 16;
  133. usRegHoldingBuf_CESS2000[425 - REG_HOLDING_START_CESS2000] = (ctn_get_norm_pow() & 0x0000ffff);
  134. if (iAddress >= 1000 && iAddress < 1100)
  135. pcs_get_modbus_data((USHORT *)(&usRegHoldingBuf_CESS2000[1000]));
  136. if (iAddress >= 1100 && iAddress < 1200)
  137. pack_get_modbus_data((USHORT *)(&usRegHoldingBuf_CESS2000[1100]));
  138. if (iAddress >= 1200 && iAddress < 1300)
  139. ac_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1200]));
  140. if (iAddress >= 1300 && iAddress < 1400)
  141. evn_get_modbus_data(DEVM_RH811AD053F, 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1300])); // 温湿度
  142. if (iAddress >= 1400 && iAddress < 1500)
  143. evn_get_modbus_data(DEVM_CD1F3300, 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1400])); // 一氧化碳
  144. if (iAddress >= 1500 && iAddress < 1600)
  145. dehumi_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1500])); // 除湿机
  146. if (iAddress >= 1600 && iAddress < 1700)
  147. meter_get_modbus_data(DEVM_ZH200, 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1600])); // 辅助电表
  148. if (iAddress >= 1700 && iAddress < 1800)
  149. fe_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1700])); // 消防
  150. for (int i = 0; i < 5; i++)
  151. {
  152. if (iAddress >= 1800 + i * 20 && iAddress < 1800 + i * 20 + 20)
  153. fa_get_modbus_data(i + 1, (USHORT *)(&usRegHoldingBuf_CESS2000[1800 + i * 20])); // 消防
  154. }
  155. if (iAddress >= 1900 && iAddress < 2000)
  156. dido_get_modbus_data(1, (USHORT *)(&usRegHoldingBuf_CESS2000[1900])); // IO
  157. int pnbr = pack_get_pack_nbr();
  158. int cnbr = pack_get_cell_nbr();
  159. usRegHoldingBuf_CESS2000[2000 - REG_HOLDING_START_CESS2000] = pnbr;
  160. usRegHoldingBuf_CESS2000[2001 - REG_HOLDING_START_CESS2000] = cnbr;
  161. unsigned short *cellv = pack_get_cell_v();
  162. unsigned short *cellt = pack_get_cell_t();
  163. memcpy(&usRegHoldingBuf_CESS2000[2005 - REG_HOLDING_START_CESS2000], cellv, pnbr * cnbr * sizeof(cellv[0]));
  164. memcpy(&usRegHoldingBuf_CESS2000[2505 - REG_HOLDING_START_CESS2000], cellt, pnbr * cnbr * sizeof(cellt[0]));
  165. }
  166. // 06
  167. static void onWriteRegHoldingBuf_CESS2000(fmodbus_t *ctx)
  168. {
  169. // log_dbg("%s, ++ register write", __func__);
  170. if (usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000] != 0)
  171. {
  172. log_info("%s,get cmd(%d) form host", __func__, usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000]);
  173. ctn_set_cmd(usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000]);
  174. usRegHoldingBuf_CESS2000[0 - REG_HOLDING_START_CESS2000] = 0;
  175. }
  176. if ((short)usRegHoldingBuf_CESS2000[13 - REG_HOLDING_START_CESS2000] != ctn_get_aps())
  177. {
  178. 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]);
  179. ctn_set_aps((short)usRegHoldingBuf_CESS2000[13 - REG_HOLDING_START_CESS2000]);
  180. }
  181. dido_fire_confirm(1, usRegHoldingBuf_CESS2000[1910 - REG_HOLDING_START_CESS2000]);
  182. dido_fan_switch(1, usRegHoldingBuf_CESS2000[1911 - REG_HOLDING_START_CESS2000]);
  183. dido_sys_alarm(1, usRegHoldingBuf_CESS2000[1912 - REG_HOLDING_START_CESS2000]);
  184. if (ctn_get_bsytikchk_en() != 1)
  185. {
  186. ctn_enable_bsytikchk();
  187. }
  188. else
  189. {
  190. // log_dbg("%s, update bsytik, %d", __func__, usRegHoldingBuf_CESS2000[200 - REG_HOLDING_START_CESS2000]);
  191. ctn_set_bsytikchk_from_host(usRegHoldingBuf_CESS2000[200 - REG_HOLDING_START_CESS2000]);
  192. }
  193. // log_dbg("%s, --", __func__);
  194. }
  195. static void prepareRegHoldingBuf_EMA(fmodbus_t *ctx)
  196. {
  197. float temp_f = 0;
  198. unsigned short temp_u16 = 0;
  199. signed short temp_s16 = 0;
  200. signed int temp_s32 = 0;
  201. unsigned short *ptemp = NULL;
  202. // log_info("%s,running!!!!!",__func__);
  203. if (ems_get_mode() == EMSMOD_NONE)
  204. {
  205. usRegHoldingBuf_EMA[1000 - REG_HOLDING_START_EMA] = 0;
  206. usRegHoldingBuf_EMA[1001 - REG_HOLDING_START_EMA] = 0;
  207. }
  208. else if (ems_get_mode() == EMSMOD_PCURV)
  209. {
  210. usRegHoldingBuf_EMA[1000 - REG_HOLDING_START_EMA] = 0;
  211. usRegHoldingBuf_EMA[1001 - REG_HOLDING_START_EMA] = 1;
  212. }
  213. temp_f = 600.0;
  214. usRegHoldingBuf_EMA[1002 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  215. usRegHoldingBuf_EMA[1003 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  216. usRegHoldingBuf_EMA[1004 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  217. usRegHoldingBuf_EMA[1005 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  218. temp_f = ctn_get_soc();
  219. usRegHoldingBuf_EMA[1006 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  220. usRegHoldingBuf_EMA[1007 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  221. // cell fault state-------reserve
  222. temp_f = 2000.0;
  223. usRegHoldingBuf_EMA[1010 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  224. usRegHoldingBuf_EMA[1011 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  225. temp_f = 1800;
  226. usRegHoldingBuf_EMA[1012 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  227. usRegHoldingBuf_EMA[1013 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  228. temp_f = pcs_get_chg_e_total();
  229. usRegHoldingBuf_EMA[1014 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  230. usRegHoldingBuf_EMA[1015 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  231. temp_f = pcs_get_dhg_e_total();
  232. usRegHoldingBuf_EMA[1016 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  233. usRegHoldingBuf_EMA[1017 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  234. usRegHoldingBuf_EMA[1018 - REG_HOLDING_START_EMA] = 0;
  235. usRegHoldingBuf_EMA[1019 - REG_HOLDING_START_EMA] = ctn_get_tick();
  236. temp_s32 = ctn_cal_get_cycle();
  237. usRegHoldingBuf_EMA[1020 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32 >> 16;
  238. usRegHoldingBuf_EMA[1021 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32;
  239. usRegHoldingBuf_EMA[1022 - REG_HOLDING_START_EMA] = 0;
  240. usRegHoldingBuf_EMA[1023 - REG_HOLDING_START_EMA] = ctn_get_state();
  241. temp_s32 = ctn_get_ap();
  242. usRegHoldingBuf_EMA[1024 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32 >> 16;
  243. usRegHoldingBuf_EMA[1025 - REG_HOLDING_START_EMA] = *(unsigned int *)&temp_s32;
  244. temp_f = pcs_get_grid_freq();
  245. usRegHoldingBuf_EMA[1026 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f)) >> 16;
  246. usRegHoldingBuf_EMA[1027 - REG_HOLDING_START_EMA] = *((unsigned int *)(&temp_f));
  247. usRegHoldingBuf_EMA[1028 - REG_HOLDING_START_EMA] = 0;
  248. usRegHoldingBuf_EMA[1029 - REG_HOLDING_START_EMA] = ctn_get_err();
  249. }
  250. static void procRegHoldingBuf_EMA(fmodbus_t *ctx)
  251. {
  252. unsigned int temp = 0;
  253. int aps = 0;
  254. temp = usRegHoldingBuf_EMA[2000 - REG_HOLDING_START_EMA];
  255. // log_info("%s,temp(%d)",__func__,temp);
  256. temp = (temp << 16) + usRegHoldingBuf_EMA[2001 - REG_HOLDING_START_EMA];
  257. // log_info("%s,temp(%d)",__func__,temp);
  258. aps = *((float *)(&temp));
  259. // if(aps != 0){
  260. 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]);
  261. if (ems_get_mode() == EMSMOD_NONE)
  262. ctn_set_aps(aps);
  263. //}
  264. }
  265. eMBErrorCode eMBRegInputCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs)
  266. {
  267. eMBErrorCode eStatus = MB_ENOERR;
  268. int iRegIndex;
  269. // struct chan_t* chan = &STA.chan[ctx->chanidx];
  270. // if (chan->mbsidx >= 1 && chan->mbsidx <= PCS_NBR_MAX)
  271. if (ctx->mbsidx == MBSIDX_CTN)
  272. {
  273. // if ((usAddress >= REG_INPUT_START_PCS) && (usAddress + usNRegs <= REG_INPUT_START_PCS + REG_INPUT_NREGS_PCS))
  274. // if ((usAddress >= REG_HOLDING_START_CESS2000) && (usAddress + usNRegs <= REG_HOLDING_START_CESS2000 + REG_HOLDING_NREGS_CESS2000))
  275. if ((usAddress >= REG_INPUT_START_ZH200) && (usAddress + usNRegs <= REG_INPUT_START_ZH200 + REG_INPUT_REG_ZH200))
  276. {
  277. // iRegIndex = (int)(usAddress - usRegInputStart_PCS[chan->mbsidx]);
  278. onReadInputBuf_CESS2000(ctx);
  279. iRegIndex = (int)(usAddress - REG_INPUT_START_ZH200);
  280. while (usNRegs > 0)
  281. {
  282. *pucRegBuffer++ = (UCHAR)(usRegInputReg_ZH200[iRegIndex] >> 8);
  283. *pucRegBuffer++ = (UCHAR)(usRegInputReg_ZH200[iRegIndex] & 0xFF);
  284. iRegIndex++;
  285. usNRegs--;
  286. }
  287. }
  288. else
  289. {
  290. eStatus = MB_ENOREG;
  291. }
  292. return eStatus;
  293. }
  294. return MB_ENOREG;
  295. }
  296. eMBErrorCode eMBRegHoldingCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode)
  297. {
  298. eMBErrorCode eStatus = MB_ENOERR;
  299. int iRegIndex;
  300. int i = 0;
  301. // struct chan_t* chan = &STA.chan[ctx->chanidx];
  302. // if( chan->dbg > 0 ){
  303. // log_dbg("%s, chanidx:%d, mbsidx:%d, mbsdevm:%d, mbsdevidx:%d, usAddress:%d, usNRegs:%d, eMode:%d",
  304. // __func__, ctx->chanidx, chan->mbsidx, chan->mbsdevm, chan->mbsdevidx,
  305. // usAddress, usNRegs, eMode);
  306. // }
  307. // if( chan->en == 0 ){
  308. // return MB_EIO;
  309. // }
  310. if (ctx->mbsidx == MBSIDX_CTN)
  311. { // ctn modbus tcp slave
  312. if ((usAddress >= REG_HOLDING_START_CESS2000) && (usAddress + usNRegs <= REG_HOLDING_START_CESS2000 + REG_HOLDING_NREGS_CESS2000))
  313. {
  314. iRegIndex = (int)(usAddress - REG_HOLDING_START_CESS2000);
  315. switch (eMode)
  316. {
  317. /* Pass current register values to the protocol stack. */
  318. case MB_REG_READ:
  319. prepareRegHoldingBuf_CESS2000(ctx, usAddress);
  320. while (usNRegs > 0)
  321. {
  322. *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_CESS2000[iRegIndex] >> 8);
  323. *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_CESS2000[iRegIndex] & 0xFF);
  324. iRegIndex++;
  325. usNRegs--;
  326. }
  327. break;
  328. /* Update current register values with new values from the
  329. * protocol stack. */
  330. case MB_REG_WRITE:
  331. while (usNRegs > 0)
  332. {
  333. usRegHoldingBuf_CESS2000[iRegIndex] = *pucRegBuffer++ << 8;
  334. usRegHoldingBuf_CESS2000[iRegIndex] |= *pucRegBuffer++;
  335. iRegIndex++;
  336. usNRegs--;
  337. }
  338. onWriteRegHoldingBuf_CESS2000(ctx);
  339. break;
  340. }
  341. }
  342. else
  343. {
  344. eStatus = MB_ENOREG;
  345. }
  346. return eStatus;
  347. }
  348. else if (ctx->mbsidx == MBSIDX_EMA)
  349. { // ctn modbus tcp slave
  350. if ((usAddress >= REG_HOLDING_START_EMA) && (usAddress + usNRegs <= REG_HOLDING_START_EMA + REG_HOLDING_NREGS_EMA))
  351. {
  352. iRegIndex = (int)(usAddress - REG_HOLDING_START_EMA);
  353. // log_info("%s,%d",__func__,iRegIndex);
  354. switch (eMode)
  355. {
  356. /* Pass current register values to the protocol stack. */
  357. case MB_REG_READ:
  358. prepareRegHoldingBuf_EMA(ctx);
  359. while (usNRegs > 0)
  360. {
  361. *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_EMA[iRegIndex] >> 8);
  362. *pucRegBuffer++ = (UCHAR)(usRegHoldingBuf_EMA[iRegIndex] & 0xFF);
  363. iRegIndex++;
  364. usNRegs--;
  365. }
  366. break;
  367. /* Update current register values with new values from the
  368. * protocol stack. */
  369. case MB_REG_WRITE:
  370. while (usNRegs > 0)
  371. {
  372. usRegHoldingBuf_EMA[iRegIndex] = *pucRegBuffer++ << 8;
  373. usRegHoldingBuf_EMA[iRegIndex] |= *pucRegBuffer++;
  374. iRegIndex++;
  375. usNRegs--;
  376. }
  377. procRegHoldingBuf_EMA(ctx);
  378. break;
  379. }
  380. }
  381. else
  382. {
  383. eStatus = MB_ENOREG;
  384. }
  385. return eStatus;
  386. }
  387. }
  388. eMBErrorCode eMBRegCoilsCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode)
  389. {
  390. return MB_ENOREG;
  391. }
  392. eMBErrorCode eMBRegDiscreteCB(fmodbus_t *ctx, UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
  393. {
  394. return MB_ENOREG;
  395. }
  396. int mbs_start_ctn(int port)
  397. {
  398. int ret = 0;
  399. const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
  400. pthread_t xthrd;
  401. if (eMBTCPInit(&MB[MBSIDX_CTN], port) != MB_ENOERR)
  402. {
  403. log_dbg("%s, eMBTCPInit fail", __func__);
  404. ret = -1;
  405. }
  406. else if (eMBSetSlaveID(MB[MBSIDX_CTN], 0x34, TRUE, ucSlaveID, 3) != MB_ENOERR)
  407. {
  408. log_dbg("%s, eMBSetSlaveID fail", __func__);
  409. ret = -2;
  410. }
  411. else
  412. {
  413. MB[MBSIDX_CTN]->mbsidx = MBSIDX_CTN;
  414. if (pthread_create(&xthrd, NULL, pvPollingThread, MB[MBSIDX_CTN]) != 0)
  415. {
  416. log_dbg("%s, pthread_create fail", __func__);
  417. ret = -3;
  418. }
  419. else
  420. {
  421. log_dbg("%s, start ok", __func__);
  422. }
  423. }
  424. return ret;
  425. }
  426. int mbs_start_EMA(int port)
  427. {
  428. int ret = 0;
  429. const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
  430. pthread_t xthrd;
  431. if (eMBTCPInit(&MB[MBSIDX_EMA], port) != MB_ENOERR)
  432. {
  433. log_dbg("%s, eMBTCPInit fail", __func__);
  434. ret = -1;
  435. }
  436. else if (eMBSetSlaveID(MB[MBSIDX_EMA], 0x34, TRUE, ucSlaveID, 3) != MB_ENOERR)
  437. {
  438. log_dbg("%s, eMBSetSlaveID fail", __func__);
  439. ret = -2;
  440. }
  441. else
  442. {
  443. MB[MBSIDX_EMA]->mbsidx = MBSIDX_EMA;
  444. if (pthread_create(&xthrd, NULL, pvPollingThread, MB[MBSIDX_EMA]) != 0)
  445. {
  446. log_dbg("%s, pthread_create fail", __func__);
  447. ret = -3;
  448. }
  449. else
  450. {
  451. log_dbg("%s, port:%d,start ok", __func__, port);
  452. }
  453. }
  454. return ret;
  455. }