123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229 |
- #include "TestService.h"
- #include "pid.h"
- #include "Modbus.h"
- #include<QDateTime>
- #include<QTime>
- #include<QEventLoop>
- #include<QMessageBox>
- #include<cmath>
- #include <QSignalSpy>
- //#include <QAxObject>
- //#include <QAxWidget>
- #include <QDebug>
- //#include <QFileDialog>
- //#include <QTextCodec>
- //#include <QtCore/QtMath>
- #include <QFile>
- #include <QDir>
- TestService::TestService(QThread *parent)
- :QThread(parent)
- ,m_SerialUi3(nullptr)
- ,m_SerialUi4(nullptr)
- ,m_pTimer_1(nullptr)
- ,m_pTimer_2(nullptr)
- ,m_compartmentid(0)
- {
- m_bRunning=false;
- for(int i=0; i<MAX_COMPARTMENT_NUM; i++ ){
- m_tank.m_comparts[i].m_id = i;
- }
- //moveToThread(&m_Thread);
- //connect(this, SIGNAL(operate()), this, SLOT(doTest()));
- }
- TestService::~TestService()
- {
- qDebug()<<"TestService destory";
- m_SerialUi3.closeConnect();
- m_SerialUi4.closeConnect();
- }
- void TestService::init()
- {
- m_pTimer_1 = new QTimer(this);
- connect(m_pTimer_1, SIGNAL(timeout()), this, SLOT(OnTimer1()));
- //m_pTimer_2 = new QTimer(this);
- // connect(m_pTimer_2, SIGNAL(timeout()), this, SLOT(OnTimer2()));
- if(!m_SerialUi3.serialConnect("COM3", "9600", "8", "0", "1")){
- QString str = "打开串口COM3失败";
- emit onNotice(str);
- }
- #if 1
- if(!m_SerialUi4.serialConnect("COM4", "9600", "8", "2", "1")){
- QString str = "打开串口COM4失败";
- emit onNotice(str);
- }
- #endif
- connect(&m_report, SIGNAL(onProgress(QString)), this, SLOT(receive_reportprogress(QString)));
- }
- bool TestService::setTankinfo(const QVariantList& list)
- {
- QString standard_name;
- QString licenseplate_str;
- QString companyname_str;
- int compartment_num = 0;
- int total_volume = 0;
- int volume[MAX_COMPARTMENT_NUM] ={0};
- for(int i=0; i<list.count(); i++){
- QString item_string = list[i].toString();
- if(0 == i){
- standard_name = item_string;
- }else if(1 == i){
- licenseplate_str = item_string;
- }else if(2 == i){
- companyname_str = item_string;
- }else if(3 == i){
- total_volume = item_string.toInt();
- }else if(4 == i){
- compartment_num = item_string.toInt();
- }else{
- if(i< (8+5))
- volume[i-5] = item_string.toInt();
- }
- }
- //check invalid
- bool ret_bool = true;
- if( compartment_num > 0 && compartment_num <= MAX_COMPARTMENT_NUM){
- int volume_sum =0;
- for(int i=0; i<compartment_num; i++){
- volume_sum += volume[i];
- }
- if(total_volume != volume_sum){
- ret_bool = false;
- }
- }else{
- ret_bool = false;
- }
- if(true == ret_bool){
- m_standardname = standard_name;
- qDebug("setTankinfo: standard_name:[%s]", standard_name.toStdString().c_str());
- m_tank.licenseplate_str = licenseplate_str;
- qDebug("setTankinfo: licenseplate:[%s]", licenseplate_str.toStdString().c_str());
- m_tank.companyname_str = companyname_str;
- qDebug("setTankinfo: companyname:[%s]", companyname_str.toStdString().c_str());
- m_tank.total_volume = total_volume;
- qDebug("setTankinfo: total_volume:[%d]", total_volume);
- m_tank.compartment_num = compartment_num;
- qDebug("setTankinfo: compartment_num:[%d]", compartment_num);
- Standard standard;
- g_StandardManager->getStandard(m_standardname, standard);
- for(int i=0; i<compartment_num; i++){
- m_tank.m_comparts[i].m_volume = volume[i];
- qDebug("setTankinfo: volume [%d]:[%d]", i, volume[i]);
- for(int j=0; j<standard.item_count; j++){
- if(volume[i] >= standard.item[j].oilcompartment_volume){
- m_tank.m_comparts[i].m_standarditem = standard.item[j];
- qDebug("setTankinfo: compart [%d]: volume[%d]", i, standard.item[j].oilcompartment_volume);
- qDebug("setTankinfo: compart [%d]: sys[%f]", i, standard.item[j].system_threshold);
- qDebug("setTankinfo: compart [%d]: valve[%f]", i, standard.item[j].valve_threshold);
- }
- }
- }
- }
- return ret_bool;
- }
- bool TestService::tstart(int compartmentid, int direction)
- {
- if(m_bRunning){
- QString str = "当前有任务正在进行中...";
- emit onNotice(str);
- return false;
- }
- m_compartmentid = compartmentid-1;
- if(0 == direction)
- m_direction = Test_Direction_Positive;
- else
- m_direction = Test_Direction_Negative;
- m_stage = Test_Stage_SysTest;
- m_step = Test_Step_Start;
- m_pressuremutex.lock();
- m_pressurelist.clear(); //用来计算平均压力
- m_pressuremutex.unlock();
- QMessageBox::StandardButton result = QMessageBox::information(NULL, "提示:", "请对油气回收系统相关部件进行检查", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
- switch (result)
- {
- case QMessageBox::Ok:
- qDebug()<<"Ok";
- m_pTimer_1->start(1*1000); // 1s
- m_bRunning = true;
- start();
- break;
- case QMessageBox::Abort:
- qDebug()<<"Abort";
- return false;
- break;
- default:
- qDebug()<<"default";
- return false;
- break;
- }
- return true;
- }
- bool TestService::tselfstart(int compartmentid, int direction)
- {
- if(m_bRunning){
- QString str = "当前有任务正在进行中...";
- emit onNotice(str);
- return false;
- }
- m_compartmentid = compartmentid-1;
- if(0 == direction)
- m_direction = Test_Direction_Positive;
- else
- m_direction = Test_Direction_Negative;
- m_stage = Test_Stage_SysTest;
- m_step = Test_Step_Start;
- m_pressuremutex.lock();
- m_pressurelist.clear(); //用来计算平均压力
- m_pressuremutex.unlock();
- QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "系统即将自检,请确认所有部件都己接好", QMessageBox::Ok|QMessageBox::Abort, QMessageBox::Ok);
- switch (result)
- {
- case QMessageBox::Ok:
- qDebug()<<"Ok";
- m_pTimer_1->start(1*1000); // 1s
- m_bRunning = true;
- start();
- break;
- case QMessageBox::Abort:
- qDebug()<<"Abort";
- return false;
- break;
- default:
- qDebug()<<"default";
- return false;
- break;
- }
- return true;
- }
- bool TestService::tstop(int compartmentid)
- {
- if(m_compartmentid != compartmentid-1){
- return false;
- }
- if(m_pTimer_1->isActive())
- m_pTimer_1->stop();
- //if(m_pTimer_2->isActive())
- // m_pTimer_2->stop();
- if(m_bRunning){
- //stop();
- m_bRunning = false;
- }
- return true;
- }
- bool TestService::reportpic_Ready(int compartmentid)
- {
- Compartment& com = m_tank.get_compartment(compartmentid-1);
- com.m_bpicReady = true;
- return true;
- }
- bool TestService::check_alltest_ok()
- {
- bool ret_flag = true;
- for(int i=0; i< m_tank.compartment_num; i++){
- Compartment& com = m_tank.get_compartment(i);
- if(com.m_bpicReady == false ){
- ret_flag = false;
- break;
- }
- }
- if(false == ret_flag){
- QMessageBox::StandardButton result = QMessageBox::question(NULL, "提示:", "有油仓还未进行检测,是否强制生成报告?", QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
- switch (result)
- {
- case QMessageBox::Yes:
- ret_flag = true;
- break;
- case QMessageBox::No:
- break;
- default:
- break;
- }
- }
- return ret_flag;
- }
- void TestService::receive_reportprogress(QString qstr)
- {
- qDebug() << "TestService::receive_reportprogress ";
- qDebug() << qstr;
- emit onGenreportProgress(qstr);
- }
- bool TestService::genReport(QString word_path, QString pic_dir)
- {
- bool ret = false;
- if(word_path.contains(".doc") || word_path.contains(".pdf") ){
- m_report.makeReport(m_tank, word_path, pic_dir);
- }else{
- QString str = "word_path invalid!";
- qDebug() << str;
- }
- return ret;
- }
- QJsonObject TestService::get_vrs_items()
- {
- return m_tank.get_vrs_items();
- }
- QJsonObject TestService::get_bls_items()
- {
- return m_tank.get_bls_items();
- }
- bool TestService::set_item_result(const QString& jsonStr )
- {
- return m_tank.set_item_result(jsonStr);
- }
- unsigned short TestService::Calcrc(const unsigned char *ptr, int count)
- {
- unsigned short crc;
- crc=CRC16_MODBUS((unsigned char *)ptr,count);
- return crc;
- }
- void TestService::SendCmd(QSerialPort* pSerial, QByteArray data)
- {
- int crc;
- //unsigned char buf[128];
- crc=Calcrc((unsigned char *)data.data(),data.size());
- data.append((crc>>8)&0xff);
- data.append(crc&0xff);
- if(pSerial==nullptr)
- return;
- if (pSerial->isOpen())
- {
- pSerial->write(data);
- QString str = QString::fromUtf8("TX: ");
- str.append(data.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- }
- }
- bool TestService::check_crc(QByteArray data)
- {
- int size = data.size();
- unsigned short crc1 =0, crc2= 0;
- unsigned char crc_h = data.at(size-2);
- //qDebug("crc_h: %x", crc_h);
- unsigned char crc_l = data.at(size-1);
- // qDebug("crc_l: %x", crc_l);
- crc1 = (crc_h <<8) |crc_l;
- // qDebug("crc1: %x", crc1);
- crc2 = Calcrc((unsigned char *)data.data(),data.size()-2);
- // qDebug("crc2: %x", crc2);
- if(crc1 == crc2){
- return true;
- }else{
- return false;
- }
- }
- bool TestService::Read_pressure(float& pressure) //FROM COM4
- {
- QByteArray tx_buf;
- //bool ret_flag = true;
- char zero = 0x00;
- tx_buf.append(PRESSURESENSOR_ADDRESS);
- //tx_buf.append(0x03);
- tx_buf.append(0x04);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(0x02);
- int crc;
- crc=Calcrc((unsigned char *)tx_buf.data(),tx_buf.size());
- tx_buf.append((crc>>8)&0xff);
- tx_buf.append(crc&0xff);
- QByteArray rx_buf;
- rx_buf = m_SerialUi4.serialWriteReponse(tx_buf);
- if(rx_buf.size() > 0){
- if(check_crc(rx_buf)){
- char value[4];
- value[3] = rx_buf.at(5);
- value[2] = rx_buf.at(6);
- value[1] = rx_buf.at(3);
- value[0] = rx_buf.at(4);
- float * p = (float*)value;
- pressure = *p;
- qDebug("read pressure , the pressure is [%f]", pressure);
- push_pressurelist(pressure);
- pressure = calculate_averagepressure();
- return true;
- }else{
- //pressure = calculate_averagepressure();
- qDebug("Read_pressure <<< check crc failed");
- return false;
- }
- }else{
- return false;
- }
- #if 0
- m_Serial4mutex.lock();
- SendCmd(m_SerialPort4, tx_buf);
- /*
- if(!m_SerialPort4->waitForBytesWritten(200)){
- qDebug("waitForBytesWritten failed");
- m_Serial4mutex.unlock();
- return false;
- }
- */
- QSignalSpy spy(m_SerialPort4, SIGNAL(readyRead()));
- bool ret = spy.wait(650);
- if(ret == true){
- QByteArray buffer = m_SerialPort4->readAll();
- while (spy.wait(50) == true){
- buffer += m_SerialPort4->readAll();
- }
- if(buffer.size() == 9){
- //QString str = QString::fromUtf8("Read_pressure RX: ");
- //str.append(buffer.toHex().toUpper());
- //qDebug("[%s]", str.toStdString().c_str());
- if(!check_crc(buffer)){
- pressure = calculate_averagepressure();
- ret_flag = false;
- }else{
- char value[4];
- value[3] = buffer.at(5);
- value[2] = buffer.at(6);
- value[1] = buffer.at(3);
- value[0] = buffer.at(4);
- float * p = (float*)value;
- pressure = *p;
- qDebug("read pressure , the pressure is [%f]", pressure);
- push_pressurelist(pressure);
- pressure = calculate_averagepressure();
- }
- }else{
- QString str = QString::fromUtf8("Read_pressure less than 9 RX: ");
- str.append(buffer.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- pressure = calculate_averagepressure();
- ret_flag = false;
- }
- }
- else{
- qDebug()<<"Read_pressure time out";
- pressure = calculate_averagepressure();
- ret_flag = false;
- }
- m_Serial4mutex.unlock();
- return ret_flag;
- #endif
- #if 0
- QEventLoop eventLoop;//局部事件循环,不卡
- QTimer::singleShot(2000, &eventLoop, &QEventLoop::quit);
- QObject::connect(m_SerialPort4, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
- eventLoop.exec();
- if(m_SerialPort4->bytesAvailable() > 0)
- {
- QByteArray buffer;
- int count = 2;
- while (1)
- {
- buffer += m_SerialPort4->readAll();
- QString str = QString::fromUtf8("Read_pressure RX: ");
- str.append(buffer.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- if(buffer.size() == 9){
- QString str = QString::fromUtf8("Read_pressure RX: ");
- str.append(buffer.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- if(!check_crc(buffer)){
- return false;
- }
- char value[4];
- value[3] = buffer.at(5);
- value[2] = buffer.at(6);
- value[1] = buffer.at(3);
- value[0] = buffer.at(4);
- float * p = (float*)value;
- pressure = *p;
- qDebug("read pressure , the pressure is [%f]", pressure);
- push_pressurelist(pressure);
- pressure = calculate_averagepressure();
- return true;
- }else if(buffer.size() > 9){
- QString str = QString::fromUtf8("Read_pressure big than 9 RX: ");
- str.append(buffer.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- return false;
- }
- if(!m_SerialPort3->waitForReadyRead(5)){
- count--;
- if(count > 0){
- break;
- }
- }
- }
- QString str = QString::fromUtf8("Read_pressure timeout RX: ");
- str.append(buffer.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- return false;
- }
- else
- {
- qInfo()<<m_SerialPort4->portName()<<" no answer ";
- QObject::disconnect(m_SerialPort4, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
- return false;
- }
- #endif
- }
- void TestService::push_pressurelist(float pressure)
- {
- m_pressuremutex.lock();
- if(m_pressurelist.size() == AVERAGE_PRESSURE_COUNT){
- m_pressurelist.removeAt(0);
- }
- m_pressurelist.append(pressure);
- m_pressuremutex.unlock();
- }
- void TestService::clear_pressurelist()
- {
- m_pressuremutex.lock();
- m_pressurelist.clear(); //用来计算平均压力
- m_pressuremutex.unlock();
- }
- float TestService::calculate_averagepressure()
- {
- float average = 0.0;
- m_pressuremutex.lock();
- int n = m_pressurelist.size();
- if(n>0){
- float sum = 0.0;
- for(int i=0; i<n; i++){
- sum += m_pressurelist.at(i);
- //qDebug("push_pressurelist 333 size[%f]", m_pressurelist.at(i));
- }
- average = sum/n;
- }
- m_pressuremutex.unlock();
- return average;
- }
- bool TestService::Adjust_pressure(unsigned short voltage)
- {
- QByteArray tx_buf;
- //bool ret_flag = true;
- //char zero = 0x00;
- tx_buf.append(ADJUSTVALVE_ADDRESS);
- tx_buf.append(0x06);
- tx_buf.append(0x20);
- tx_buf.append(0x01);
- char c_high = (voltage>>8)&0xff;
- char c_low = (voltage)&0xff;
- tx_buf.append(c_high);
- tx_buf.append(c_low);
- int crc;
- crc=Calcrc((unsigned char *)tx_buf.data(),tx_buf.size());
- tx_buf.append((crc>>8)&0xff);
- tx_buf.append(crc&0xff);
- QByteArray rx_buf;
- rx_buf = m_SerialUi3.serialWriteReponse(tx_buf);
- if(rx_buf.size() > 0){
- if(check_crc(rx_buf)){
- return true;
- }else{
- return false;
- }
- }else{
- return false;
- }
- #if 0
- m_Serial3mutex.lock();
- SendCmd(m_SerialPort3, tx_buf);
- /*
- if(!m_SerialPort3->waitForBytesWritten(100)){
- qDebug("waitForBytesWritten failed");
- m_Serial3mutex.unlock();
- return false;
- }
- */
- #if 1
- QSignalSpy spy(m_SerialPort3, SIGNAL(readyRead()));
- bool ret = spy.wait(500);
- if(ret == true){
- QByteArray buffer;
- //while (m_SerialPort3->waitForReadyRead(5))
- //{
- buffer += m_SerialPort3->readAll();
- //QString str = QString::fromUtf8("adjust_pressure RX: ");
- //str.append(buffer.toHex().toUpper());
- //qDebug("[%s]", str.toStdString().c_str());
- if(!check_crc(buffer)){
- ret_flag = false;
- }
- }else
- {
- qInfo()<<m_SerialPort3->portName()<<" no answer ";
- ret_flag = true;
- }
- m_Serial3mutex.unlock();
- return ret_flag;
- #else
- QEventLoop eventLoop;//局部事件循环,不卡界面
- QTimer::singleShot(2000, &eventLoop, &QEventLoop::quit);
- QObject::connect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
- eventLoop.exec();
- if(m_SerialPort3->bytesAvailable() > 0)
- {
- QByteArray buffer;
- //while (m_SerialPort3->waitForReadyRead(5))
- //{
- buffer += m_SerialPort3->readAll();
- QString str = QString::fromUtf8("adjust_pressure RX: ");
- str.append(buffer.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- if(check_crc(buffer)){
- return true;
- }else{
- return false;
- }
- //}
- //return true;
- }
- else
- {
- qInfo()<<m_SerialPort3->portName()<<" no answer ";
- QObject::disconnect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
- return false;
- }
- #endif
- #endif
- }
- bool TestService::Valve_op(Valve_ID id, Valve_OP op)
- {
- QByteArray tx_buf;
- //bool ret_flag = true;
- char zero = 0x00;
- tx_buf.append(RELAY_ADDRESS);
- if(Valve_ID_All == id){
- tx_buf.append(0x0F);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(zero);
- tx_buf.append(0x10);
- tx_buf.append(0x02);
- }else{
- tx_buf.append(0x05);
- tx_buf.append(zero);
- tx_buf.append(static_cast<char>(id));
- }
- if(Valve_OP_Open == op){
- tx_buf.append(0xFF);
- tx_buf.append(zero);
- }
- else{
- tx_buf.append(zero);
- tx_buf.append(zero);
- }
- int crc;
- crc=Calcrc((unsigned char *)tx_buf.data(),tx_buf.size());
- tx_buf.append((crc>>8)&0xff);
- tx_buf.append(crc&0xff);
- QByteArray rx_buf;
- rx_buf = m_SerialUi3.serialWriteReponse(tx_buf);
- if(rx_buf.size() > 0){
- if(check_crc(rx_buf)){
- qDebug("Valve_op ok valve_id[%d], op[%d] ", id, op);
- return true;
- }else{
- qDebug("Valve_op failed valve_id[%d], op[%d] ", id, op);
- return false;
- }
- }else{
- qDebug("Valve_op failed valve_id[%d], op[%d] ", id, op);
- return false;
- }
- #if 0
- m_Serial3mutex.lock();
- SendCmd(m_SerialPort3, tx_buf);
- /*
- if(!m_SerialPort3->waitForBytesWritten(100)){
- qDebug("waitForBytesWritten failed");
- m_Serial3mutex.unlock();
- return false;
- }
- */
- #if 1
- QSignalSpy spy(m_SerialPort3, SIGNAL(readyRead()));
- bool ret = spy.wait(500);
- if(ret == true){
- QByteArray buffer;
- //while (m_SerialPort3->waitForReadyRead(5))
- //{
- buffer += m_SerialPort3->readAll();
- //QString str = QString::fromUtf8("adjust_pressure RX: ");
- //str.append(buffer.toHex().toUpper());
- //qDebug("[%s]", str.toStdString().c_str());
- if(!check_crc(buffer)){
- ret_flag = false;
- }
- }else
- {
- qInfo()<<m_SerialPort3->portName()<<" no answer ";
- ret_flag = false;
- }
- m_Serial3mutex.unlock();
- return ret_flag;
- #else
- QEventLoop eventLoop;//局部事件循环,不卡界面
- QTimer::singleShot(2000, &eventLoop, &QEventLoop::quit);
- QObject::connect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
- eventLoop.exec();
- if(m_SerialPort3->bytesAvailable() > 0)
- {
- QByteArray ba;
- //while (m_SerialPort3->waitForReadyRead(5))
- //{
- ba += m_SerialPort3->readAll();
- QString str = QString::fromUtf8("adjust_pressure RX: ");
- str.append(ba.toHex().toUpper());
- qDebug("[%s]", str.toStdString().c_str());
- if(check_crc(ba)){
- return true;
- }else{
- return false;
- }
- //}
- //return true;
- }
- else
- {
- qInfo()<<m_SerialPort3->portName()<<" no answer ";
- QObject::disconnect(m_SerialPort3, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
- return false;
- }
- #endif
- #endif
- }
- void TestService::valve_open(Valve_ID id)
- {
- qDebug("valve_open [%d]", id);
- Valve_op(id, Valve_OP_Open);
- }
- void TestService::valve_close(Valve_ID id)
- {
- qDebug("valve_close [%d]", id);
- Valve_op(id, Valve_OP_Close);
- }
- QString TestService::read_PressureValue()
- {
- float pressure = 0.0;
- static int timeout_count =0;
- if(Read_pressure(pressure)){
- //qDebug("Read_PressureValue [%f]", pressure);
- timeout_count=0;
- }else{
- pressure = calculate_averagepressure();
- qDebug("Read_PressureValue failed");
- timeout_count++;
- if(timeout_count >= AVERAGE_PRESSURE_COUNT){
- //data is to old, clear
- clear_pressurelist();
- qDebug("clear_pressurelist ");
- }
- }
- QString pressure_str = QString("%1").arg(pressure, 5,'f',3,QLatin1Char('0'));
- return pressure_str;
- }
- void TestService::adjust_Pressure(QString voltage_str)
- {
- bool ok;
- ushort voltage = voltage_str.toUShort(&ok, 10);
- qDebug("Add_Pressure [%d]", voltage);
- Adjust_pressure(voltage);
- }
- bool TestService::OpenSerial3()
- {
- m_SerialPort3 = new QSerialPort();
- m_SerialPort3->setPortName("COM3");
- m_SerialPort3->setBaudRate(9600, QSerialPort::AllDirections);
- m_SerialPort3->setDataBits(QSerialPort::Data8);
- m_SerialPort3->setParity(QSerialPort::NoParity);
- m_SerialPort3->setStopBits(QSerialPort::OneStop);
- //m_SerialPort3->setFlowControl(QSerialPort::NoFlowControl);//Set to no flow control
- if (m_SerialPort3->open(QIODevice::ReadWrite) == false){
- qDebug("open COM3 failed");
- //QMessageBox::warning(this, "警告", "打开串口COM3失败");
- QString str = "打开串口COM3失败";
- emit onNotice(str);
- return false;
- }
- else
- {
- //controlserial->close();
- //connect(m_SerialPort3,SIGNAL(readyRead()),this,SLOT(ReadSerialport3()));
- return true;
- }
- }
- bool TestService::OpenSerial4()
- {
- m_SerialPort4 = new QSerialPort();
- m_SerialPort4->setPortName("COM4");
- m_SerialPort4->setBaudRate(9600, QSerialPort::AllDirections);
- m_SerialPort4->setDataBits(QSerialPort::Data8);
- m_SerialPort4->setParity(QSerialPort::EvenParity);
- m_SerialPort4->setStopBits(QSerialPort::OneStop);
- m_SerialPort4->setFlowControl(QSerialPort::NoFlowControl);//Set to no flow control
- if (m_SerialPort4->open(QIODevice::ReadWrite) == false){
- qDebug("open COM4 failed");
- //QMessageBox::warning(this, "警告", "打开串口COM4失败");
- QString str = "打开串口COM4失败,无法读取压力";
- emit onNotice(str);
- return false;
- }
- else
- {
- //readserial->close();
- //connect(m_SerialPort4,SIGNAL(readyRead()),this,SLOT(ReadSerialport4()));
- return true;
- }
- }
- void TestService::OnTimer1()
- {
- float pressure = 0.001;
- static int timeout_count =0;
- if(Read_pressure(pressure)){
- //if(false){
- //qDebug("Read_PressureValue [%f]", pressure);
- timeout_count=0;
- }else{
- qDebug("Read_PressureValue failed");
- pressure = calculate_averagepressure();
- timeout_count++;
- if(timeout_count >= AVERAGE_PRESSURE_COUNT){
- //data is to old, clear
- clear_pressurelist();
- qDebug("clear_pressurelist ");
- }
- }
- #if 0
- if(timeout_count < 180){
- m_stage =Test_Stage_SysTest;
- m_step = Test_Step_Start;
- //pressure = 0.1 * timeout_count;
- if(timeout_count > 60){
- m_step = Test_Step_KeepPressure;
- //pressure = 0.1 * timeout_count;
- }
- }else if(timeout_count < 360){
- if(timeout_count == 180){
- m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = 4.5;
- m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = 4.3;
- m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = 0.2;
- m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
- m_tank.m_comparts[m_compartmentid].m_result.sys_test_ok = true;
- }
- m_stage =Test_Stage_ValveTest;
- m_step = Test_Step_Start;
- //pressure = 0.1 * timeout_count;
- if(timeout_count > 240){
- m_step = Test_Step_KeepPressure;
- //pressure = 0.1 * timeout_count;
- }
- }else {
- if(timeout_count == 360){
- m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = 4.5;
- m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = 3.3;
- m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = 1.2;
- m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
- m_tank.m_comparts[m_compartmentid].m_result.valve_test_ok = true;
- }
- m_step = Test_Step_End;
- }
- //pressure = 0.1 * timeout_count;
- pressure = 3 + 3*sin(M_PI*timeout_count/60);
- #endif
- int _ipressure = pressure*100;
- pressure = _ipressure/100.0;
- QString pressure_str = QString("%1").arg(pressure, 4,'f',2,QLatin1Char('0'));
- PressureItem item;
- //item.m_pressure = pressure_str;
- item.m_testDirection = m_direction;
- item.m_testStage = m_stage;
- item.m_testStep = m_step;
- QVariantMap map;
- map.clear();
- map.insert("pressure", pressure_str);
- map.insert("direction", item.m_testDirection);
- map.insert("stage", item.m_testStage);
- map.insert("step", item.m_testStep);
- //QString pressure_str = QString("%1").arg(pressure, 5,'f',3,QLatin1Char('0'));
- //m_tank.m_comparts[m_compartmentid].m_currentpressure= pressure_str.toFloat();
- emit sigPressure(m_compartmentid+1, map);
- }
- void TestService::OnTimer2()
- {
- qDebug("OnTimer2 >>>enter ");
- //float pressure;
- //Read_pressure(pressure);
- QThread::sleep(5);
- //emit Pressure_changed(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
- qDebug("OnTimer2 >>>leave ");
- }
- Ret_Value TestService::Pressure_RoughAdjust(const TestExpect& expect)
- {
- Ret_Value ret_v = Ret_OK;
- int keep_count = 0;
- bool bOpen1 = false;
- bool bOpen2 = false;
- bool bOpen3 = false;
- //bool inTake_Open = false;
- int exceed_times = 0;
- int exceed_count = 0;
- int delay_count = 0;
- QElapsedTimer timer;
- timer.start();
- float current_pressure;
- //等待压力达到目标值附近
- while(m_bRunning){
- current_pressure = calculate_averagepressure();
- if(current_pressure >= expect.target+0.02){
- if(0 == exceed_count){
- exceed_times++;
- }
- exceed_count++;
- if(bOpen1){
- if(Valve_op(Valve_ID_ROUGH_0, Valve_OP_Close))
- bOpen1 = false;
- }
- if(bOpen2){
- if(Valve_op(Valve_ID_ROUGH_1, Valve_OP_Close))
- bOpen2 = false;
- }
- if(bOpen3){
- if(Valve_op(Valve_ID_ROUGH_2, Valve_OP_Close))
- bOpen3 = false;
- }
- if(exceed_count >= 30){
- keep_count = 50;
- }
- if(exceed_times == 1){
- delay_count = 15;
- }else if(exceed_times == 2){
- delay_count = 10;
- }else {
- delay_count = 5;
- }
- //break;
- }else if(current_pressure >= expect.target && current_pressure < expect.target+0.02){
- //从超出, 回落到该区间
- if(exceed_count > 0){
- keep_count++;
- }
- //exceed_count = 0;
- }else if(current_pressure < expect.target) {
- exceed_count =0;
- if(delay_count > 0){
- delay_count--;
- }
- if(0 == delay_count){
- if(0 == exceed_times){
- if(!bOpen1){
- bOpen1 = Valve_op(Valve_ID_ROUGH_0, Valve_OP_Open);
- }
- if(!bOpen2){
- bOpen2 = Valve_op(Valve_ID_ROUGH_1, Valve_OP_Open);
- }
- if(!bOpen3){
- bOpen3 = Valve_op(Valve_ID_ROUGH_2, Valve_OP_Open);
- }
- }else if(1 == exceed_times){
- if(!bOpen1){
- bOpen1 = Valve_op(Valve_ID_ROUGH_0, Valve_OP_Open);
- }
- if(!bOpen2){
- bOpen2 = Valve_op(Valve_ID_ROUGH_1, Valve_OP_Open);
- }
- }else{
- if(!bOpen1){
- bOpen1 = Valve_op(Valve_ID_ROUGH_0, Valve_OP_Open);
- }
- }
- }
- }
- if(keep_count >= 30){ //about 60s
- break;
- }
- QThread::msleep(1000);
- int remainingtime = expect.timeout*1000 - timer.elapsed();
- if(remainingtime <= 0){
- qDebug(" add_pressure timeout, Rough valve will colse");
- ret_v = Ret_Timeout;
- break;
- }
- }
- if(!m_bRunning){
- // be stoped
- ret_v = Ret_Exit;
- }
- if(bOpen1){
- if(Valve_op(Valve_ID_ROUGH_0, Valve_OP_Close))
- bOpen1 = false;
- }
- if(bOpen2){
- if(Valve_op(Valve_ID_ROUGH_1, Valve_OP_Close))
- bOpen2 = false;
- }
- if(bOpen3){
- if(Valve_op(Valve_ID_ROUGH_2, Valve_OP_Close))
- bOpen3 = false;
- }
- return ret_v;
- }
- #if 1
- Ret_Value TestService::Pressure_Adjust(const TestExpect& expect)
- {
- PID pid = PID(1, 20, -10, 5.1, 28.0, 1.0);
- double current_pressure;
- //double last_pressure;
- Ret_Value ret_v = Ret_OK;
- int voltage_offset = 0;
- int keep_times = 0;
- QElapsedTimer timer;
- bool inTake_Open = false;
- int exceed_times = 0;
- int exceed_count = 0;
- int delay_count = 0;
- inTake_Open = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
- timer.start();
- while(m_bRunning){
- current_pressure = calculate_averagepressure();
- if(current_pressure > expect.target + 0.05){
- if(exceed_count == 0){
- exceed_times++;
- }
- exceed_count++;
- if(inTake_Open == true){
- if(Valve_op(Valve_ID_INTAKE, Valve_OP_Close)){
- inTake_Open = false;
- }
- }
- if(exceed_count > 15){
- keep_times = 20;
- }
- if(exceed_times == 1){
- delay_count = 15;
- }else if(exceed_times == 2){
- delay_count = 10;
- }else{
- delay_count = 5;
- }
- }else if(current_pressure >= expect.target && current_pressure <= expect.target+0.05){
- if(exceed_count > 0){
- keep_times = 20;
- }
- exceed_count= 0;
- }else{
- exceed_count= 0;
- if(delay_count > 0){
- delay_count--;
- }
- if(0 == delay_count){
- if(false == inTake_Open){
- inTake_Open = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
- }
- double inc = pid.calculate(expect.target, current_pressure);
- voltage_offset = (inc);
- if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
- QThread::msleep(200);
- if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
- qDebug("communication error, ajust_pressure failed");
- ret_v = Ret_Failed;
- break;
- }
- }
- }
- }
- if(keep_times >= 15){
- break;
- }
- QThread::msleep(1000);
- int remainingtime = expect.timeout*1000 - timer.elapsed();
- if(remainingtime < 0){
- ret_v = Ret_Timeout;
- qDebug("timeout, ajust_pressure failed");
- break;
- }
- }
- Valve_op(Valve_ID_INTAKE, Valve_OP_Close);
- Adjust_pressure(0);
- if(!m_bRunning){
- // be stoped
- ret_v = Ret_Exit;
- }
- return ret_v;
- }
- #else
- int TestService::Pressure_Adjust(const TestExpect& expect)
- {
- float current_pressure;
- int keep_times = 0;
- bool bValveOpen = false;
- int add_base = 5;
- int sub_base = 0;
- float delta = 0.0; //当前差值
- keep_times = 0;
- int count_h = 0;
- int count_l =0;
- short voltage_offset = 0;
- QElapsedTimer timer;
- timer.start();
- int ret_value = 0;
- keep_times=0;
- while(m_bRunning){
- current_pressure = calculate_averagepressure();
- if(current_pressure >= expect.target-0.04){
- count_l = 0;
- delta = current_pressure - expect.target;
- if(delta >= 0.08){
- keep_times=0;
- count_h++;
- if(count_h < 30){
- if(bValveOpen){
- if(Valve_op(Valve_ID_INTAKE, Valve_OP_Close))
- bValveOpen = false;
- qDebug("ajust_pressure IntakeValve close return[%d] ", bValveOpen == true? 1:0);
- }
- }
- qDebug("ajust_pressure count_h[%d] ", count_h);
- if(count_h > 30){
- voltage_offset = 0;
- qDebug("ajust_pressure , voltage: %d \n", expect.start_voltage+voltage_offset);
- if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
- QThread::msleep(200);
- if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
- qDebug("communication error, ajust_pressure failed");
- }
- }
- if(false == bValveOpen){
- bValveOpen = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
- qDebug("ajust_pressure IntakeValve open return[%d] ", bValveOpen == true? 1:0);
- }
- }
- }else{
- keep_times++;
- //count_h=0;
- if(count_h > 30){
- if(delta > 0.05){
- if(bValveOpen){
- if(Valve_op(Valve_ID_INTAKE, Valve_OP_Close))
- bValveOpen = false;
- }
- }else if(){
- }
- }
- }
- }else {
- keep_times=0;
- count_h=0;
- count_l++;
- delta = expect.target - current_pressure;
- if(delta > 0.0){
- int k = count_l/30;
- if(voltage_offset != (add_base+k*1)){
- voltage_offset = add_base+k*1;
- //add_base = voltage_offset;
- qDebug("ajust_pressure voltage: %d \n", expect.start_voltage+voltage_offset);
- if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
- QThread::msleep(200);
- if(!Adjust_pressure(expect.start_voltage+voltage_offset)){
- qDebug("communication error, ajust_pressure failed");
- }
- }
- }
- if(false == bValveOpen){
- bValveOpen = Valve_op(Valve_ID_INTAKE, Valve_OP_Open);
- qDebug("ajust_pressure IntakeValve open return[%d] ", bValveOpen == true? 1:0);
- }
- }
- }
- qDebug("ajust_pressure keep_times [%d] ", keep_times);
- if(keep_times >= 30){
- break;
- }
- QThread::msleep(500);
- int remainingtime = expect.timeout*1000 - timer.elapsed();
- if(remainingtime < 0){
- ret_value = -1;
- qDebug("timeout, ajust_pressure failed");
- break;
- }
- }
- if(!m_bRunning){
- // be stoped
- return 1;
- }
- return ret_value;
- }
- #endif
- Ret_Value TestService::Pressure_Relief(const TestExpect& expect)
- {
- Ret_Value ret_v = Ret_OK;
- //打开泄压阀
- if(!Valve_op(Valve_ID_VENT, Valve_OP_Open)){
- QThread::msleep(200);
- if(!Valve_op(Valve_ID_VENT, Valve_OP_Open)){
- qDebug("Pressure_Relief faile, valve communicate failed");
- ret_v = Ret_Failed;
- return ret_v;
- }
- }
- //等待压力清零
- float last_pressure = calculate_averagepressure();
- float current_pressure;
- int keep_count = 0;
- while(m_bRunning){
- QThread::msleep(1000);
- current_pressure = calculate_averagepressure();
- if(abs(current_pressure-last_pressure) < 0.02){
- keep_count++;
- if(keep_count > 15){
- break;
- }
- }else{
- keep_count = 0;
- }
- last_pressure = current_pressure;
- }
- //关闭泄压阀
- if(!Valve_op(Valve_ID_VENT, Valve_OP_Close)){
- if(!Valve_op(Valve_ID_VENT, Valve_OP_Close)){
- qDebug("Pressure_Relief faile, valve communicate failed");
- ret_v = Ret_Failed;
- return ret_v;
- }
- }
- if(!m_bRunning){
- ret_v = Ret_Exit;
- }
- return ret_v;
- }
- Ret_Value TestService::Pressure_Relief()
- {
- Ret_Value ret_v = Ret_OK;
- bool bVent_Open = false;
- Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Open);
- //等待压力清零
- float current_pressure;
- int keep_count = 0;
- while(m_bRunning){
- //打开泄压阀
- if(bVent_Open == false){
- bVent_Open = Valve_op(Valve_ID_VENT, Valve_OP_Open);
- }
- QThread::msleep(1000);
- current_pressure = calculate_averagepressure();
- if(abs(current_pressure) < 0.5){
- keep_count++;
- if(keep_count > 10){
- break;
- }
- }else{
- keep_count = 0;
- }
- }
- //关闭泄压阀
- if(bVent_Open == true){
- Valve_op(Valve_ID_VENT, Valve_OP_Close);
- }
- if(!m_bRunning){
- ret_v = Ret_Exit;
- }
- return ret_v;
- }
- Ret_Value TestService::Pressure_Keep(int keeptime, float threshold, float& delta)
- {
- int count = 0;
- Ret_Value ret_v = Ret_OK;
- float start_pressure = calculate_averagepressure();
- while((count < keeptime) && m_bRunning){
- QThread::msleep(1000);
- count++;
- }
- if(!m_bRunning){
- // be stoped
- //return 1;
- ret_v = Ret_Exit;
- }else{
- delta = start_pressure - calculate_averagepressure();
- if(abs(delta) <= threshold){
- ret_v = Ret_OK;
- }else{
- ret_v = Ret_Failed;
- }
- }
- return ret_v;
- }
- int TestService::make_messagebox(Test_Direction direction, Test_Step step, Ret_Value ret_v)
- {
- int ret_btn = QMessageBox::NoButton;
- QString notice_str;
- if(Test_Direction_Positive == direction){
- switch(step){
- case Test_Step_AddPressure:
- if(Ret_Timeout == ret_v){
- notice_str = "加压超时, 是否重试?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
- }else if(Ret_Failed == ret_v){
- notice_str = "继电器通信失败, 和序终止";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
- }
- break;
- case Test_Step_AdjustPressure:
- if( Ret_Timeout == ret_v ){
- notice_str = "比例阀调压精调超时, 是否重试?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
- //emit onNotice(notice_str);
- }else if(Ret_Failed == ret_v){
- notice_str = "继电器通信失败, 程序终止";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
- //emit onNotice(notice_str);
- }
- break;
- default:
- break;
- }
- }else {
- }
- return ret_btn;
- }
- void TestService::run()
- {
- qDebug("doTest >>>enter ");
- QString notice_str;
- QElapsedTimer q_time;
- int ret_btn = QMessageBox::NoButton;
- TestExpect texpect;
- bool bQuit = false;
- q_time.start();
- Ret_Value ret_v = Ret_OK;
- #if 0
- while(m_bRunning){
- QThread::sleep(4);
- int ret = m_msgbox.warning(NULL, "提示:", "请打开旋拧阀给系统加压", QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Abort);
- switch (ret)
- {
- case QMessageBox::Ok:
- qDebug()<<"111 OK";
- break;
- case QMessageBox::Retry:
- qDebug()<<"111 Retry";
- break;
- case QMessageBox::Abort:
- qDebug()<<"111 Abort";
- break;
- default:
- qDebug()<<"111 default";
- break;
- }
- }
- #else
- if(m_compartmentid >= 0){
- if(m_direction == Test_Direction_Positive){
- while(m_bRunning && (bQuit == false)){
- switch(m_step){
- case Test_Step_Start:
- {
- m_tank.get_compartment(m_compartmentid).clear_result();
- // close all valves
- Valve_op(Valve_ID_All, Valve_OP_Close);
- ret_btn = m_msgbox.information(NULL, "提示:", "请打开旋拧阀给系统加压", QMessageBox::Ok, QMessageBox::Ok);
- switch (ret_btn)
- {
- case QMessageBox::Ok:
- qDebug()<<"Yes";
- // 打开压力传感阀
- Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Open);
- // 如果容积大于 1000L , 才进入粗调步骤
- if(m_tank.get_compartment(m_compartmentid).m_volume >= 500){
- m_step = Test_Step_AddPressure;
- }else{
- m_step = Test_Step_AdjustPressure;
- }
- break;
- default:
- break;
- }
- }
- break;
- case Test_Step_AddPressure:
- {
- if(m_stage == Test_Stage_SysTest){
- emit sigState(m_compartmentid+1, "回收系统测试,开始加压", "");
- }else{
- emit sigState(m_compartmentid+1, "回收阀测试,开始加压", "");
- }
- texpect.target = 4.50;
- texpect.timeout = m_tank.m_comparts[m_compartmentid].get_Roughadjust_timeout();
- ret_v = Pressure_RoughAdjust(texpect);
- if(Ret_OK == ret_v){
- //m_step = Test_Step_AdjustPressure;
- //m_step = Test_Step_KeepPressure;
- if(m_stage == Test_Stage_SysTest){
- m_step = Test_Step_KeepPressure;
- }else if(m_stage == Test_Stage_ValveTest){
- m_step = Test_Step_reliefPressure;
- }
- }else if(Ret_Exit == ret_v){
- bQuit = true;
- }else{
- ret_btn = make_messagebox(m_direction, m_step, ret_v);
- switch (ret_btn)
- {
- case QMessageBox::Retry:
- qDebug()<<"Retry";
- break;
- case QMessageBox::Abort:
- qDebug()<<"Abort";
- bQuit = true;
- break;
- case QMessageBox::Ok:
- qDebug()<<"OK";
- bQuit = true;
- break;
- default:
- break;
- }
- }
- #if 0
- if(ret_v < 0){
- ret_btn = QMessageBox::NoButton;
- if( -1 == state ){
- notice_str = "加压超时, 是否重试?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
- //emit onNotice(notice_str);
- }else if(-2 == state){
- notice_str = "继电器读写不成功, 和序终止";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
- //emit onNotice(notice_str);
- }
- qDebug() << notice_str;
- //ret_btn = QMessageBox::information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- switch (ret_btn)
- {
- case QMessageBox::Retry:
- qDebug()<<"Retry";
- break;
- case QMessageBox::Abort:
- qDebug()<<"Abort";
- bQuit = true;
- break;
- case QMessageBox::Ok:
- qDebug()<<"OK";
- bQuit = true;
- break;
- default:
- break;
- }
- }else{
- m_step = Test_Step_AdjustPressure;
- }
- #endif
- }
- break;
- case Test_Step_AdjustPressure:
- {
- if(m_stage == Test_Stage_SysTest){
- emit sigState(m_compartmentid+1, "回收系统测试,开始调压", "");
- }else{
- emit sigState(m_compartmentid+1, "回收阀测试,开始调压", "");
- }
- texpect.start_voltage = 175;
- texpect.target = 4.50;
- texpect.timeout = m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
- ret_v = Pressure_Adjust(texpect);
- if(Ret_OK == ret_v){
- if(m_stage == Test_Stage_SysTest){
- m_step = Test_Step_KeepPressure;
- }else if(m_stage == Test_Stage_ValveTest){
- m_step = Test_Step_reliefPressure;
- }
- }else if(Ret_Exit == ret_v){
- bQuit = true;
- }else{
- ret_btn = make_messagebox(m_direction, m_step, ret_v);
- switch (ret_btn)
- {
- case QMessageBox::Retry:
- qDebug()<<"Retry";
- break;
- case QMessageBox::Abort:
- qDebug()<<"Abort";
- bQuit = true;
- break;
- case QMessageBox::Ok:
- qDebug()<<"OK";
- bQuit = true;
- break;
- default:
- break;
- }
- }
- #if 0
- if(state < 0){
- ret_btn = QMessageBox::NoButton;
- if( -1 == state ){
- notice_str = "比例阀调压超时, 是否重试?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
- //emit onNotice(notice_str);
- }else if(-2 == state){
- notice_str = "继电器读写不成功, 程序终止";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
- //emit onNotice(notice_str);
- }
- qDebug() << notice_str;
- //ret_btn = QMessageBox::information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- switch (ret_btn)
- {
- case QMessageBox::Retry:
- qDebug()<<"Retry";
- break;
- case QMessageBox::Abort:
- qDebug()<<"Abort";
- bQuit = true;
- break;
- case QMessageBox::Ok:
- qDebug()<<"OK";
- bQuit = true;
- break;
- default:
- break;
- }
- }else{
- if(m_stage == Test_Stage_SysTest){
- m_step = Test_Step_KeepPressure;
- }else if(m_stage == Test_Stage_ValveTest){
- m_step = Test_Step_reliefPressure;
- }
- }
- #endif
- }
- break;
- case Test_Step_KeepPressure:
- {
- if(m_stage == Test_Stage_SysTest){
- emit sigState(m_compartmentid+1, "回收系统测试,开始保压", "");
- }else{
- emit sigState(m_compartmentid+1, "回收阀测试,开始保压", "");
- }
- // 检测 5分钟都没超限,就是通过了
- float delta = 0;
- float threshold = 0.0;
- bool bPassed = true;
- if(m_stage == Test_Stage_SysTest){
- threshold = m_tank.m_comparts[m_compartmentid].m_standarditem.system_threshold;
- m_tank.m_comparts[m_compartmentid].m_result.sysstart_pressure = calculate_averagepressure();
- ret_v = Pressure_Keep(5*60, threshold, delta);
- m_tank.m_comparts[m_compartmentid].m_result.sysend_pressure = calculate_averagepressure();
- m_tank.m_comparts[m_compartmentid].m_result.delta_sys_pressure = delta;
- if(Ret_Exit == ret_v){
- bQuit = true;
- } else {
- if(Ret_OK == ret_v){
- //正压测试通过
- bPassed = true;
- m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = true;
- } else{
- //notice_str = "油气回收系统密封性,测试未通过";
- bPassed = false;
- m_tank.m_comparts[m_compartmentid].m_result.pass_sys_pressure = false;
- }
- m_tank.m_comparts[m_compartmentid].m_result.sys_test_ok = true;
- QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
- emit sigSystemResult(m_compartmentid+1, delta_str, bPassed);
- emit sigState(m_compartmentid+1, "回收系统检测,结束", "");
- ret_btn = QMessageBox::NoButton;
- notice_str = "系统密闭性已经检测完成,是否进行阀门密闭性检测?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- switch (ret_btn)
- {
- case QMessageBox::Yes:
- m_stage = Test_Stage_ValveTest;
- if(calculate_averagepressure() < 4.50){
- m_step = Test_Step_AddPressure;
- }else{
- m_step = Test_Step_reliefPressure; //如果压力还在4.5以上, 可以不调压
- }
- break;
- case QMessageBox::No:
- m_step = Test_Step_End;
- break;
- default:
- break;
- }
- }
- }else{
- threshold = m_tank.m_comparts[m_compartmentid].m_standarditem.valve_threshold;
- m_tank.m_comparts[m_compartmentid].m_result.valvestart_pressure = calculate_averagepressure();
- ret_v = Pressure_Keep(5*60, threshold, delta);
- m_tank.m_comparts[m_compartmentid].m_result.valveend_pressure = calculate_averagepressure();
- m_tank.m_comparts[m_compartmentid].m_result.delta_valve_pressure = delta;
- if(Ret_Exit == ret_v){
- bQuit = true;
- }else {
- if(Ret_OK == ret_v){
- m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = true;
- bPassed = true;
- }else{
- m_tank.m_comparts[m_compartmentid].m_result.pass_valve_pressure = false;
- bPassed = false;
- }
- m_tank.m_comparts[m_compartmentid].m_result.valve_test_ok = true;
- QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
- emit sigValveResult(m_compartmentid+1, delta_str, bPassed);
- emit sigState(m_compartmentid+1, "回收阀测试,结束", "");
- m_step = Test_Step_End;
- }
- }
- }
- break;
- case Test_Step_reliefPressure:
- {
- ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经关闭油气回收阀。", QMessageBox::Ok, QMessageBox::Ok);
- switch (ret_btn)
- {
- case QMessageBox::Ok:
- break;
- default:
- break;
- }
- if(m_stage == Test_Stage_SysTest){
- emit sigState(m_compartmentid+1, "油气回收系统测试,开始泄压", "");
- }else{
- emit sigState(m_compartmentid+1, "油气回收阀测试,开始泄压", "");
- }
- texpect.start_voltage = 0;
- texpect.target = 0.0;
- texpect.timeout = 0;//m_tank.m_comparts[m_compartmentid].get_adjust_timeout();
- ret_v = Pressure_Relief(texpect);
- if(Ret_Exit == ret_v){
- bQuit = true;
- }else if(Ret_OK == ret_v){
- m_step = Test_Step_KeepPressure;
- }else{
- ret_btn = QMessageBox::NoButton;
- if(Ret_Timeout == ret_v){
- notice_str = "泄压超时, 是否重试?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
- }else{
- notice_str = "继电器通信失败, 程序终止";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
- }
- switch (ret_btn)
- {
- case QMessageBox::Retry:
- break;
- case QMessageBox::Abort:
- bQuit = true;
- break;
- case QMessageBox::Ok:
- bQuit = true;
- break;
- default:
- break;
- }
- }
- }
- break;
- default:
- break;
- }
- if(Test_Step_End == m_step){
- break;
- }
- }
- }else{
- //负压测试, 暂不支持
- m_stage = Test_Stage_SysTest;
- qDebug(" negative test is not support now ");
- }
- }else{
- //self test
- if(m_direction == Test_Direction_Positive){
- while(m_bRunning && (bQuit == false)){
- switch(m_step){
- case Test_Step_Start:
- {
- emit sigState(m_compartmentid+1, "开始", "");
- int errors = 0;
- // close all valves
- errors = Valve_op(Valve_ID_All, Valve_OP_Close)? 0:1;
- if(errors > 0){
- ret_btn = m_msgbox.information(NULL, "提示:", "继电器通信故障,请终止程序", QMessageBox::Abort, QMessageBox::Abort);
- }else{
- ret_btn = m_msgbox.information(NULL, "提示:", "请确认已经打开旋拧阀给系统施缓慢加压", QMessageBox::Ok, QMessageBox::Ok);
- }
- switch (ret_btn)
- {
- case QMessageBox::Ok:
- qDebug()<<"Yes";
- // 打开压力传感阀
- Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Open);
- m_step = Test_Step_AdjustPressure;
- break;
- case QMessageBox::Abort:
- bQuit = true;
- break;
- }
- }
- break;
- case Test_Step_AdjustPressure:
- {
- emit sigState(m_compartmentid+1, "调压", "");
- QThread::msleep(2000);
- texpect.start_voltage = 175;
- texpect.target = 4.50;
- texpect.timeout = 10*60; //10分钟
- ret_v = Pressure_Adjust(texpect);
- if(Ret_Exit == ret_v){
- bQuit = true;
- }else if(Ret_OK == ret_v){
- m_step = Test_Step_KeepPressure;
- }else{
- ret_btn = QMessageBox::NoButton;
- if(Ret_Timeout == ret_v){
- notice_str = "比例阀调压超时, 是否重试?";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Retry|QMessageBox::Abort, QMessageBox::Retry);
- }else{
- notice_str = "继电器通信失败, 程序终止";
- ret_btn = m_msgbox.information(NULL, "提示:", notice_str, QMessageBox::Ok, QMessageBox::Ok);
- }
- switch (ret_btn)
- {
- case QMessageBox::Retry:
- break;
- case QMessageBox::Abort:
- bQuit = true;
- break;
- case QMessageBox::Ok:
- bQuit = true;
- break;
- default:
- break;
- }
- }
- }
- break;
- case Test_Step_KeepPressure:
- {
- emit sigState(m_compartmentid+1, "保压", "");
- // 检测 5分钟都没超限,就是通过了
- float delta = 0;
- float threshold = 0.6;
- bool bPassed = true;
- ret_v = Pressure_Keep(5*60, threshold, delta);
- if(Ret_Exit == ret_v){
- bQuit = true;
- }else{
- if(Ret_OK == ret_v){
- bPassed = true;
- }else{
- bPassed = false;
- }
- QString delta_str = QString("%1").arg(delta, 4,'f',2,QLatin1Char('0'));
- emit sigSelfTestResult(m_compartmentid+1, delta_str, bPassed);
- emit sigState(m_compartmentid+1, "结束", "");
- m_step = Test_Step_End;
- }
- }
- break;
- default:
- break;
- }
- if(Test_Step_End == m_step){
- break;
- }
- }
- }else{ //negtive
- }
- }
- if(Test_Step_End == m_step){
- ret_btn = m_msgbox.information(NULL, "提示:", "栓测任务结束,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- }else{
- if(true == bQuit){
- if(Ret_Exit == ret_v){ // 主动退出
- ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- }else if(Ret_Timeout == ret_v){ // 超时退出
- ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- }else{//通信错误退出
- ret_btn = m_msgbox.information(NULL, "提示:", "程序退出,是否要泄压?", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
- }
- }
- }
- switch (ret_btn)
- {
- case QMessageBox::Yes:
- qDebug()<<"Yes";
- // 打开泄压阀
- //Valve_op(Valve_ID_VENT, Valve_OP_Open);
- Pressure_Relief();
- break;
- default:
- break;
- }
- if(m_bRunning){
- m_bRunning = false;
- emit sigStop(m_compartmentid+1);
- }else{
- }
- // 关闭压力传感阀
- //Valve_op(Valve_ID_PRESSURESENSOR, Valve_OP_Close);
- // 复位
- Adjust_pressure(0);
- Valve_op(Valve_ID_All, Valve_OP_Close);
- //m_pTimer_1->stop();
- #endif
- qDebug("doTest >>>leave ");
- }
|