// // Created by lloyd on 2020/9/28. // #include #include #include #include #include #include #include "string" #include "gtdatasource.h" #include "gtrunconf.h" #include "modbus.h" #include "DataMapping/dgns_gtgktype.h" #include "DataMapping/gtdgnsdao.h" std::vector> gtdatasource::getGTTest() { std::string sgt = "000025360010257700102610001026470020269500302744004027840050282700602866007029020090293901002978012030150130305501503097017031370190317802003225022032710240331402603358028034020310344403303490035035380380358704003636042036890450373904703793050038450520389605503945057039940600404506204090065041230670413807004138073041230750410007804080081040720830407208604081088040990910412209404141096041510990415310204146104041351070412010904106112040981140409611704100119041091210412012404130126041381280413913004135133041271350411713704107139041001410409914304102145041091470411814904128150041331520413515404136155041331570412715904119160041131610410816304107164041071650410816604112167041151690411617004116171041161720411517204112173041111740411017404114175041231750414017604155176041711760418317704189177041901770417417704156177041311760409717604055175040181740397717303929172038801710383417003790170037501690371116703672166036301650359116303554162035291600349715903430157033591550329215403218152031521500309914803031146029491440286814102787139027221370266913402615132025661290253712702522125025271220254811902573117025871140258911102578108025611060253910302518100025030970249909402502092025100890252008602530083025370800253607702530075025200720250906902499066024930630249106102492058024960550249905302503050025050470250204502499043024950400248903702484035024840330248203002481028024830260248402402483022024850200248601802483017024830150248501302482012024780100248200902481007024780060248200502489004024860030249100202497001024940010248900002495000024920000248700002489000024900000248000002486000025020000251400002536"; int unitwidth = 8; int sgt_point_num = sgt.size() / unitwidth; std::vector> gt;//[sgt_point_num]; std::cout << "num1: " << sgt_point_num << std::endl; for (int i = 0; i < sgt_point_num; i++) { std::string s = sgt.substr(i * unitwidth, 3); std::string l = sgt.substr(i * unitwidth + 3, 5); // std::cout< point; point.push_back(s_d); point.push_back(l_d); gt.push_back(point); } return gt; } /*double **gtdatasource::getGTTest2() { std::string sgt = "000025360010257700102610001026470020269500302744004027840050282700602866007029020090293901002978012030150130305501503097017031370190317802003225022032710240331402603358028034020310344403303490035035380380358704003636042036890450373904703793050038450520389605503945057039940600404506204090065041230670413807004138073041230750410007804080081040720830407208604081088040990910412209404141096041510990415310204146104041351070412010904106112040981140409611704100119041091210412012404130126041381280413913004135133041271350411713704107139041001410409914304102145041091470411814904128150041331520413515404136155041331570412715904119160041131610410816304107164041071650410816604112167041151690411617004116171041161720411517204112173041111740411017404114175041231750414017604155176041711760418317704189177041901770417417704156177041311760409717604055175040181740397717303929172038801710383417003790170037501690371116703672166036301650359116303554162035291600349715903430157033591550329215403218152031521500309914803031146029491440286814102787139027221370266913402615132025661290253712702522125025271220254811902573117025871140258911102578108025611060253910302518100025030970249909402502092025100890252008602530083025370800253607702530075025200720250906902499066024930630249106102492058024960550249905302503050025050470250204502499043024950400248903702484035024840330248203002481028024830260248402402483022024850200248601802483017024830150248501302482012024780100248200902481007024780060248200502489004024860030249100202497001024940010248900002495000024920000248700002489000024900000248000002486000025020000251400002536"; int unitwidth = 8; int sgt_point_num = sgt.size() / unitwidth; double **gt; gt = (double **) malloc(sgt_point_num * sizeof(double *)); //std::cout << "num1: " << sgt_point_num << std::endl; for (int i = 0; i < sgt_point_num; i++) { std::string s = sgt.substr(i * unitwidth, 3); std::string l = sgt.substr(i * unitwidth + 3, 5); // std::cout<sgtpointnum = pointnum; uint16_t timebyte[5]; modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 40985, 6, timebyte); if (regs != 6) return; //const char *gtdate = "12453";// 获取的时间有bug /*printf("%04X-%02X-%02X %02X:%02X:%02X \n", timebyte[0], timebyte[1], timebyte[2], timebyte[3], timebyte[4], timebyte[5]);*/ std::stringstream ss; ss << std::hex << timebyte[0] << "-" << std::setw(2) << std::setfill('0') << std::hex << timebyte[1] << "-" << std::setw(2) << std::setfill('0') << std::hex << timebyte[2] << " " << std::setw(2) << std::setfill('0') << std::hex << timebyte[3] << ":" << std::setw(2) << std::setfill('0') << std::hex << timebyte[4] << ":" << std::setw(2) << std::setfill('0') << std::hex << timebyte[5]; // ss>>gt_data->recivetime; gt_data->recivetime = ss.str(); /* gtprocdate = std::to_string(timebyte[0]) + "-" + std::to_string(timebyte[1]) + "-" + std::to_string(timebyte[2]) + " " + std::to_string(timebyte[3]) + ":" + std::to_string(timebyte[4]) + ":" + std::to_string(timebyte[5]);//std::string (gtdate);*/ uint16_t chongci; modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 40991, 1, &chongci); if (regs != 1) return; //printf("gt chongci: %5.2f \n", 60.0 / (chongci / 1000.0)); // N = 60.0 / (chongci / 1000.0); gt_data->n = 60.0 / (chongci / 1000.0); uint16_t chongcheng; modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 40993, 1, &chongcheng); if (regs != 1) return; //printf("gt chongcheng: %5.2f \n", chongcheng / 100.0); // S = chongcheng / 100.0; gt_data->s_pre = chongcheng / 100.0; uint16_t WeiYi[250]; uint16_t ZaiHe[250]; //weiyi modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 41001, 125, &WeiYi[0]); if (regs != 125) return; modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 41126, 125, &WeiYi[125]); if (regs != 125) return; //zaihe modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 41251, 125, &ZaiHe[0]); if (regs != 125) return; modbus_set_slave(pmbs_ctx, 1); regs = modbus_read_registers(pmbs_ctx, 41376, 125, &ZaiHe[125]); if (regs != 125) return; //printf("gt data :"); //std::vector> gtformatedata = new double *[sgtpointnum]; gt_data->sgt.clear(); for (int i = 0; i < gt_data->sgtpointnum; i++) { std::vector p; p.push_back(WeiYi[i] / 100.0); p.push_back(ZaiHe[i] / 100.0); gt_data->sgt.push_back(p); //printf("[%2.2lf,%5.2lf]\t", p[0], p[1]); } printf("\n"); printf("--------------------------------------------------\n"); // 关闭modbus连接 modbus_close(pmbs_ctx); //释放modbus资源,使用完libmodbus需要释放掉 modbus_free(pmbs_ctx); } void gtdatasource::get_std_gt(dgns_gtbaseparam >_data, std::string sgtprodate) { //gt_data_std=NULL; //gt_data=NULL; /* std::vector ls=dao.get_dgns_gtstandard_list(); if(ls.empty()||ls.size()==0) return; gt_data_std=ls[0];*/ if (sgtprodate.empty()) return; gtdgnsdao::get_dgns_gtbaseparam_single(gt_data, "", sgtprodate); return; //have completed !!! in 2020-11-19 /** * 1、从PG数据库中获取到 标准功图 ok using gtdgnsdao::get_dgns_gtstandard_list() * 2 根据获取到的标准功图的时间,查得所需的gtbaseparam * 1 not null and 2 not null * 1 NULL and 2 NULL */ } /** * 标准工况类型编码后内容输入到modbus server * @param buff 工况既定标准输出编码数组 * @param buffnum 个数 * @return */ bool gtdatasource::write_gtcvr_out_content_to_modbus_tcp_server(uint16_t *buff, unsigned int buffnum) { //TODO:完成工况类型标准输入modbus server--测试还没有通过 //like :::: //int nRet = modbus_write_registers(pmbs_ctx, 42001, 5, tmpbuff); if ((buff == NULL) || (buffnum < 1)) return false; int n = 0; modbus_t *mb; mb = modbus_new_tcp(rf_modbus_server_ip, rf_modbus_server_port); modbus_connect(mb); /* Read 5 registers from the address 0 */ n = modbus_write_registers(mb, 375, buffnum, buff); return n > 0;//返回写入成功与否 } /** * 标准工况类型编码后内容输入到modbus server * @param buff 工况既定标准输出编码数组 using vector as container * @return */ bool gtdatasource::write_gtcvr_out_content_to_modbus_tcp_server(uint16_t buff) { //TODO:完成工况类型标准输入modbus server--测试还没有通过 //like :::: //int nRet = modbus_write_registers(pmbs_ctx, 42001, 5, tmpbuff); if ((buff < 1)) return false; int n = 0; modbus_t *mb; mb = modbus_new_tcp(rf_modbus_server_ip, rf_modbus_server_port); modbus_connect(mb); /* Read 5 registers from the address 0 */ n = modbus_write_register(mb, 375, buff); return n > 0;//返回写入成功与否 } bool gtdatasource::send_gtcvr_out_content_to_modbuscode_rf(std::string gk_name_CH) { std::map gktypes_; dgns_gtgktype type_gkout; bool r = false; if (gtdgnsdao::get_dgns_gtgktype_single(type_gkout, -1, gk_name_CH)) { int s = 0; std::vector buffcodes; std::string code_str; for (int i = 0; i < type_gkout.modbustranscode.length(); i++) { //将字符串分割成字符串数组 if (type_gkout.modbustranscode[i] == '[') { //以空格作为分隔符 continue; } if (type_gkout.modbustranscode[i] == ']') { if (!code_str.empty()) buffcodes.push_back(std::atoi(code_str.c_str())); code_str = ""; continue; } code_str += type_gkout.modbustranscode[i]; //将分割好的字符串放到K数组里 } uint16_t *buff = new uint16_t[buffcodes.size()]; for (int i = 0; i < buffcodes.size(); ++i) { buff[i] = buffcodes[i]; } r = write_gtcvr_out_content_to_modbus_tcp_server(buff, buffcodes.size()); } return r; } bool gtdatasource::write_gtaoid_out_singlegtproc_to_modbus_tcp_server(uint16_t buff) { //TODO:完成工况类型标准输入modbus server--测试还没有通过 //like :::: //int nRet = modbus_write_registers(pmbs_ctx, 42001, 5, tmpbuff); if ((buff < 1)) return false; int n = 0; modbus_t *mb; mb = modbus_new_tcp(rf_modbus_server_ip, rf_modbus_server_port); modbus_connect(mb); /* Read 5 registers from the address 0 */ n = modbus_write_register(mb, 385, buff); return n > 0;//返回写入成功与否 } bool gtdatasource::send_gtaoid_out_singlegtproc_to_modbuscode_rf(double liqproc,int gtnumdaily) { bool r = false; uint16_t buff=(int)(liqproc/gtly_val_effect_hours*(1/(gtnumdaily/24))*std::pow(10,gtly_val_to_rf_RTU_SD)); r = write_gtaoid_out_singlegtproc_to_modbus_tcp_server(buff); return r; }