TestService.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. #include "TestService.h"
  2. #include "pid.h"
  3. #include "Modbus.h"
  4. #include<QDateTime>
  5. #include<QTime>
  6. #include<QEventLoop>
  7. #include<QMessageBox>
  8. #include<cmath>
  9. #include <QSignalSpy>
  10. //#include <QAxObject>
  11. //#include <QAxWidget>
  12. #include <QDebug>
  13. //#include <QFileDialog>
  14. //#include <QTextCodec>
  15. //#include <QtCore/QtMath>
  16. #include <QFile>
  17. #include <QDir>
  18. TestService::TestService(QThread *parent)
  19. :QThread(parent)
  20. ,m_SerialUi3(nullptr)
  21. ,m_SerialUi4(nullptr)
  22. ,m_pTimer_1(nullptr)
  23. ,m_pTimer_2(nullptr)
  24. ,m_compartmentid(0)
  25. {
  26. m_bRunning=false;
  27. for(int i=0; i<MAX_COMPARTMENT_NUM; i++ ){
  28. m_tank.m_comparts[i].m_id = i;
  29. }
  30. //moveToThread(&m_Thread);
  31. //connect(this, SIGNAL(operate()), this, SLOT(doTest()));
  32. }
  33. TestService::~TestService()
  34. {
  35. qDebug()<<"TestService destory";
  36. m_SerialUi3.closeConnect();
  37. m_SerialUi4.closeConnect();
  38. }
  39. void TestService::init()
  40. {
  41. m_pTimer_1 = new QTimer(this);
  42. connect(m_pTimer_1, SIGNAL(timeout()), this, SLOT(OnTimer1()));
  43. //m_pTimer_2 = new QTimer(this);
  44. // connect(m_pTimer_2, SIGNAL(timeout()), this, SLOT(OnTimer2()));
  45. if(!m_SerialUi3.serialConnect("COM3", "9600", "8", "0", "1")){
  46. QString str = "打开串口COM3失败";
  47. emit onNotice(str);
  48. }
  49. #if 1
  50. if(!m_SerialUi4.serialConnect("COM4", "9600", "8", "2", "1")){
  51. QString str = "打开串口COM4失败";
  52. emit onNotice(str);
  53. }
  54. #endif
  55. connect(&m_report, SIGNAL(onProgress(QString)), this, SLOT(receive_reportprogress(QString)));
  56. }
  57. bool TestService::setTankinfo(const QVariantList& list)
  58. {
  59. QString standard_name;
  60. QString licenseplate_str;
  61. QString companyname_str;
  62. int compartment_num = 0;
  63. int total_volume = 0;
  64. int volume[MAX_COMPARTMENT_NUM] ={0};
  65. for(int i=0; i<list.count(); i++){
  66. QString item_string = list[i].toString();
  67. if(0 == i){
  68. standard_name = item_string;
  69. }else if(1 == i){
  70. licenseplate_str = item_string;
  71. }else if(2 == i){
  72. companyname_str = item_string;
  73. }else if(3 == i){
  74. total_volume = item_string.toInt();
  75. }else if(4 == i){
  76. compartment_num = item_string.toInt();
  77. }else{
  78. if(i< (8+5))
  79. volume[i-5] = item_string.toInt();
  80. }
  81. }
  82. //check invalid
  83. bool ret_bool = true;
  84. if( compartment_num > 0 && compartment_num <= MAX_COMPARTMENT_NUM){
  85. int volume_sum =0;
  86. for(int i=0; i<compartment_num; i++){
  87. volume_sum += volume[i];
  88. }
  89. if(total_volume != volume_sum){
  90. ret_bool = false;
  91. }
  92. }else{
  93. ret_bool = false;
  94. }
  95. if(true == ret_bool){
  96. m_standardname = standard_name;
  97. qDebug("setTankinfo: standard_name:[%s]", standard_name.toStdString().c_str());
  98. m_tank.licenseplate_str = licenseplate_str;
  99. qDebug("setTankinfo: licenseplate:[%s]", licenseplate_str.toStdString().c_str());
  100. m_tank.companyname_str = companyname_str;
  101. qDebug("setTankinfo: companyname:[%s]", companyname_str.toStdString().c_str());
  102. m_tank.total_volume = total_volume;
  103. qDebug("setTankinfo: total_volume:[%d]", total_volume);
  104. m_tank.compartment_num = compartment_num;
  105. qDebug("setTankinfo: compartment_num:[%d]", compartment_num);
  106. Standard standard;
  107. g_StandardManager->getStandard(m_standardname, standard);
  108. for(int i=0; i<compartment_num; i++){
  109. m_tank.m_comparts[i].m_volume = volume[i];
  110. qDebug("setTankinfo: volume [%d]:[%d]", i, volume[i]);
  111. for(int j=0; j<standard.item_count; j++){
  112. if(volume[i] >= standard.item[j].oilcompartment_volume){
  113. m_tank.m_comparts[i].m_standarditem = standard.item[j];
  114. qDebug("setTankinfo: compart [%d]: volume[%d]", i, standard.item[j].oilcompartment_volume);
  115. qDebug("setTankinfo: compart [%d]: sys[%f]", i, standard.item[j].system_threshold);
  116. qDebug("setTankinfo: compart [%d]: valve[%f]", i, standard.item[j].valve_threshold);
  117. }
  118. }
  119. }
  120. }
  121. return ret_bool;
  122. }
  123. bool TestService::tstart(int compartmentid, int direction)
  124. {
  125. if(m_bRunning){
  126. QString str = "当前有任务正在进行中...";
  127. emit onNotice(str);
  128. return false;
  129. }
  130. m_compartmentid = compartmentid-1;
  131. if(0 == direction)
  132. m_direction = Test_Direction_Positive;
  133. else
  134. m_direction = Test_Direction_Negative;
  135. m_stage = Test_Stage_SysTest;
  136. m_step = Test_Step_Start;
  137. m_pressuremutex.lock();
  138. m_pressurelist.clear(); //用来计算平均压力
  139. m_pressuremutex.unlock();
  140. QMessageBox::StandardButton result = QMessageBox::information(NULL, "提示:", "请对油气回收系统相关部件进行检查", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
  141. switch (result)
  142. {
  143. case QMessageBox::Ok:
  144. qDebug()<<"Ok";
  145. m_pTimer_1->start(1*1000); // 1s
  146. m_bRunning = true;
  147. start();
  148. break;
  149. case QMessageBox::Abort:
  150. qDebug()<<"Abort";
  151. return false;
  152. break;
  153. default:
  154. qDebug()<<"default";
  155. return false;
  156. break;
  157. }
  158. return true;
  159. }
  160. bool TestService::tselfstart(int compartmentid, int direction)
  161. {
  162. if(m_bRunning){
  163. QString str = "当前有任务正在进行中...";
  164. emit onNotice(str);
  165. return false;
  166. }
  167. m_compartmentid = compartmentid-1;
  168. if(0 == direction)
  169. m_direction = Test_Direction_Positive;
  170. else
  171. m_direction = Test_Direction_Negative;
  172. m_stage = Test_Stage_SysTest;
  173. m_step = Test_Step_Start;
  174. m_pressuremutex.lock();
  175. m_pressurelist.clear(); //用来计算平均压力
  176. m_pressuremutex.unlock();
  177. QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "系统即将自检,请确认所有部件都己接好", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
  178. switch (result)
  179. {
  180. case QMessageBox::Ok:
  181. qDebug()<<"Ok";
  182. m_pTimer_1->start(1*1000); // 1s
  183. m_bRunning = true;
  184. start();
  185. break;
  186. case QMessageBox::Abort:
  187. qDebug()<<"Abort";
  188. return false;
  189. break;
  190. default:
  191. qDebug()<<"default";
  192. return false;
  193. break;
  194. }
  195. return true;
  196. }
  197. bool TestService::tstop(int compartmentid)
  198. {
  199. if(m_compartmentid != compartmentid-1){
  200. return false;
  201. }
  202. if(m_pTimer_1->isActive())
  203. m_pTimer_1->stop();
  204. //if(m_pTimer_2->isActive())
  205. // m_pTimer_2->stop();
  206. if(m_bRunning){
  207. //stop();
  208. m_bRunning = false;
  209. }
  210. return true;
  211. }
  212. bool TestService::reportpic_Ready(int compartmentid)
  213. {
  214. Compartment& com = m_tank.get_compartment(compartmentid-1);
  215. com.m_bpicReady = true;
  216. return true;
  217. }
  218. bool TestService::check_alltest_ok()
  219. {
  220. bool ret_flag = true;
  221. for(int i=0; i< m_tank.compartment_num; i++){
  222. Compartment& com = m_tank.get_compartment(i);
  223. if(com.m_bpicReady == false ){
  224. ret_flag = false;
  225. break;
  226. }
  227. }
  228. if(false == ret_flag){
  229. QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "有油仓还未进行检测,是否强制生成报告?", QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
  230. switch (result)
  231. {
  232. case QMessageBox::Yes:
  233. ret_flag = true;
  234. break;
  235. case QMessageBox::No:
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. return ret_flag;
  242. }
  243. void TestService::receive_reportprogress(QString qstr)
  244. {
  245. qDebug() << "TestService::receive_reportprogress ";
  246. qDebug() << qstr;
  247. emit onGenreportProgress(qstr);
  248. }
  249. bool TestService::genReport(QString word_path, QString pic_dir)
  250. {
  251. bool ret = false;
  252. if(word_path.contains(".doc") || word_path.contains(".pdf") ){
  253. m_report.makeReport(m_tank, word_path, pic_dir);
  254. }else{
  255. QString str = "word_path invalid!";
  256. qDebug() << str;
  257. }
  258. return ret;
  259. }
  260. QJsonObject TestService::get_vrs_items()
  261. {
  262. return m_tank.get_vrs_items();
  263. }
  264. QJsonObject TestService::get_bls_items()
  265. {
  266. return m_tank.get_bls_items();
  267. }
  268. bool TestService::set_item_result(const QString& jsonStr )
  269. {
  270. return m_tank.set_item_result(jsonStr);
  271. }
  272. unsigned short TestService::Calcrc(const unsigned char *ptr, int count)
  273. {
  274. unsigned short crc;
  275. crc=CRC16_MODBUS((unsigned char *)ptr,count);
  276. return crc;
  277. }
  278. void TestService::SendCmd(QSerialPort* pSerial, QByteArray data)
  279. {
  280. int crc;
  281. //unsigned char buf[128];
  282. crc=Calcrc((unsigned char *)data.data(),data.size());
  283. data.append((crc>>8)&0xff);
  284. data.append(crc&0xff);
  285. if(pSerial==nullptr)
  286. return;
  287. if (pSerial->isOpen())
  288. {
  289. pSerial->write(data);
  290. QString str = QString::fromUtf8("TX: ");
  291. str.append(data.toHex().toUpper());
  292. qDebug("[%s]", str.toStdString().c_str());
  293. }
  294. }
  295. bool TestService::check_crc(QByteArray data)
  296. {
  297. int size = data.size();
  298. unsigned short crc1 =0, crc2= 0;
  299. unsigned char crc_h = data.at(size-2);
  300. //qDebug("crc_h: %x", crc_h);
  301. unsigned char crc_l = data.at(size-1);
  302. // qDebug("crc_l: %x", crc_l);
  303. crc1 = (crc_h <<8) |crc_l;
  304. // qDebug("crc1: %x", crc1);
  305. crc2 = Calcrc((unsigned char *)data.data(),data.size()-2);
  306. // qDebug("crc2: %x", crc2);
  307. if(crc1 == crc2){
  308. return true;
  309. }else{
  310. return false;
  311. }
  312. }
  313. bool TestService::Read_pressure(float& pressure) //FROM COM4
  314. {
  315. QByteArray tx_buf;
  316. //bool ret_flag = true;
  317. char zero = 0x00;
  318. tx_buf.append(PRESSURESENSOR_ADDRESS);
  319. //tx_buf.append(0x03);
  320. tx_buf.append(0x04);
  321. tx_buf.append(zero);
  322. tx_buf.append(zero);
  323. tx_buf.append(zero);
  324. tx_buf.append(0x02);
  325. int crc;
  326. crc=Calcrc((unsigned char *)tx_buf.data(),tx_buf.size());
  327. tx_buf.append((crc>>8)&0xff);
  328. tx_buf.append(crc&0xff);
  329. QByteArray rx_buf;
  330. rx_buf = m_SerialUi4.serialWriteReponse(tx_buf);
  331. if(rx_buf.size() > 0){
  332. if(check_crc(rx_buf)){
  333. char value[4];
  334. value[3] = rx_buf.at(5);
  335. value[2] = rx_buf.at(6);
  336. value[1] = rx_buf.at(3);
  337. value[0] = rx_buf.at(4);
  338. float * p = (float*)value;
  339. pressure = *p;
  340. qDebug("read pressure , the pressure is [%f]", pressure);
  341. push_pressurelist(pressure);
  342. pressure = calculate_averagepressure();
  343. return true;
  344. }else{
  345. //pressure = calculate_averagepressure();
  346. qDebug("Read_pressure <<< check crc failed");
  347. return false;
  348. }
  349. }else{
  350. return false;
  351. }
  352. #if 0
  353. m_Serial4mutex.lock();
  354. SendCmd(m_SerialPort4, tx_buf);
  355. /*
  356. if(!m_SerialPort4->waitForBytesWritten(200)){
  357. qDebug("waitForBytesWritten failed");
  358. m_Serial4mutex.unlock();
  359. return false;
  360. }
  361. */
  362. QSignalSpy spy(m_SerialPort4, SIGNAL(readyRead()));
  363. bool ret = spy.wait(650);
  364. if(ret == true){
  365. QByteArray buffer = m_SerialPort4->readAll();
  366. while (spy.wait(50) == true){
  367. buffer += m_SerialPort4->readAll();
  368. }
  369. if(buffer.size() == 9){
  370. //QString str = QString::fromUtf8("Read_pressure RX: ");
  371. //str.append(buffer.toHex().toUpper());
  372. //qDebug("[%s]", str.toStdString().c_str());
  373. if(!check_crc(buffer)){
  374. pressure = calculate_averagepressure();
  375. ret_flag = false;
  376. }else{
  377. char value[4];
  378. value[3] = buffer.at(5);
  379. value[2] = buffer.at(6);
  380. value[1] = buffer.at(3);
  381. value[0] = buffer.at(4);
  382. float * p = (float*)value;
  383. pressure = *p;
  384. qDebug("read pressure , the pressure is [%f]", pressure);
  385. push_pressurelist(pressure);
  386. pressure = calculate_averagepressure();
  387. }
  388. }else{
  389. QString str = QString::fromUtf8("Read_pressure less than 9 RX: ");
  390. str.append(buffer.toHex().toUpper());
  391. qDebug("[%s]", str.toStdString().c_str());
  392. pressure = calculate_averagepressure();
  393. ret_flag = false;
  394. }
  395. }
  396. else{
  397. qDebug()<<"Read_pressure time out";
  398. pressure = calculate_averagepressure();
  399. ret_flag = false;
  400. }
  401. m_Serial4mutex.unlock();
  402. return ret_flag;
  403. #endif
  404. #if 0
  405. QEventLoop eventLoop;//局部事件循环,不卡
  406. QTimer::singleShot(2000, &eventLoop, &QEventLoop::quit);
  407. QObject::connect(m_SerialPort4, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
  408. eventLoop.exec();
  409. if(m_SerialPort4->bytesAvailable() > 0)
  410. {
  411. QByteArray buffer;
  412. int count = 2;
  413. while (1)
  414. {
  415. buffer += m_SerialPort4->readAll();
  416. QString str = QString::fromUtf8("Read_pressure RX: ");
  417. str.append(buffer.toHex().toUpper());
  418. qDebug("[%s]", str.toStdString().c_str());
  419. if(buffer.size() == 9){
  420. QString str = QString::fromUtf8("Read_pressure RX: ");
  421. str.append(buffer.toHex().toUpper());
  422. qDebug("[%s]", str.toStdString().c_str());
  423. if(!check_crc(buffer)){
  424. return false;
  425. }
  426. char value[4];
  427. value[3] = buffer.at(5);
  428. value[2] = buffer.at(6);
  429. value[1] = buffer.at(3);
  430. value[0] = buffer.at(4);
  431. float * p = (float*)value;
  432. pressure = *p;
  433. qDebug("read pressure , the pressure is [%f]", pressure);
  434. push_pressurelist(pressure);
  435. pressure = calculate_averagepressure();
  436. return true;
  437. }else if(buffer.size() > 9){
  438. QString str = QString::fromUtf8("Read_pressure big than 9 RX: ");
  439. str.append(buffer.toHex().toUpper());
  440. qDebug("[%s]", str.toStdString().c_str());
  441. return false;
  442. }
  443. if(!m_SerialPort3->waitForReadyRead(5)){
  444. count--;
  445. if(count > 0){
  446. break;
  447. }
  448. }
  449. }
  450. QString str = QString::fromUtf8("Read_pressure timeout RX: ");
  451. str.append(buffer.toHex().toUpper());
  452. qDebug("[%s]", str.toStdString().c_str());
  453. return false;
  454. }
  455. else
  456. {
  457. qInfo()<<m_SerialPort4->portName()<<" no answer ";
  458. QObject::disconnect(m_SerialPort4, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
  459. return false;
  460. }
  461. #endif
  462. }
  463. void TestService::push_pressurelist(float pressure)
  464. {
  465. m_pressuremutex.lock();
  466. if(m_pressurelist.size() == AVERAGE_PRESSURE_COUNT){
  467. m_pressurelist.removeAt(0);
  468. }
  469. m_pressurelist.append(pressure);
  470. m_pressuremutex.unlock();
  471. }
  472. void TestService::clear_pressurelist()
  473. {
  474. m_pressuremutex.lock();
  475. m_pressurelist.clear(); //用来计算平均压力
  476. m_pressuremutex.unlock();
  477. }
  478. float TestService::calculate_averagepressure()
  479. {
  480. float average = 0.0;
  481. m_pressuremutex.lock();
  482. int n = m_pressurelist.size();
  483. if(n>0){
  484. float sum = 0.0;
  485. for(int i=0; i<n; i++){
  486. sum += m_pressurelist.at(i);
  487. //qDebug("push_pressurelist 333 size[%f]", m_pressurelist.at(i));
  488. }
  489. average = sum/n;
  490. }
  491. m_pressuremutex.unlock();
  492. return average;
  493. }
  494. bool TestService::Adjust_pressure(unsigned short voltage)
  495. {
  496. QByteArray tx_buf;
  497. //bool ret_flag = true;
  498. //char zero = 0x00;
  499. tx_buf.append(ADJUSTVALVE_ADDRESS);
  500. tx_buf.append(0x06);
  501. tx_buf.append(0x20);
  502. tx_buf.append(0x01);
  503. char c_high = (voltage>>8)&0xff;
  504. char c_low = (voltage)&0xff;
  505. tx_buf.append(c_high);
  506. tx_buf.append(c_low);
  507. int crc;
  508. crc=Calcrc((unsigned char *)tx_buf.data(),tx_buf.size());
  509. tx_buf.append((crc>>8)&0xff);
  510. tx_buf.append(crc&0xff);
  511. QByteArray rx_buf;
  512. rx_buf = m_SerialUi3.serialWriteReponse(tx_buf);
  513. if(rx_buf.size() > 0){
  514. if(check_crc(rx_buf)){
  515. return true;
  516. }else{
  517. return false;
  518. }
  519. }else{
  520. return false;
  521. }
  522. #if 0
  523. m_Serial3mutex.lock();
  524. SendCmd(m_SerialPort3, tx_buf);
  525. /*
  526. if(!m_SerialPort3->waitForBytesWritten(100)){
  527. qDebug("waitForBytesWritten failed");
  528. m_Serial3mutex.unlock();
  529. return false;
  530. }
  531. */
  532. #if 1
  533. QSignalSpy spy(m_SerialPort3, SIGNAL(readyRead()));
  534. bool ret = spy.wait(500);
  535. if(ret == true){
  536. QByteArray buffer;
  537. //while (m_SerialPort3->waitForReadyRead(5))
  538. //{
  539. buffer += m_SerialPort3->readAll();
  540. //QString str = QString::fromUtf8("adjust_pressure RX: ");
  541. //str.append(buffer.toHex().toUpper());
  542. //qDebug("[%s]", str.toStdString().c_str());
  543. if(!check_crc(buffer)){
  544. ret_flag = false;
  545. }
  546. }else
  547. {
  548. qInfo()<<m_SerialPort3->portName()<<" no answer ";
  549. ret_flag = true;
  550. }
  551. m_Serial3mutex.unlock();
  552. return ret_flag;
  553. #else
  554. QEventLoop eventLoop;//局部事件循环,不卡界面
  555. QTimer::singleShot(2000, &eventLoop, &QEventLoop::quit);
  556. QObject::connect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
  557. eventLoop.exec();
  558. if(m_SerialPort3->bytesAvailable() > 0)
  559. {
  560. QByteArray buffer;
  561. //while (m_SerialPort3->waitForReadyRead(5))
  562. //{
  563. buffer += m_SerialPort3->readAll();
  564. QString str = QString::fromUtf8("adjust_pressure RX: ");
  565. str.append(buffer.toHex().toUpper());
  566. qDebug("[%s]", str.toStdString().c_str());
  567. if(check_crc(buffer)){
  568. return true;
  569. }else{
  570. return false;
  571. }
  572. //}
  573. //return true;
  574. }
  575. else
  576. {
  577. qInfo()<<m_SerialPort3->portName()<<" no answer ";
  578. QObject::disconnect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
  579. return false;
  580. }
  581. #endif
  582. #endif
  583. }
  584. bool TestService::Valve_op(Valve_ID id, Valve_OP op)
  585. {
  586. QByteArray tx_buf;
  587. //bool ret_flag = true;
  588. char zero = 0x00;
  589. tx_buf.append(RELAY_ADDRESS);
  590. if(Valve_ID_All == id){
  591. tx_buf.append(0x0F);
  592. tx_buf.append(zero);
  593. tx_buf.append(zero);
  594. tx_buf.append(zero);
  595. tx_buf.append(0x10);
  596. tx_buf.append(0x02);
  597. }else{
  598. tx_buf.append(0x05);
  599. tx_buf.append(zero);
  600. tx_buf.append(static_cast<char>(id));
  601. }
  602. if(Valve_OP_Open == op){
  603. tx_buf.append(0xFF);
  604. tx_buf.append(zero);
  605. }
  606. else{
  607. tx_buf.append(zero);
  608. tx_buf.append(zero);
  609. }
  610. int crc;
  611. crc=Calcrc((unsigned char *)tx_buf.data(),tx_buf.size());
  612. tx_buf.append((crc>>8)&0xff);
  613. tx_buf.append(crc&0xff);
  614. QByteArray rx_buf;
  615. rx_buf = m_SerialUi3.serialWriteReponse(tx_buf);
  616. if(rx_buf.size() > 0){
  617. if(check_crc(rx_buf)){
  618. qDebug("Valve_op ok valve_id[%d], op[%d] ", id, op);
  619. return true;
  620. }else{
  621. qDebug("Valve_op failed valve_id[%d], op[%d] ", id, op);
  622. return false;
  623. }
  624. }else{
  625. qDebug("Valve_op failed valve_id[%d], op[%d] ", id, op);
  626. return false;
  627. }
  628. #if 0
  629. m_Serial3mutex.lock();
  630. SendCmd(m_SerialPort3, tx_buf);
  631. /*
  632. if(!m_SerialPort3->waitForBytesWritten(100)){
  633. qDebug("waitForBytesWritten failed");
  634. m_Serial3mutex.unlock();
  635. return false;
  636. }
  637. */
  638. #if 1
  639. QSignalSpy spy(m_SerialPort3, SIGNAL(readyRead()));
  640. bool ret = spy.wait(500);
  641. if(ret == true){
  642. QByteArray buffer;
  643. //while (m_SerialPort3->waitForReadyRead(5))
  644. //{
  645. buffer += m_SerialPort3->readAll();
  646. //QString str = QString::fromUtf8("adjust_pressure RX: ");
  647. //str.append(buffer.toHex().toUpper());
  648. //qDebug("[%s]", str.toStdString().c_str());
  649. if(!check_crc(buffer)){
  650. ret_flag = false;
  651. }
  652. }else
  653. {
  654. qInfo()<<m_SerialPort3->portName()<<" no answer ";
  655. ret_flag = false;
  656. }
  657. m_Serial3mutex.unlock();
  658. return ret_flag;
  659. #else
  660. QEventLoop eventLoop;//局部事件循环,不卡界面
  661. QTimer::singleShot(2000, &eventLoop, &QEventLoop::quit);
  662. QObject::connect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
  663. eventLoop.exec();
  664. if(m_SerialPort3->bytesAvailable() > 0)
  665. {
  666. QByteArray ba;
  667. //while (m_SerialPort3->waitForReadyRead(5))
  668. //{
  669. ba += m_SerialPort3->readAll();
  670. QString str = QString::fromUtf8("adjust_pressure RX: ");
  671. str.append(ba.toHex().toUpper());
  672. qDebug("[%s]", str.toStdString().c_str());
  673. if(check_crc(ba)){
  674. return true;
  675. }else{
  676. return false;
  677. }
  678. //}
  679. //return true;
  680. }
  681. else
  682. {
  683. qInfo()<<m_SerialPort3->portName()<<" no answer ";
  684. QObject::disconnect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
  685. return false;
  686. }
  687. #endif
  688. #endif
  689. }
  690. void TestService::valve_open(Valve_ID id)
  691. {
  692. qDebug("valve_open [%d]", id);
  693. Valve_op(id, Valve_OP_Open);
  694. }
  695. void TestService::valve_close(Valve_ID id)
  696. {
  697. qDebug("valve_close [%d]", id);
  698. Valve_op(id, Valve_OP_Close);
  699. }
  700. QString TestService::read_PressureValue()
  701. {
  702. float pressure = 0.0;
  703. static int timeout_count =0;
  704. if(Read_pressure(pressure)){
  705. //qDebug("Read_PressureValue [%f]", pressure);
  706. timeout_count=0;
  707. }else{
  708. pressure = calculate_averagepressure();
  709. qDebug("Read_PressureValue failed");
  710. timeout_count++;
  711. if(timeout_count >= AVERAGE_PRESSURE_COUNT){
  712. //data is to old, clear
  713. clear_pressurelist();
  714. qDebug("clear_pressurelist ");
  715. }
  716. }
  717. QString pressure_str = QString("%1").arg(pressure, 5,'f',3,QLatin1Char('0'));
  718. return pressure_str;
  719. }
  720. void TestService::adjust_Pressure(QString voltage_str)
  721. {
  722. bool ok;
  723. ushort voltage = voltage_str.toUShort(&ok, 10);
  724. qDebug("Add_Pressure [%d]", voltage);
  725. Adjust_pressure(voltage);
  726. }
  727. bool TestService::OpenSerial3()
  728. {
  729. m_SerialPort3 = new QSerialPort();
  730. m_SerialPort3->setPortName("COM3");
  731. m_SerialPort3->setBaudRate(9600, QSerialPort::AllDirections);
  732. m_SerialPort3->setDataBits(QSerialPort::Data8);
  733. m_SerialPort3->setParity(QSerialPort::NoParity);
  734. m_SerialPort3->setStopBits(QSerialPort::OneStop);
  735. //m_SerialPort3->setFlowControl(QSerialPort::NoFlowControl);//Set to no flow control
  736. if (m_SerialPort3->open(QIODevice::ReadWrite) == false){
  737. qDebug("open COM3 failed");
  738. //QMessageBox::warning(this, "警告", "打开串口COM3失败");
  739. QString str = "打开串口COM3失败";
  740. emit onNotice(str);
  741. return false;
  742. }
  743. else
  744. {
  745. //controlserial->close();
  746. //connect(m_SerialPort3,SIGNAL(readyRead()),this,SLOT(ReadSerialport3()));
  747. return true;
  748. }
  749. }
  750. bool TestService::OpenSerial4()
  751. {
  752. m_SerialPort4 = new QSerialPort();
  753. m_SerialPort4->setPortName("COM4");
  754. m_SerialPort4->setBaudRate(9600, QSerialPort::AllDirections);
  755. m_SerialPort4->setDataBits(QSerialPort::Data8);
  756. m_SerialPort4->setParity(QSerialPort::EvenParity);
  757. m_SerialPort4->setStopBits(QSerialPort::OneStop);
  758. m_SerialPort4->setFlowControl(QSerialPort::NoFlowControl);//Set to no flow control
  759. if (m_SerialPort4->open(QIODevice::ReadWrite) == false){
  760. qDebug("open COM4 failed");
  761. //QMessageBox::warning(this, "警告", "打开串口COM4失败");
  762. QString str = "打开串口COM4失败,无法读取压力";
  763. emit onNotice(str);
  764. return false;
  765. }
  766. else
  767. {
  768. //readserial->close();
  769. //connect(m_SerialPort4,SIGNAL(readyRead()),this,SLOT(ReadSerialport4()));
  770. return true;
  771. }
  772. }
  773. void TestService::OnTimer1()
  774. {
  775. float pressure = 0.001;
  776. static int timeout_count =0;
  777. if(Read_pressure(pressure)){
  778. //if(false){
  779. //qDebug("Read_PressureValue [%f]", pressure);
  780. timeout_count=0;
  781. }else{
  782. qDebug("Read_PressureValue failed");
  783. pressure = calculate_averagepressure();
  784. timeout_count++;
  785. if(timeout_count >= AVERAGE_PRESSURE_COUNT){
  786. //data is to old, clear
  787. clear_pressurelist();
  788. qDebug("clear_pressurelist ");
  789. }
  790. }
  791. #if 0
  792. if(timeout_count < 180){
  793. m_stage =Test_Stage_SysTest;
  794. m_step = Test_Step_Start;
  795. //pressure = 0.1 * timeout_count;
  796. if(timeout_count > 60){
  797. m_step = Test_Step_KeepPressure;
  798. //pressure = 0.1 * timeout_count;
  799. }
  800. }else if(timeout_count < 360){
  801. if(timeout_count == 180){
  802. m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = 4.5;
  803. m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = 4.3;
  804. m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = 0.2;
  805. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
  806. m_tank.m_comparts[m_compartmentid].m_result.sys_test_ok = true;
  807. }
  808. m_stage =Test_Stage_ValveTest;
  809. m_step = Test_Step_Start;
  810. //pressure = 0.1 * timeout_count;
  811. if(timeout_count > 240){
  812. m_step = Test_Step_KeepPressure;
  813. //pressure = 0.1 * timeout_count;
  814. }
  815. }else {
  816. if(timeout_count == 360){
  817. m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = 4.5;
  818. m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = 3.3;
  819. m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = 1.2;
  820. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
  821. m_tank.m_comparts[m_compartmentid].m_result.valve_test_ok = true;
  822. }
  823. m_step = Test_Step_End;
  824. }
  825. //pressure = 0.1 * timeout_count;
  826. pressure = 3 + 3*sin(M_PI*timeout_count/60);
  827. #endif
  828. int _ipressure = pressure*100;
  829. pressure = _ipressure/100.0;
  830. QString pressure_str = QString("%1").arg(pressure, 4,'f',2,QLatin1Char('0'));
  831. PressureItem item;
  832. //item.m_pressure = pressure_str;
  833. item.m_testDirection = m_direction;
  834. item.m_testStage = m_stage;
  835. item.m_testStep = m_step;
  836. QVariantMap map;
  837. map.clear();
  838. map.insert("pressure", pressure_str);
  839. map.insert("direction", item.m_testDirection);
  840. map.insert("stage", item.m_testStage);
  841. map.insert("step", item.m_testStep);
  842. //QString pressure_str = QString("%1").arg(pressure, 5,'f',3,QLatin1Char('0'));
  843. //m_tank.m_comparts[m_compartmentid].m_currentpressure= pressure_str.toFloat();
  844. emit sigPressure(m_compartmentid+1, map);
  845. }
  846. void TestService::OnTimer2()
  847. {
  848. qDebug("OnTimer2 >>>enter ");
  849. //float pressure;
  850. //Read_pressure(pressure);
  851. QThread::sleep(5);
  852. //emit Pressure_changed(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
  853. qDebug("OnTimer2 >>>leave ");
  854. }
  855. Ret_Value TestService::Pressure_RoughAdjust(const TestExpect& expect)
  856. {
  857. Ret_Value ret_v = Ret_OK;
  858. int keep_count = 0;
  859. bool bOpen1 = false;
  860. bool bOpen2 = false;
  861. bool bOpen3 = false;
  862. //bool inTake_Open = false;
  863. int exceed_times = 0;
  864. int exceed_count = 0;
  865. int delay_count = 0;
  866. QElapsedTimer timer;
  867. timer.start();
  868. float current_pressure;
  869. //等待压力达到目标值附近
  870. while(m_bRunning){
  871. current_pressure = calculate_averagepressure();
  872. if(current_pressure >= expect.target+0.02){
  873. if(0 == exceed_count){
  874. exceed_times++;
  875. }
  876. exceed_count++;
  877. if(bOpen1){
  878. if(Valve_op(Valve_ID_ROUGH_0, Valve_OP_Close))
  879. bOpen1 = false;
  880. }
  881. if(bOpen2){
  882. if(Valve_op(Valve_ID_ROUGH_1, Valve_OP_Close))
  883. bOpen2 = false;
  884. }
  885. if(bOpen3){
  886. if(Valve_op(Valve_ID_ROUGH_2, Valve_OP_Close))
  887. bOpen3 = false;
  888. }
  889. if(exceed_count >= 30){
  890. keep_count = 50;
  891. }
  892. if(exceed_times == 1){
  893. delay_count = 15;
  894. }else if(exceed_times == 2){
  895. delay_count = 10;
  896. }else {
  897. delay_count = 5;
  898. }
  899. //break;
  900. }else if(current_pressure >= expect.target && current_pressure < expect.target+0.02){
  901. //从超出, 回落到该区间
  902. if(exceed_count > 0){
  903. keep_count++;
  904. }
  905. //exceed_count = 0;
  906. }else if(current_pressure < expect.target) {
  907. exceed_count =0;
  908. if(delay_count > 0){
  909. delay_count--;
  910. }
  911. if(0 == delay_count){
  912. if(0 == exceed_times){
  913. if(!bOpen1){
  914. bOpen1 = Valve_op(Valve_ID_ROUGH_0, Valve_OP_Open);
  915. }
  916. if(!bOpen2){
  917. bOpen2 = Valve_op(Valve_ID_ROUGH_1, Valve_OP_Open);
  918. }
  919. if(!bOpen3){
  920. bOpen3 = Valve_op(Valve_ID_ROUGH_2, Valve_OP_Open);
  921. }
  922. }else if(1 == exceed_times){
  923. if(!bOpen1){
  924. bOpen1 = Valve_op(Valve_ID_ROUGH_0, Valve_OP_Open);
  925. }
  926. if(!bOpen2){
  927. bOpen2 = Valve_op(Valve_ID_ROUGH_1, Valve_OP_Open);
  928. }
  929. }else{
  930. if(!bOpen1){
  931. bOpen1 = Valve_op(Valve_ID_ROUGH_0, Valve_OP_Open);
  932. }
  933. }
  934. }
  935. }
  936. if(keep_count >= 30){ //about 60s
  937. break;
  938. }
  939. QThread::msleep(1000);
  940. int remainingtime = expect.timeout*1000 - timer.elapsed();
  941. if(remainingtime <= 0){
  942. qDebug(" add_pressure timeout, Rough valve will colse");
  943. ret_v = Ret_Timeout;
  944. break;
  945. }
  946. }
  947. if(!m_bRunning){
  948. // be stoped
  949. ret_v = Ret_Exit;
  950. }
  951. if(bOpen1){
  952. if(Valve_op(Valve_ID_ROUGH_0, Valve_OP_Close))
  953. bOpen1 = false;
  954. }
  955. if(bOpen2){
  956. if(Valve_op(Valve_ID_ROUGH_1, Valve_OP_Close))
  957. bOpen2 = false;
  958. }
  959. if(bOpen3){
  960. if(Valve_op(Valve_ID_ROUGH_2, Valve_OP_Close))
  961. bOpen3 = false;
  962. }
  963. return ret_v;
  964. }
  965. #if 1
  966. Ret_Value TestService::Pressure_Adjust(const TestExpect& expect)
  967. {
  968. PID pid = PID(1, 20, -10, 5.1, 28.0, 1.0);
  969. double current_pressure;
  970. //double last_pressure;
  971. Ret_Value ret_v = Ret_OK;
  972. int voltage_offset = 0;
  973. int keep_times = 0;
  974. QElapsedTimer timer;
  975. bool inTake_Open = false;
  976. int exceed_times = 0;
  977. int exceed_count = 0;
  978. int delay_count = 0;
  979. inTake_Open = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
  980. timer.start();
  981. while(m_bRunning){
  982. current_pressure = calculate_averagepressure();
  983. if(current_pressure > expect.target + 0.05){
  984. if(exceed_count == 0){
  985. exceed_times++;
  986. }
  987. exceed_count++;
  988. if(inTake_Open == true){
  989. if(Valve_op(Valve_ID_INTAKE, Valve_OP_Close)){
  990. inTake_Open = false;
  991. }
  992. }
  993. if(exceed_count > 15){
  994. keep_times = 20;
  995. }
  996. if(exceed_times == 1){
  997. delay_count = 15;
  998. }else if(exceed_times == 2){
  999. delay_count = 10;
  1000. }else{
  1001. delay_count = 5;
  1002. }
  1003. }else if(current_pressure >= expect.target && current_pressure <= expect.target+0.05){
  1004. if(exceed_count > 0){
  1005. keep_times = 20;
  1006. }
  1007. exceed_count= 0;
  1008. }else{
  1009. exceed_count= 0;
  1010. if(delay_count > 0){
  1011. delay_count--;
  1012. }
  1013. if(0 == delay_count){
  1014. if(false == inTake_Open){
  1015. inTake_Open = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
  1016. }
  1017. double inc = pid.calculate(expect.target, current_pressure);
  1018. voltage_offset = (inc);
  1019. if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
  1020. QThread::msleep(200);
  1021. if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
  1022. qDebug("communication error, ajust_pressure failed");
  1023. ret_v = Ret_Failed;
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. }
  1029. if(keep_times >= 15){
  1030. break;
  1031. }
  1032. QThread::msleep(1000);
  1033. int remainingtime = expect.timeout*1000 - timer.elapsed();
  1034. if(remainingtime < 0){
  1035. ret_v = Ret_Timeout;
  1036. qDebug("timeout, ajust_pressure failed");
  1037. break;
  1038. }
  1039. }
  1040. Valve_op(Valve_ID_INTAKE, Valve_OP_Close);
  1041. Adjust_pressure(0);
  1042. if(!m_bRunning){
  1043. // be stoped
  1044. ret_v = Ret_Exit;
  1045. }
  1046. return ret_v;
  1047. }
  1048. #else
  1049. int TestService::Pressure_Adjust(const TestExpect& expect)
  1050. {
  1051. float current_pressure;
  1052. int keep_times = 0;
  1053. bool bValveOpen = false;
  1054. int add_base = 5;
  1055. int sub_base = 0;
  1056. float delta = 0.0; //当前差值
  1057. keep_times = 0;
  1058. int count_h = 0;
  1059. int count_l =0;
  1060. short voltage_offset = 0;
  1061. QElapsedTimer timer;
  1062. timer.start();
  1063. int ret_value = 0;
  1064. keep_times=0;
  1065. while(m_bRunning){
  1066. current_pressure = calculate_averagepressure();
  1067. if(current_pressure >= expect.target-0.04){
  1068. count_l = 0;
  1069. delta = current_pressure - expect.target;
  1070. if(delta >= 0.08){
  1071. keep_times=0;
  1072. count_h++;
  1073. if(count_h < 30){
  1074. if(bValveOpen){
  1075. if(Valve_op(Valve_ID_INTAKE, Valve_OP_Close))
  1076. bValveOpen = false;
  1077. qDebug("ajust_pressure IntakeValve close return[%d] ", bValveOpen == true? 1:0);
  1078. }
  1079. }
  1080. qDebug("ajust_pressure count_h[%d] ", count_h);
  1081. if(count_h > 30){
  1082. voltage_offset = 0;
  1083. qDebug("ajust_pressure , voltage: %d \n", expect.start_voltage+voltage_offset);
  1084. if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
  1085. QThread::msleep(200);
  1086. if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
  1087. qDebug("communication error, ajust_pressure failed");
  1088. }
  1089. }
  1090. if(false == bValveOpen){
  1091. bValveOpen = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
  1092. qDebug("ajust_pressure IntakeValve open return[%d] ", bValveOpen == true? 1:0);
  1093. }
  1094. }
  1095. }else{
  1096. keep_times++;
  1097. //count_h=0;
  1098. if(count_h > 30){
  1099. if(delta > 0.05){
  1100. if(bValveOpen){
  1101. if(Valve_op(Valve_ID_INTAKE, Valve_OP_Close))
  1102. bValveOpen = false;
  1103. }
  1104. }else if(){
  1105. }
  1106. }
  1107. }
  1108. }else {
  1109. keep_times=0;
  1110. count_h=0;
  1111. count_l++;
  1112. delta = expect.target - current_pressure;
  1113. if(delta > 0.0){
  1114. int k = count_l/30;
  1115. if(voltage_offset != (add_base+k*1)){
  1116. voltage_offset = add_base+k*1;
  1117. //add_base = voltage_offset;
  1118. qDebug("ajust_pressure voltage: %d \n", expect.start_voltage+voltage_offset);
  1119. if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
  1120. QThread::msleep(200);
  1121. if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
  1122. qDebug("communication error, ajust_pressure failed");
  1123. }
  1124. }
  1125. }
  1126. if(false == bValveOpen){
  1127. bValveOpen = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
  1128. qDebug("ajust_pressure IntakeValve open return[%d] ", bValveOpen == true? 1:0);
  1129. }
  1130. }
  1131. }
  1132. qDebug("ajust_pressure keep_times [%d] ", keep_times);
  1133. if(keep_times >= 30){
  1134. break;
  1135. }
  1136. QThread::msleep(500);
  1137. int remainingtime = expect.timeout*1000 - timer.elapsed();
  1138. if(remainingtime < 0){
  1139. ret_value = -1;
  1140. qDebug("timeout, ajust_pressure failed");
  1141. break;
  1142. }
  1143. }
  1144. if(!m_bRunning){
  1145. // be stoped
  1146. return 1;
  1147. }
  1148. return ret_value;
  1149. }
  1150. #endif
  1151. Ret_Value TestService::Pressure_Relief(const TestExpect& expect)
  1152. {
  1153. Ret_Value ret_v = Ret_OK;
  1154. //打开泄压阀
  1155. if(!Valve_op(Valve_ID_VENT, Valve_OP_Open)){
  1156. QThread::msleep(200);
  1157. if(!Valve_op(Valve_ID_VENT, Valve_OP_Open)){
  1158. qDebug("Pressure_Relief faile, valve communicate failed");
  1159. ret_v = Ret_Failed;
  1160. return ret_v;
  1161. }
  1162. }
  1163. //等待压力清零
  1164. float last_pressure = calculate_averagepressure();
  1165. float current_pressure;
  1166. int keep_count = 0;
  1167. while(m_bRunning){
  1168. QThread::msleep(1000);
  1169. current_pressure = calculate_averagepressure();
  1170. if(abs(current_pressure-last_pressure) < 0.02){
  1171. keep_count++;
  1172. if(keep_count > 15){
  1173. break;
  1174. }
  1175. }else{
  1176. keep_count = 0;
  1177. }
  1178. last_pressure = current_pressure;
  1179. }
  1180. //关闭泄压阀
  1181. if(!Valve_op(Valve_ID_VENT, Valve_OP_Close)){
  1182. if(!Valve_op(Valve_ID_VENT, Valve_OP_Close)){
  1183. qDebug("Pressure_Relief faile, valve communicate failed");
  1184. ret_v = Ret_Failed;
  1185. return ret_v;
  1186. }
  1187. }
  1188. if(!m_bRunning){
  1189. ret_v = Ret_Exit;
  1190. }
  1191. return ret_v;
  1192. }
  1193. Ret_Value TestService::Pressure_Relief()
  1194. {
  1195. Ret_Value ret_v = Ret_OK;
  1196. bool bVent_Open = false;
  1197. Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Open);
  1198. //等待压力清零
  1199. float current_pressure;
  1200. int keep_count = 0;
  1201. while(m_bRunning){
  1202. //打开泄压阀
  1203. if(bVent_Open == false){
  1204. bVent_Open = Valve_op(Valve_ID_VENT, Valve_OP_Open);
  1205. }
  1206. QThread::msleep(1000);
  1207. current_pressure = calculate_averagepressure();
  1208. if(abs(current_pressure) < 0.5){
  1209. keep_count++;
  1210. if(keep_count > 10){
  1211. break;
  1212. }
  1213. }else{
  1214. keep_count = 0;
  1215. }
  1216. }
  1217. //关闭泄压阀
  1218. if(bVent_Open == true){
  1219. Valve_op(Valve_ID_VENT, Valve_OP_Close);
  1220. }
  1221. if(!m_bRunning){
  1222. ret_v = Ret_Exit;
  1223. }
  1224. return ret_v;
  1225. }
  1226. Ret_Value TestService::Pressure_Keep(int keeptime, float threshold, float& delta)
  1227. {
  1228. int count = 0;
  1229. Ret_Value ret_v = Ret_OK;
  1230. float start_pressure = calculate_averagepressure();
  1231. while((count < keeptime) && m_bRunning){
  1232. QThread::msleep(1000);
  1233. count++;
  1234. }
  1235. if(!m_bRunning){
  1236. // be stoped
  1237. //return 1;
  1238. ret_v = Ret_Exit;
  1239. }else{
  1240. delta = start_pressure - calculate_averagepressure();
  1241. if(abs(delta) <= threshold){
  1242. ret_v = Ret_OK;
  1243. }else{
  1244. ret_v = Ret_Failed;
  1245. }
  1246. }
  1247. return ret_v;
  1248. }
  1249. int TestService::make_messagebox(Test_Direction direction, Test_Step step, Ret_Value ret_v)
  1250. {
  1251. int ret_btn = QMessageBox::NoButton;
  1252. QString notice_str;
  1253. if(Test_Direction_Positive == direction){
  1254. switch(step){
  1255. case Test_Step_AddPressure:
  1256. if(Ret_Timeout == ret_v){
  1257. notice_str = "加压超时, 是否重试?";
  1258. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1259. }else if(Ret_Failed == ret_v){
  1260. notice_str = "继电器通信失败, 和序终止";
  1261. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1262. }
  1263. break;
  1264. case Test_Step_AdjustPressure:
  1265. if( Ret_Timeout == ret_v ){
  1266. notice_str = "比例阀调压精调超时, 是否重试?";
  1267. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1268. //emit onNotice(notice_str);
  1269. }else if(Ret_Failed == ret_v){
  1270. notice_str = "继电器通信失败, 程序终止";
  1271. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1272. //emit onNotice(notice_str);
  1273. }
  1274. break;
  1275. default:
  1276. break;
  1277. }
  1278. }else {
  1279. }
  1280. return ret_btn;
  1281. }
  1282. void TestService::run()
  1283. {
  1284. qDebug("doTest >>>enter ");
  1285. QString notice_str;
  1286. QElapsedTimer q_time;
  1287. int ret_btn = QMessageBox::NoButton;
  1288. TestExpect texpect;
  1289. bool bQuit = false;
  1290. q_time.start();
  1291. Ret_Value ret_v = Ret_OK;
  1292. #if 0
  1293. while(m_bRunning){
  1294. QThread::sleep(4);
  1295. int ret = m_msgbox.warning(NULL, "提示:", "请打开旋拧阀给系统加压", QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Abort);
  1296. switch (ret)
  1297. {
  1298. case QMessageBox::Ok:
  1299. qDebug()<<"111 OK";
  1300. break;
  1301. case QMessageBox::Retry:
  1302. qDebug()<<"111 Retry";
  1303. break;
  1304. case QMessageBox::Abort:
  1305. qDebug()<<"111 Abort";
  1306. break;
  1307. default:
  1308. qDebug()<<"111 default";
  1309. break;
  1310. }
  1311. }
  1312. #else
  1313. if(m_compartmentid >= 0){
  1314. if(m_direction == Test_Direction_Positive){
  1315. while(m_bRunning && (bQuit == false)){
  1316. switch(m_step){
  1317. case Test_Step_Start:
  1318. {
  1319. m_tank.get_compartment(m_compartmentid).clear_result();
  1320. // close all valves
  1321. Valve_op(Valve_ID_All, Valve_OP_Close);
  1322. ret_btn = m_msgbox.information(NULL, "提示:", "请打开旋拧阀给系统加压", QMessageBox::Ok, QMessageBox::Ok);
  1323. switch (ret_btn)
  1324. {
  1325. case QMessageBox::Ok:
  1326. qDebug()<<"Yes";
  1327. // 打开压力传感阀
  1328. Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Open);
  1329. // 如果容积大于 1000L , 才进入粗调步骤
  1330. if(m_tank.get_compartment(m_compartmentid).m_volume >= 500){
  1331. m_step = Test_Step_AddPressure;
  1332. }else{
  1333. m_step = Test_Step_AdjustPressure;
  1334. }
  1335. break;
  1336. default:
  1337. break;
  1338. }
  1339. }
  1340. break;
  1341. case Test_Step_AddPressure:
  1342. {
  1343. if(m_stage == Test_Stage_SysTest){
  1344. emit sigState(m_compartmentid+1, "回收系统测试,开始加压", "");
  1345. }else{
  1346. emit sigState(m_compartmentid+1, "回收阀测试,开始加压", "");
  1347. }
  1348. texpect.target = 4.50;
  1349. texpect.timeout = m_tank.m_comparts[m_compartmentid].get_Roughadjust_timeout();
  1350. ret_v = Pressure_RoughAdjust(texpect);
  1351. if(Ret_OK == ret_v){
  1352. //m_step = Test_Step_AdjustPressure;
  1353. //m_step = Test_Step_KeepPressure;
  1354. if(m_stage == Test_Stage_SysTest){
  1355. m_step = Test_Step_KeepPressure;
  1356. }else if(m_stage == Test_Stage_ValveTest){
  1357. m_step = Test_Step_reliefPressure;
  1358. }
  1359. }else if(Ret_Exit == ret_v){
  1360. bQuit = true;
  1361. }else{
  1362. ret_btn = make_messagebox(m_direction, m_step, ret_v);
  1363. switch (ret_btn)
  1364. {
  1365. case QMessageBox::Retry:
  1366. qDebug()<<"Retry";
  1367. break;
  1368. case QMessageBox::Abort:
  1369. qDebug()<<"Abort";
  1370. bQuit = true;
  1371. break;
  1372. case QMessageBox::Ok:
  1373. qDebug()<<"OK";
  1374. bQuit = true;
  1375. break;
  1376. default:
  1377. break;
  1378. }
  1379. }
  1380. #if 0
  1381. if(ret_v < 0){
  1382. ret_btn = QMessageBox::NoButton;
  1383. if( -1 == state ){
  1384. notice_str = "加压超时, 是否重试?";
  1385. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1386. //emit onNotice(notice_str);
  1387. }else if(-2 == state){
  1388. notice_str = "继电器读写不成功, 和序终止";
  1389. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1390. //emit onNotice(notice_str);
  1391. }
  1392. qDebug() << notice_str;
  1393. //ret_btn = QMessageBox::information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1394. switch (ret_btn)
  1395. {
  1396. case QMessageBox::Retry:
  1397. qDebug()<<"Retry";
  1398. break;
  1399. case QMessageBox::Abort:
  1400. qDebug()<<"Abort";
  1401. bQuit = true;
  1402. break;
  1403. case QMessageBox::Ok:
  1404. qDebug()<<"OK";
  1405. bQuit = true;
  1406. break;
  1407. default:
  1408. break;
  1409. }
  1410. }else{
  1411. m_step = Test_Step_AdjustPressure;
  1412. }
  1413. #endif
  1414. }
  1415. break;
  1416. case Test_Step_AdjustPressure:
  1417. {
  1418. if(m_stage == Test_Stage_SysTest){
  1419. emit sigState(m_compartmentid+1, "回收系统测试,开始调压", "");
  1420. }else{
  1421. emit sigState(m_compartmentid+1, "回收阀测试,开始调压", "");
  1422. }
  1423. texpect.start_voltage = 175;
  1424. texpect.target = 4.50;
  1425. texpect.timeout = m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
  1426. ret_v = Pressure_Adjust(texpect);
  1427. if(Ret_OK == ret_v){
  1428. if(m_stage == Test_Stage_SysTest){
  1429. m_step = Test_Step_KeepPressure;
  1430. }else if(m_stage == Test_Stage_ValveTest){
  1431. m_step = Test_Step_reliefPressure;
  1432. }
  1433. }else if(Ret_Exit == ret_v){
  1434. bQuit = true;
  1435. }else{
  1436. ret_btn = make_messagebox(m_direction, m_step, ret_v);
  1437. switch (ret_btn)
  1438. {
  1439. case QMessageBox::Retry:
  1440. qDebug()<<"Retry";
  1441. break;
  1442. case QMessageBox::Abort:
  1443. qDebug()<<"Abort";
  1444. bQuit = true;
  1445. break;
  1446. case QMessageBox::Ok:
  1447. qDebug()<<"OK";
  1448. bQuit = true;
  1449. break;
  1450. default:
  1451. break;
  1452. }
  1453. }
  1454. #if 0
  1455. if(state < 0){
  1456. ret_btn = QMessageBox::NoButton;
  1457. if( -1 == state ){
  1458. notice_str = "比例阀调压超时, 是否重试?";
  1459. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1460. //emit onNotice(notice_str);
  1461. }else if(-2 == state){
  1462. notice_str = "继电器读写不成功, 程序终止";
  1463. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1464. //emit onNotice(notice_str);
  1465. }
  1466. qDebug() << notice_str;
  1467. //ret_btn = QMessageBox::information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1468. switch (ret_btn)
  1469. {
  1470. case QMessageBox::Retry:
  1471. qDebug()<<"Retry";
  1472. break;
  1473. case QMessageBox::Abort:
  1474. qDebug()<<"Abort";
  1475. bQuit = true;
  1476. break;
  1477. case QMessageBox::Ok:
  1478. qDebug()<<"OK";
  1479. bQuit = true;
  1480. break;
  1481. default:
  1482. break;
  1483. }
  1484. }else{
  1485. if(m_stage == Test_Stage_SysTest){
  1486. m_step = Test_Step_KeepPressure;
  1487. }else if(m_stage == Test_Stage_ValveTest){
  1488. m_step = Test_Step_reliefPressure;
  1489. }
  1490. }
  1491. #endif
  1492. }
  1493. break;
  1494. case Test_Step_KeepPressure:
  1495. {
  1496. if(m_stage == Test_Stage_SysTest){
  1497. emit sigState(m_compartmentid+1, "回收系统测试,开始保压", "");
  1498. }else{
  1499. emit sigState(m_compartmentid+1, "回收阀测试,开始保压", "");
  1500. }
  1501. // 检测 5分钟都没超限,就是通过了
  1502. float delta = 0;
  1503. float threshold = 0.0;
  1504. bool bPassed = true;
  1505. if(m_stage == Test_Stage_SysTest){
  1506. threshold = m_tank.m_comparts[m_compartmentid].m_standarditem.system_threshold;
  1507. m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = calculate_averagepressure();
  1508. ret_v = Pressure_Keep(5*60, threshold, delta);
  1509. m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = calculate_averagepressure();
  1510. m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = delta;
  1511. if(Ret_Exit == ret_v){
  1512. bQuit = true;
  1513. } else {
  1514. if(Ret_OK == ret_v){
  1515. //正压测试通过
  1516. bPassed = true;
  1517. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
  1518. } else{
  1519. //notice_str = "油气回收系统密封性,测试未通过";
  1520. bPassed = false;
  1521. m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = false;
  1522. }
  1523. m_tank.m_comparts[m_compartmentid].m_result.sys_test_ok = true;
  1524. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1525. emit sigSystemResult(m_compartmentid+1, delta_str, bPassed);
  1526. emit sigState(m_compartmentid+1, "回收系统检测,结束", "");
  1527. ret_btn = QMessageBox::NoButton;
  1528. notice_str = "系统密闭性已经检测完成,是否进行阀门密闭性检测?";
  1529. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1530. switch (ret_btn)
  1531. {
  1532. case QMessageBox::Yes:
  1533. m_stage = Test_Stage_ValveTest;
  1534. if(calculate_averagepressure() < 4.50){
  1535. m_step = Test_Step_AddPressure;
  1536. }else{
  1537. m_step = Test_Step_reliefPressure; //如果压力还在4.5以上, 可以不调压
  1538. }
  1539. break;
  1540. case QMessageBox::No:
  1541. m_step = Test_Step_End;
  1542. break;
  1543. default:
  1544. break;
  1545. }
  1546. }
  1547. }else{
  1548. threshold = m_tank.m_comparts[m_compartmentid].m_standarditem.valve_threshold;
  1549. m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = calculate_averagepressure();
  1550. ret_v = Pressure_Keep(5*60, threshold, delta);
  1551. m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = calculate_averagepressure();
  1552. m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = delta;
  1553. if(Ret_Exit == ret_v){
  1554. bQuit = true;
  1555. }else {
  1556. if(Ret_OK == ret_v){
  1557. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
  1558. bPassed = true;
  1559. }else{
  1560. m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = false;
  1561. bPassed = false;
  1562. }
  1563. m_tank.m_comparts[m_compartmentid].m_result.valve_test_ok = true;
  1564. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1565. emit sigValveResult(m_compartmentid+1, delta_str, bPassed);
  1566. emit sigState(m_compartmentid+1, "回收阀测试,结束", "");
  1567. m_step = Test_Step_End;
  1568. }
  1569. }
  1570. }
  1571. break;
  1572. case Test_Step_reliefPressure:
  1573. {
  1574. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
  1575. switch (ret_btn)
  1576. {
  1577. case QMessageBox::Ok:
  1578. break;
  1579. default:
  1580. break;
  1581. }
  1582. if(m_stage == Test_Stage_SysTest){
  1583. emit sigState(m_compartmentid+1, "油气回收系统测试,开始泄压", "");
  1584. }else{
  1585. emit sigState(m_compartmentid+1, "油气回收阀测试,开始泄压", "");
  1586. }
  1587. texpect.start_voltage = 0;
  1588. texpect.target = 0.0;
  1589. texpect.timeout = 0;//m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
  1590. ret_v = Pressure_Relief(texpect);
  1591. if(Ret_Exit == ret_v){
  1592. bQuit = true;
  1593. }else if(Ret_OK == ret_v){
  1594. m_step = Test_Step_KeepPressure;
  1595. }else{
  1596. ret_btn = QMessageBox::NoButton;
  1597. if(Ret_Timeout == ret_v){
  1598. notice_str = "泄压超时, 是否重试?";
  1599. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1600. }else{
  1601. notice_str = "继电器通信失败, 程序终止";
  1602. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1603. }
  1604. switch (ret_btn)
  1605. {
  1606. case QMessageBox::Retry:
  1607. break;
  1608. case QMessageBox::Abort:
  1609. bQuit = true;
  1610. break;
  1611. case QMessageBox::Ok:
  1612. bQuit = true;
  1613. break;
  1614. default:
  1615. break;
  1616. }
  1617. }
  1618. }
  1619. break;
  1620. default:
  1621. break;
  1622. }
  1623. if(Test_Step_End == m_step){
  1624. break;
  1625. }
  1626. }
  1627. }else{
  1628. //负压测试, 暂不支持
  1629. m_stage = Test_Stage_SysTest;
  1630. qDebug(" negative test is not support now ");
  1631. }
  1632. }else{
  1633. //self test
  1634. if(m_direction == Test_Direction_Positive){
  1635. while(m_bRunning && (bQuit == false)){
  1636. switch(m_step){
  1637. case Test_Step_Start:
  1638. {
  1639. emit sigState(m_compartmentid+1, "开始", "");
  1640. int errors = 0;
  1641. // close all valves
  1642. errors = Valve_op(Valve_ID_All, Valve_OP_Close)? 0:1;
  1643. if(errors > 0){
  1644. ret_btn = m_msgbox.information(NULL, "提示:", "继电器通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
  1645. }else{
  1646. ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀给系统施缓慢加压", QMessageBox::Ok, QMessageBox::Ok);
  1647. }
  1648. switch (ret_btn)
  1649. {
  1650. case QMessageBox::Ok:
  1651. qDebug()<<"Yes";
  1652. // 打开压力传感阀
  1653. Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Open);
  1654. m_step = Test_Step_AdjustPressure;
  1655. break;
  1656. case QMessageBox::Abort:
  1657. bQuit = true;
  1658. break;
  1659. }
  1660. }
  1661. break;
  1662. case Test_Step_AdjustPressure:
  1663. {
  1664. emit sigState(m_compartmentid+1, "调压", "");
  1665. QThread::msleep(2000);
  1666. texpect.start_voltage = 175;
  1667. texpect.target = 4.50;
  1668. texpect.timeout = 10*60; //10分钟
  1669. ret_v = Pressure_Adjust(texpect);
  1670. if(Ret_Exit == ret_v){
  1671. bQuit = true;
  1672. }else if(Ret_OK == ret_v){
  1673. m_step = Test_Step_KeepPressure;
  1674. }else{
  1675. ret_btn = QMessageBox::NoButton;
  1676. if(Ret_Timeout == ret_v){
  1677. notice_str = "比例阀调压超时, 是否重试?";
  1678. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
  1679. }else{
  1680. notice_str = "继电器通信失败, 程序终止";
  1681. ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
  1682. }
  1683. switch (ret_btn)
  1684. {
  1685. case QMessageBox::Retry:
  1686. break;
  1687. case QMessageBox::Abort:
  1688. bQuit = true;
  1689. break;
  1690. case QMessageBox::Ok:
  1691. bQuit = true;
  1692. break;
  1693. default:
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. break;
  1699. case Test_Step_KeepPressure:
  1700. {
  1701. emit sigState(m_compartmentid+1, "保压", "");
  1702. // 检测 5分钟都没超限,就是通过了
  1703. float delta = 0;
  1704. float threshold = 0.6;
  1705. bool bPassed = true;
  1706. ret_v = Pressure_Keep(5*60, threshold, delta);
  1707. if(Ret_Exit == ret_v){
  1708. bQuit = true;
  1709. }else{
  1710. if(Ret_OK == ret_v){
  1711. bPassed = true;
  1712. }else{
  1713. bPassed = false;
  1714. }
  1715. QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
  1716. emit sigSelfTestResult(m_compartmentid+1, delta_str, bPassed);
  1717. emit sigState(m_compartmentid+1, "结束", "");
  1718. m_step = Test_Step_End;
  1719. }
  1720. }
  1721. break;
  1722. default:
  1723. break;
  1724. }
  1725. if(Test_Step_End == m_step){
  1726. break;
  1727. }
  1728. }
  1729. }else{ //negtive
  1730. }
  1731. }
  1732. if(Test_Step_End == m_step){
  1733. ret_btn = m_msgbox.information(NULL, "提示:", "栓测任务结束,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1734. }else{
  1735. if(true == bQuit){
  1736. if(Ret_Exit == ret_v){ // 主动退出
  1737. ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1738. }else if(Ret_Timeout == ret_v){ // 超时退出
  1739. ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1740. }else{//通信错误退出
  1741. ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
  1742. }
  1743. }
  1744. }
  1745. switch (ret_btn)
  1746. {
  1747. case QMessageBox::Yes:
  1748. qDebug()<<"Yes";
  1749. // 打开泄压阀
  1750. //Valve_op(Valve_ID_VENT, Valve_OP_Open);
  1751. Pressure_Relief();
  1752. break;
  1753. default:
  1754. break;
  1755. }
  1756. if(m_bRunning){
  1757. m_bRunning = false;
  1758. emit sigStop(m_compartmentid+1);
  1759. }else{
  1760. }
  1761. // 关闭压力传感阀
  1762. //Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Close);
  1763. // 复位
  1764. Adjust_pressure(0);
  1765. Valve_op(Valve_ID_All, Valve_OP_Close);
  1766. //m_pTimer_1->stop();
  1767. #endif
  1768. qDebug("doTest >>>leave ");
  1769. }