123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- //
- // Created by lloyd on 2020/9/28.
- //
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <cstdlib>
- #include <map>
- #include <iomanip>
- #include "string"
- #include "gtdatasource.h"
- #include "gtrunconf.h"
- #include "modbus.h"
- #include "DataMapping/dgns_gtgktype.h"
- #include "DataMapping/gtdgnsdao.h"
- std::vector<std::vector<double>> gtdatasource::getGTTest() {
- std::string sgt = "000025360010257700102610001026470020269500302744004027840050282700602866007029020090293901002978012030150130305501503097017031370190317802003225022032710240331402603358028034020310344403303490035035380380358704003636042036890450373904703793050038450520389605503945057039940600404506204090065041230670413807004138073041230750410007804080081040720830407208604081088040990910412209404141096041510990415310204146104041351070412010904106112040981140409611704100119041091210412012404130126041381280413913004135133041271350411713704107139041001410409914304102145041091470411814904128150041331520413515404136155041331570412715904119160041131610410816304107164041071650410816604112167041151690411617004116171041161720411517204112173041111740411017404114175041231750414017604155176041711760418317704189177041901770417417704156177041311760409717604055175040181740397717303929172038801710383417003790170037501690371116703672166036301650359116303554162035291600349715903430157033591550329215403218152031521500309914803031146029491440286814102787139027221370266913402615132025661290253712702522125025271220254811902573117025871140258911102578108025611060253910302518100025030970249909402502092025100890252008602530083025370800253607702530075025200720250906902499066024930630249106102492058024960550249905302503050025050470250204502499043024950400248903702484035024840330248203002481028024830260248402402483022024850200248601802483017024830150248501302482012024780100248200902481007024780060248200502489004024860030249100202497001024940010248900002495000024920000248700002489000024900000248000002486000025020000251400002536";
- int unitwidth = 8;
- int sgt_point_num = sgt.size() / unitwidth;
- std::vector<std::vector<double>> 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<<s<<std::endl;
- // std::cout<<l<<std::endl;
- double s_d = atof(s.c_str()) / 100.0;
- double l_d = atof(l.c_str()) / 100.0;
- std::vector<double> 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<<s<<std::endl;
- // std::cout<<l<<std::endl;
- double s_d = atof(s.c_str()) / 100.0;
- double l_d = atof(l.c_str()) / 100.0;
- double point[] = {s_d, l_d};
- gt[i] = (double *) malloc(2 * sizeof(double));
- gt[i][0] = s_d;
- gt[i][1] = l_d;
- }
- return gt;
- }*/
- /*void gtdatasource::getGTTest3(std::string sgtstr) {
- std::string sgt = sgtstr;
- int unitwidth = SGTSTRUNITWIDTH;
- sgtpointnum = sgt.size() / unitwidth;
- gtformatedata = new double *[sgtpointnum];
- for (int i = 0; i < sgtpointnum; i++) {
- std::string s = sgt.substr(i * unitwidth, 3);
- std::string l = sgt.substr(i * unitwidth + 3, 5);
- double s_d = atof(s.c_str()) / 100.0;
- double l_d = atof(l.c_str()) / 100.0;
- double point[] = {s_d, l_d};
- gtformatedata[i] = new double[2];
- gtformatedata[i][0] = s_d;
- gtformatedata[i][1] = l_d;
- }
- N = 1.2;
- //申请空间
- *//* int** a2 = new int*[rows];
- for(int i=0;i<rows;i++)
- a2[i] = new int[columns];*//*
- //释放空间
- *//*for(int i=0;i<rows;i++)
- delete []a2[i];
- delete []a2;*//*
- }*/
- void gtdatasource::getGTFromModbus(dgns_gtbaseparam *gt_data) {
- const char *chIp = rf_modbus_server_ip;
- int nPort = rf_modbus_server_port;
- modbus_t *pmbs_ctx = modbus_new_tcp(chIp, nPort);
- int nRet = modbus_connect(pmbs_ctx);
- if (-1 == nRet) {
- printf("connect failed: %s\n", modbus_strerror(errno));
- modbus_free(pmbs_ctx);
- }
- // 设置Modbus超时时间
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = rf_modbus_server_response_timeout;
- if (rf_modbus_server_response_timeout_setting_enable) //设置modbus超时时间为1000毫秒
- modbus_set_response_timeout(pmbs_ctx, &tv);
- printf("----------------read gt data from modbus----------------------------------\n");
- // 读取保持寄存器的值,起始地址为 ,寄存器个数为 ,读取到 数组中
- uint16_t pointnum;
- modbus_set_slave(pmbs_ctx, 1);
- int regs = modbus_read_registers(pmbs_ctx, 40984, 1, &pointnum);
- printf("所取得功图点数: %d \n", pointnum);
- if (regs != 1) return;
- //printf("gt pointnum: %d \n", pointnum);
- //sgtpointnum = pointnum;
- gt_data->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<std::vector<double>> gtformatedata = new double *[sgtpointnum];
- gt_data->sgt.clear();
- for (int i = 0; i < gt_data->sgtpointnum; i++) {
- std::vector<double> 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<dgns_gtstandard> 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<std::string, dgns_gtgktype> 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<int> 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;
- }
|