mainwindow.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. #include "mainwindow.h"
  2. #include "./ui_mainwindow.h"
  3. HANDLE MainWindow::semget()
  4. {
  5. return semForRF;
  6. }
  7. SemaphoreWorkerThread::SemaphoreWorkerThread(QObject* parent, MainWindow* wnd = nullptr) : QThread(parent)
  8. {
  9. this->wnd = wnd;
  10. }
  11. void SemaphoreWorkerThread::run()
  12. {
  13. #ifdef _WIN32
  14. HANDLE sem = wnd->semget();
  15. DWORD dwWaitResult = WaitForSingleObject(sem, 20000);
  16. #endif
  17. delay(50);
  18. qDebug() << "Semaphore released!";
  19. }
  20. Counter::Counter(int counts)
  21. {
  22. this->ncounts = 0;
  23. this->counts = counts;
  24. }
  25. void Counter::count()
  26. {
  27. ncounts++;
  28. qDebug() << "ncounts: " << ncounts;
  29. qDebug() << "counts: " << counts;
  30. if(ncounts >= counts)
  31. {
  32. qDebug() << "ncounts: " << ncounts;
  33. emit Counter::countsEnd();
  34. }
  35. }
  36. void Counter::reset()
  37. {
  38. ncounts = 0;
  39. qDebug() << "counts: " << this->counts;
  40. }
  41. void Counter::rebind(int counts)
  42. {
  43. this->counts = counts;
  44. qDebug() << "counts: " << this->counts;
  45. }
  46. void delay(int ms)
  47. {
  48. QTime dieTime= QTime::currentTime().addMSecs(ms);
  49. while (QTime::currentTime() < dieTime)
  50. QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
  51. }
  52. MainWindow::MainWindow(QWidget *parent)
  53. : QMainWindow(parent)
  54. , ui(new Ui::MainWindow)
  55. {
  56. ui->setupUi(this);
  57. ui->SampleRateBox->addItem("2 MHz");
  58. ui->SampleRateBox->addItem("8 MHz");
  59. ui->SampleRateBox->addItem("10 MHz");
  60. ui->SampleRateBox->addItem("12.5 MHz");
  61. ui->SampleRateBox->addItem("16 MHz");
  62. ui->SampleRateBox->addItem("20 MHz");
  63. ui->GainEdit->setEnabled(true);
  64. ui->AnaliseButton->setEnabled(true);
  65. syncport = new QSerialPort(this);
  66. portInfos = QSerialPortInfo::availablePorts();
  67. for(const QSerialPortInfo& info : portInfos)
  68. {
  69. ui->SerialPortBox->addItem(info.portName() + " (" + info.description() + ")");
  70. }
  71. int w = ui->label->pixmap().width();
  72. int h = ui->label->pixmap().height();
  73. ui->label->setPixmap(ui->label->pixmap().scaled(w, h, Qt::KeepAspectRatio));
  74. ui->centralwidget->setLayout(ui->verticalLayout);
  75. tmrForRF = new QTimer();
  76. tmrForRF->setInterval(15000);
  77. #ifdef _WIN32
  78. semForRF = CreateSemaphoreA(NULL, 0, 1, "wait-semaphore-5d95950d-a278-4733-a041-ee9fb05ad4e4");
  79. if(semForRF == NULL)
  80. {
  81. qDebug() << "Cannot create semaphore!";
  82. exit(-1);
  83. }
  84. #endif
  85. semThread = new SemaphoreWorkerThread(this, this);
  86. ui->GRA1Check->setCheckState(Qt::Checked);
  87. graCounter = new Counter(1);
  88. // GUI events:
  89. connect(ui->SDRFileButton, &QToolButton::pressed, this, &MainWindow::sdrFDialog);
  90. connect(ui->AnaliseButton, &QPushButton::pressed, this, &MainWindow::analizeButtonPress);
  91. connect(ui->SyncFileButton, &QToolButton::pressed, this, &MainWindow::syncFDialog);
  92. connect(ui->GRAFileButton, &QToolButton::pressed, this, &MainWindow::graFDialog);
  93. connect(ui->GRAFileButton2, &QToolButton::pressed, this, &MainWindow::gra2FDialog);
  94. connect(ui->GRAFileButton3, &QToolButton::pressed, this, &MainWindow::gra3FDialog);
  95. connect(ui->OutPropFileButton, &QToolButton::pressed, this, &MainWindow::outFDialog);
  96. connect(ui->RunButton, &QPushButton::pressed, this, &MainWindow::runButtonPress);
  97. connect(&(this->plotproc), &QProcess::finished, this, &MainWindow::plotprocFinishedSlot);
  98. connect(ui->CancelButton, &QPushButton::pressed, this, &MainWindow::cancelProgram);
  99. connect(ui->SaveProfileButton, &QPushButton::pressed, this, &MainWindow::saveProfilePress);
  100. connect(ui->LoadProfileButton, &QPushButton::pressed, this, &MainWindow::loadProfilePress);
  101. connect(ui->OUTCheck, &QCheckBox::checkStateChanged, this, &MainWindow::OUTcheckBoxEvent);
  102. connect(ui->GRA1Check, &QCheckBox::checkStateChanged, this, &MainWindow::GRA1checkBoxEvent);
  103. connect(ui->GRA2Check, &QCheckBox::checkStateChanged, this, &MainWindow::GRA2checkBoxEvent);
  104. connect(ui->GRA3Check, &QCheckBox::checkStateChanged, this, &MainWindow::GRA3checkBoxEvent);
  105. // Make program logic:
  106. connect(&(this->syncproc), &QProcess::finished, this, &MainWindow::syncFinished); // Kills Sync, sends command to serial port and starts UDP_test
  107. connect(&(this->graproc), &QProcess::finished, graCounter, &Counter::count);
  108. connect(&(this->graproc2), &QProcess::finished, graCounter, &Counter::count);
  109. connect(&(this->graproc3), &QProcess::finished, graCounter, &Counter::count);
  110. connect(graCounter, &Counter::countsEnd, this, &MainWindow::graFinished); // Kills UDP_test.exe, starts pico_test_00_second_copy, starts timer for SDR
  111. connect(semThread, &QThread::finished, this, &MainWindow::startHackRF); // Waits for semaphore/timeout and starts hacrftrans00
  112. connect(&(this->hackrfproc), &QProcess::finished, this, &MainWindow::hackrfFinished); // Kills hackrftrans00
  113. connect(&(this->picoproc), &QProcess::finished, this, &MainWindow::picoFinished); // Kills pico_test_00_second_copy and close the serial port
  114. }
  115. int MainWindow::saveProfilePress()
  116. {
  117. if(!QDir("profiles").exists())
  118. {
  119. QDir().mkdir("profiles");
  120. }
  121. QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::currentPath() + "\\profiles", tr("JSON profiles (*.json)"));
  122. QFile file(filename);
  123. if(!file.open(QIODevice::ReadWrite)) {
  124. qDebug() << "File open error";
  125. } else {
  126. qDebug() <<"JSONTest2 File open!";
  127. }
  128. file.resize(0);
  129. QJsonObject jsonProfile, GRA, GRA1, GRA2, GRA3, SDR, OUTPROP, SYNC;
  130. SYNC.insert("FileName", ui->SyncFileEdit->text());
  131. SYNC.insert("Port", ui->SerialPortBox->currentText());
  132. if(ui->SyncDebugModeCheck->isChecked())
  133. SYNC.insert("Debug Mode", 1);
  134. else
  135. SYNC.insert("Debug Mode", 0);
  136. GRA1.insert("FileName", ui->GRAFileEdit->text());
  137. GRA1.insert("IP", ui->IP1edit->text());
  138. if(ui->GRA1Check->isChecked())
  139. GRA1.insert("enabled", 1);
  140. else
  141. GRA1.insert("enabled", 0);
  142. GRA2.insert("FileName", ui->GRAFileEdit2->text());
  143. GRA2.insert("IP", ui->IP2edit->text());
  144. if(ui->GRA2Check->isChecked())
  145. GRA2.insert("enabled", 1);
  146. else
  147. GRA2.insert("enabled", 0);
  148. GRA3.insert("FileName", ui->GRAFileEdit3->text());
  149. GRA3.insert("IP", ui->IP3edit->text());
  150. if(ui->GRA3Check->isChecked())
  151. GRA3.insert("enabled", 1);
  152. else
  153. GRA3.insert("enabled", 0);
  154. GRA.insert("GRA1", GRA1);
  155. GRA.insert("GRA2", GRA2);
  156. GRA.insert("GRA3", GRA3);
  157. if(ui->GRADebugModeCheck->isChecked())
  158. GRA.insert("Debug Mode", 1);
  159. else
  160. GRA.insert("Debug Mode", 0);
  161. OUTPROP.insert("FileName", ui->OutPropFileEdit->text());
  162. if(ui->OutPropDebugModeCheck->isChecked())
  163. OUTPROP.insert("Debug Mode", 1);
  164. else
  165. OUTPROP.insert("Debug Mode", 0);
  166. if(ui->OUTCheck->isChecked())
  167. OUTPROP.insert("Enabled", 1);
  168. else
  169. OUTPROP.insert("Enabled", 0);
  170. if(ui->BelowCheck->isChecked())
  171. OUTPROP.insert("Below", 1);
  172. else
  173. OUTPROP.insert("Below", 0);
  174. SDR.insert("FileName", ui->SDRFileEdit->text());
  175. SDR.insert("Frequency", ui->FreqBox->text());
  176. SDR.insert("Sample Rate", ui->SampleRateBox->currentText());
  177. SDR.insert("Gain", ui->GainEdit->text());
  178. if(ui->AmplCheck->isChecked())
  179. SDR.insert("Amplified", 1);
  180. else
  181. SDR.insert("Amplified", 0);
  182. jsonProfile.insert("SYNC", SYNC);
  183. jsonProfile.insert("GRA", GRA);
  184. jsonProfile.insert("OUT", OUTPROP);
  185. jsonProfile.insert("SDR", SDR);
  186. QJsonDocument doc;
  187. doc.setObject(jsonProfile);
  188. file.write(doc.toJson());
  189. file.close();
  190. qDebug() << "Writed to JSON: " + filename;
  191. return 0;
  192. }
  193. int MainWindow::loadProfilePress()
  194. {
  195. if(!QDir("profiles").exists())
  196. {
  197. QDir().mkdir("profiles");
  198. }
  199. QString val;
  200. QJsonDocument doc;
  201. QJsonObject obj, SYNC, GRA, GRA1, GRA2, GRA3, OUTPROP, SDR;
  202. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath() + "\\profiles", tr("JSON profiles (*.json)"));
  203. QFile file(filename);
  204. file.open(QIODevice::ReadOnly | QIODevice::Text);
  205. val = file.readAll();
  206. file.close();
  207. doc = QJsonDocument::fromJson(val.toUtf8());
  208. obj = doc.object();
  209. SYNC = obj.value(QString("SYNC")).toObject();
  210. GRA = obj.value(QString("GRA")).toObject();
  211. OUTPROP = obj.value(QString("OUT")).toObject();
  212. SDR = obj.value(QString("SDR")).toObject();
  213. GRA1 = GRA.value(QString("GRA1")).toObject();
  214. GRA2 = GRA.value(QString("GRA2")).toObject();
  215. GRA3 = GRA.value(QString("GRA3")).toObject();
  216. ui->SyncFileEdit->setText(SYNC.value(QString("FileName")).toString());
  217. ui->SerialPortBox->setCurrentText(SYNC.value(QString("Port")).toString());
  218. if(SYNC.value(QString("Debug Mode")).toInt() == 1)
  219. ui->SyncDebugModeCheck->setCheckState(Qt::Checked);
  220. else
  221. ui->SyncDebugModeCheck->setCheckState(Qt::Unchecked);
  222. ui->GRAFileEdit->setText(GRA1.value("FileName").toString());
  223. ui->IP1edit->setText(GRA1.value("IP").toString());
  224. if(GRA1.value(QString("enabled")).toInt() == 1)
  225. ui->GRA1Check->setCheckState(Qt::Checked);
  226. else
  227. ui->GRA1Check->setCheckState(Qt::Unchecked);
  228. ui->GRAFileEdit2->setText(GRA2.value("FileName").toString());
  229. ui->IP2edit->setText(GRA2.value("IP").toString());
  230. if(GRA2.value(QString("enabled")).toInt() == 1)
  231. ui->GRA2Check->setCheckState(Qt::Checked);
  232. else
  233. ui->GRA2Check->setCheckState(Qt::Unchecked);
  234. ui->GRAFileEdit3->setText(GRA3.value("FileName").toString());
  235. ui->IP3edit->setText(GRA3.value("IP").toString());
  236. if(GRA3.value(QString("enabled")).toInt() == 1)
  237. ui->GRA3Check->setCheckState(Qt::Checked);
  238. else
  239. ui->GRA3Check->setCheckState(Qt::Unchecked);
  240. if(GRA.value(QString("Debug Mode")).toInt() == 1)
  241. ui->GRADebugModeCheck->setCheckState(Qt::Checked);
  242. else
  243. ui->GRADebugModeCheck->setCheckState(Qt::Unchecked);
  244. ui->OutPropFileEdit->setText(OUTPROP.value("FileName").toString());
  245. if(OUTPROP.value(QString("Debug Mode")).toInt() == 1)
  246. ui->OutPropDebugModeCheck->setCheckState(Qt::Checked);
  247. else
  248. ui->OutPropDebugModeCheck->setCheckState(Qt::Unchecked);
  249. if(OUTPROP.value(QString("Enabled")).toInt() == 1)
  250. ui->OUTCheck->setCheckState(Qt::Checked);
  251. else
  252. ui->OUTCheck->setCheckState(Qt::Unchecked);
  253. if(OUTPROP.value(QString("Below")).toInt() == 1)
  254. ui->BelowCheck->setCheckState(Qt::Checked);
  255. else
  256. ui->BelowCheck->setCheckState(Qt::Unchecked);
  257. ui->SDRFileEdit->setText(SDR.value("FileName").toString());
  258. ui->FreqBox->setValue(SDR.value("Frequency").toString().toInt());
  259. ui->SampleRateBox->setCurrentText(SDR.value("Sample Rate").toString());
  260. ui->GainEdit->setValue(SDR.value("Gain").toString().toInt());
  261. if(SDR.value(QString("Amplified")).toInt() == 1)
  262. ui->AmplCheck->setCheckState(Qt::Checked);
  263. else
  264. ui->AmplCheck->setCheckState(Qt::Unchecked);
  265. return 0;
  266. }
  267. void MainWindow::GRA1checkBoxEvent()
  268. {
  269. graCounter->reset();
  270. qDebug() << "GRA1CheckBox event!";
  271. if(ui->GRA1Check->isChecked())
  272. {
  273. ui->GRAFileEdit->setEnabled(true);
  274. ui->GRAFileButton->setEnabled(true);
  275. ui->IP1edit->setEnabled(true);
  276. }
  277. else
  278. {
  279. ui->GRAFileEdit->setEnabled(false);
  280. ui->GRAFileButton->setEnabled(false);
  281. ui->IP1edit->setEnabled(false);
  282. }
  283. if((ui->GRA2Check->isChecked() || ui->GRA3Check->isChecked()) && ui->GRA1Check->isChecked())
  284. {
  285. if(ui->GRA2Check->isChecked() && ui->GRA3Check->isChecked())
  286. {
  287. graCounter->rebind(3);
  288. }
  289. else
  290. {
  291. graCounter->rebind(2);
  292. }
  293. }
  294. else if((ui->GRA2Check->isChecked() && ui->GRA3Check->isChecked()) && !ui->GRA1Check->isChecked())
  295. {
  296. graCounter->rebind(2);
  297. }
  298. else
  299. {
  300. graCounter->rebind(1);
  301. }
  302. }
  303. void MainWindow::GRA2checkBoxEvent()
  304. {
  305. graCounter->reset();
  306. qDebug() << "GRA2CheckBox event!";
  307. if(ui->GRA2Check->isChecked())
  308. {
  309. ui->GRAFileEdit2->setEnabled(true);
  310. ui->GRAFileButton2->setEnabled(true);
  311. ui->IP2edit->setEnabled(true);
  312. }
  313. else
  314. {
  315. ui->GRAFileEdit2->setEnabled(false);
  316. ui->GRAFileButton2->setEnabled(false);
  317. ui->IP2edit->setEnabled(false);
  318. }
  319. if((ui->GRA2Check->isChecked() || ui->GRA3Check->isChecked()) && ui->GRA1Check->isChecked())
  320. {
  321. if(ui->GRA2Check->isChecked() && ui->GRA3Check->isChecked())
  322. {
  323. graCounter->rebind(3);
  324. }
  325. else
  326. {
  327. graCounter->rebind(2);
  328. }
  329. }
  330. else if((ui->GRA2Check->isChecked() && ui->GRA3Check->isChecked()) && !ui->GRA1Check->isChecked())
  331. {
  332. graCounter->rebind(2);
  333. }
  334. else
  335. {
  336. graCounter->rebind(1);
  337. }
  338. }
  339. void MainWindow::GRA3checkBoxEvent()
  340. {
  341. graCounter->reset();
  342. qDebug() << "GRA3CheckBox event!";
  343. if(ui->GRA3Check->isChecked())
  344. {
  345. ui->GRAFileEdit3->setEnabled(true);
  346. ui->GRAFileButton3->setEnabled(true);
  347. ui->IP3edit->setEnabled(true);
  348. }
  349. else
  350. {
  351. ui->GRAFileEdit3->setEnabled(false);
  352. ui->GRAFileButton3->setEnabled(false);
  353. ui->IP3edit->setEnabled(false);
  354. }
  355. if((ui->GRA2Check->isChecked() || ui->GRA3Check->isChecked()) && ui->GRA1Check->isChecked())
  356. {
  357. if(ui->GRA2Check->isChecked() && ui->GRA3Check->isChecked())
  358. {
  359. graCounter->rebind(3);
  360. }
  361. else
  362. {
  363. graCounter->rebind(2);
  364. }
  365. }
  366. else if((ui->GRA2Check->isChecked() && ui->GRA3Check->isChecked()) && !ui->GRA1Check->isChecked())
  367. {
  368. graCounter->rebind(2);
  369. }
  370. else
  371. {
  372. graCounter->rebind(1);
  373. }
  374. }
  375. void MainWindow::OUTcheckBoxEvent()
  376. {
  377. if(ui->OUTCheck->isChecked())
  378. {
  379. ui->OutPropFileEdit->setEnabled(true);
  380. ui->OutPropFileButton->setEnabled(true);
  381. }
  382. else
  383. {
  384. ui->OutPropFileEdit->setEnabled(false);
  385. ui->OutPropFileButton->setEnabled(false);
  386. }
  387. }
  388. void MainWindow::sdrFDialog()
  389. {
  390. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\", tr("SDR Sequence (*.bin)"));
  391. ui->SDRFileEdit->setText(filename);
  392. }
  393. void MainWindow::syncFDialog()
  394. {
  395. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\", tr("Synchronization sequence (*.xml)"));
  396. ui->SyncFileEdit->setText(filename);
  397. }
  398. void MainWindow::graFDialog()
  399. {
  400. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\", tr("GRA Sequence (*.txt)"));
  401. ui->GRAFileEdit->setText(filename);
  402. }
  403. void MainWindow::gra2FDialog()
  404. {
  405. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\", tr("GRA Sequence (*.txt)"));
  406. ui->GRAFileEdit2->setText(filename);
  407. }
  408. void MainWindow::gra3FDialog()
  409. {
  410. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\", tr("GRA Sequence (*.txt)"));
  411. ui->GRAFileEdit3->setText(filename);
  412. }
  413. void MainWindow::outFDialog()
  414. {
  415. QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\", tr("Out Properties file (*.xml)"));
  416. ui->OutPropFileEdit->setText(filename);
  417. }
  418. int MainWindow::cancelProgram()
  419. {
  420. syncproc.blockSignals(true);
  421. graproc.blockSignals(true);
  422. graproc2.blockSignals(true);
  423. graproc3.blockSignals(true);
  424. graCounter->blockSignals(true);
  425. picoproc.blockSignals(true);
  426. hackrfproc.blockSignals(true);
  427. tmrForRF->blockSignals(true);
  428. semThread->blockSignals(true);
  429. syncport->close();
  430. syncproc.close();
  431. graCounter->reset();
  432. graproc.close();
  433. graproc2.close();
  434. graproc3.close();
  435. picoproc.close();
  436. hackrfproc.close();
  437. tmrForRF->stop();
  438. ReleaseSemaphore(semForRF, 1, NULL);
  439. semThread->terminate();
  440. picoargs.clear();
  441. hackrfargs.clear();
  442. graargs.clear();
  443. graargs2.clear();
  444. graargs3.clear();
  445. syncargs.clear();
  446. ui->progress->setEnabled(false);
  447. ui->RunButton->setEnabled(true);
  448. ui->CancelButton->setEnabled(false);
  449. ui->GRA2Check->setEnabled(true);
  450. ui->GRA3Check->setEnabled(true);
  451. syncproc.blockSignals(false);
  452. graproc.blockSignals(false);
  453. graCounter->blockSignals(false);
  454. picoproc.blockSignals(false);
  455. hackrfproc.blockSignals(false);
  456. tmrForRF->blockSignals(false);
  457. semThread->blockSignals(false);
  458. return 0;
  459. }
  460. int MainWindow::syncFinished()
  461. {
  462. syncproc.close();
  463. qDebug() << "Hwa";
  464. switch(syncproc.exitCode())
  465. {
  466. case -1:
  467. QMessageBox::critical(this, tr("LMRIgui App"), tr("Wrong arguments for sync!"));
  468. cancelProgram();
  469. return -1;
  470. break;
  471. case -2:
  472. QMessageBox::critical(this, tr("LMRIgui App"), tr("Wrong serial port number!"));
  473. cancelProgram();
  474. return -2;
  475. break;
  476. case -5:
  477. QMessageBox::critical(this, tr("LMRIgui App"), tr("Wrong sync-file structure!"));
  478. cancelProgram();
  479. return -5;
  480. break;
  481. case -6:
  482. QMessageBox::critical(this, tr("LMRIgui App"), tr("No connection to serial port in Sync.exe!"));
  483. cancelProgram();
  484. return -6;
  485. break;
  486. }
  487. pvalue = pvalue + 12;
  488. ui->progress->setValue(pvalue);
  489. syncport->setPortName(portInfos[ui->SerialPortBox->currentIndex()].portName());
  490. syncport->setBaudRate(QSerialPort::Baud9600);
  491. syncport->setDataBits(QSerialPort::Data8);
  492. syncport->setParity(QSerialPort::NoParity);
  493. syncport->setStopBits(QSerialPort::OneStop);
  494. syncport->setFlowControl(QSerialPort::NoFlowControl);
  495. if(!syncport->open(QIODevice::ReadWrite))
  496. {
  497. QMessageBox::critical(this, tr("LMRIgui App"), tr((syncport->portName() + QString(" cannot be connected!")).toStdString().c_str()));
  498. cancelProgram();
  499. return -3;
  500. }
  501. //while(syncport->isOpen())
  502. //{
  503. if(!syncport->write("e"))
  504. {
  505. QMessageBox::critical(this, tr("LMRIgui App"), tr("Cannot send data to serial port!"));
  506. cancelProgram();
  507. emit progress->canceled();
  508. return -4;
  509. };
  510. //syncport->close();
  511. //}
  512. pvalue = pvalue + 13;
  513. ui->progress->setValue(pvalue);
  514. for(QString arg : graargs)
  515. {
  516. qDebug() << arg;
  517. }
  518. if(ui->GRA1Check->isChecked())
  519. {
  520. graproc.start(QDir::currentPath() + QString("\\UDP_test.exe"), graargs);
  521. //qDebug() << QDir::currentPath();
  522. if(!graproc.waitForStarted())
  523. {
  524. QMessageBox::critical(this, tr("LMRIgui App"), tr("GRA1 programm not started"));
  525. cancelProgram();
  526. return -7;
  527. }
  528. }
  529. if(ui->GRA2Check->isChecked())
  530. {
  531. graproc2.start(QDir::currentPath() + QString("\\UDP_test.exe"), graargs2);
  532. if(!graproc2.waitForStarted())
  533. {
  534. QMessageBox::critical(this, tr("LMRIgui App"), tr("GRA2 programm not started"));
  535. cancelProgram();
  536. return -7;
  537. }
  538. }
  539. if(ui->GRA3Check->isChecked())
  540. {
  541. graproc3.start(QDir::currentPath() + QString("\\UDP_test.exe"), graargs3);
  542. if(!graproc3.waitForStarted())
  543. {
  544. QMessageBox::critical(this, tr("LMRIgui App"), tr("GRA2 programm not started"));
  545. cancelProgram();
  546. return -7;
  547. }
  548. }
  549. if(!(ui->GRA1Check->isChecked() || ui->GRA2Check->isChecked() || ui->GRA3Check->isChecked()))
  550. {
  551. graCounter->count();
  552. }
  553. return 0;
  554. }
  555. int MainWindow::readOutput()
  556. {
  557. for(int i = 0; i < test; i++)
  558. {
  559. qDebug() << picoproc.readAll();
  560. }
  561. return 0;
  562. }
  563. int MainWindow::graFinished()
  564. {
  565. graCounter->reset();
  566. graproc.close();
  567. graproc2.close();
  568. graproc3.close();
  569. pvalue = pvalue + 25;
  570. ui->progress->setValue(pvalue);
  571. for(QString arg : graargs)
  572. {
  573. qDebug() << arg;
  574. }
  575. semThread->start();
  576. tmrForRF->start();
  577. qDebug() << ui->OUTCheck->isChecked();
  578. if(ui->OUTCheck->isChecked())
  579. picoproc.start(QDir::currentPath() + QString("\\pico_test_00_second_copy.exe"), picoargs);
  580. else
  581. ReleaseSemaphore(semForRF, 1, NULL);
  582. //test = 3;
  583. //connect(&(this->picoproc), &QProcess::readyReadStandardOutput, this, &MainWindow::readOutput);
  584. return 0;
  585. }
  586. int MainWindow::startHackRF()
  587. {
  588. tmrForRF->stop();
  589. hackrfproc.start(QDir::currentPath() + QString("\\hackrftrans00.exe"), hackrfargs);
  590. qDebug() << "startHackRF 1";
  591. if(!hackrfproc.waitForStarted())
  592. {
  593. QMessageBox::critical(this, tr("LMRIgui App"), tr("SDR programm not started"));
  594. cancelProgram();
  595. return -9;
  596. }
  597. qDebug() << "startHackRF 2";
  598. return 0;
  599. }
  600. int MainWindow::hackrfFinished()
  601. {
  602. hackrfproc.close();
  603. pvalue = pvalue + 25;
  604. ui->progress->setValue(pvalue);
  605. qDebug() << "hackrfFinished 1";
  606. if(!ui->OUTCheck->isChecked())
  607. {
  608. pvalue = pvalue + 25;
  609. programFinished();
  610. }
  611. return 0;
  612. }
  613. int MainWindow::picoFinished()
  614. {
  615. picoproc.close();
  616. syncport->close();
  617. pvalue = pvalue + 25;
  618. ui->progress->setValue(pvalue);
  619. qDebug() << "picoFinished 1";
  620. programFinished();
  621. return 0;
  622. }
  623. int MainWindow::programFinished()
  624. {
  625. QMessageBox::information(this, tr("LMRIgui App"), tr("Program successufuly finished!"));
  626. tmrForRF->stop();
  627. graCounter->reset();
  628. semThread->blockSignals(true);
  629. semThread->terminate();
  630. semThread->blockSignals(false);
  631. picoargs.clear();
  632. hackrfargs.clear();
  633. graargs.clear();
  634. graargs2.clear();
  635. graargs3.clear();
  636. syncargs.clear();
  637. syncport->close();
  638. ui->progress->setEnabled(false);
  639. ui->RunButton->setEnabled(true);
  640. ui->CancelButton->setEnabled(false);
  641. ui->GRA2Check->setEnabled(true);
  642. ui->GRA3Check->setEnabled(true);
  643. return 0;
  644. }
  645. int MainWindow::runButtonPress()
  646. {
  647. if(ui->GRA2Check->isChecked() && (ui->GRAFileEdit2->text()=="" || ui->IP2edit->text()==""))
  648. {
  649. QMessageBox::warning(this, tr("LMRIgui App"), tr("Please, enter all file destinations!"));
  650. return -1;
  651. }
  652. if(ui->GRA3Check->isChecked() && (ui->GRAFileEdit3->text()=="" || ui->IP3edit->text()==""))
  653. {
  654. QMessageBox::warning(this, tr("LMRIgui App"), tr("Please, enter all file destinations!"));
  655. return -1;
  656. }
  657. if(ui->OUTCheck->isChecked() && ui->OutPropFileEdit->text() != "")
  658. {
  659. QMessageBox::warning(this, tr("LMRIgui App"), tr("Please, enter all file destinations!"));
  660. return -1;
  661. }
  662. if(ui->SyncFileEdit->text() != "" && ((ui->GRAFileEdit->text() != "" && ui->IP1edit->text() != "") || !ui->GRA2Check->isChecked()) && ui->SDRFileEdit->text() != "")
  663. {
  664. syncargs << ui->SyncFileEdit->text();
  665. graargs << ui->GRAFileEdit->text() << ui->IP1edit->text();
  666. if(ui->GRA2Check->isChecked())
  667. graargs2 << ui->GRAFileEdit2->text() << ui->IP2edit->text();
  668. if(ui->GRA3Check->isChecked())
  669. graargs3 << ui->GRAFileEdit3->text() << ui->IP3edit->text();
  670. hackrfargs << "-t" << ui->SDRFileEdit->text();
  671. hackrfargs << "-f" << ui->FreqBox->text();
  672. hackrfargs << "-a" << (ui->AmplCheck->isChecked() ? QString("1") : QString("0"));
  673. hackrfargs << "-x" << ui->GainEdit->text();
  674. if(ui->SampleRateBox->currentText() == "20 MHz")
  675. {
  676. hackrfargs << "-s" << "20000000";
  677. }
  678. else if(ui->SampleRateBox->currentText() == "16 MHz")
  679. {
  680. hackrfargs << "-s" << "16000000";
  681. }
  682. else if(ui->SampleRateBox->currentText() == "12.5 MHz")
  683. {
  684. hackrfargs << "-s" << "12500000";
  685. }
  686. else if(ui->SampleRateBox->currentText() == "10 MHz")
  687. {
  688. hackrfargs << "-s" << "10000000";
  689. }
  690. else if(ui->SampleRateBox->currentText() == "8 MHz")
  691. {
  692. hackrfargs << "-s" << "8000000";
  693. }
  694. else if(ui->SampleRateBox->currentText() == "2 MHz")
  695. {
  696. hackrfargs << "-s" << "2000000";
  697. }
  698. qDebug() << hackrfargs;
  699. picoargs << ui->OutPropFileEdit->text();
  700. }
  701. else
  702. {
  703. QMessageBox::warning(this, tr("LMRIgui App"), tr("Please, enter all file destinations!"));
  704. return -1;
  705. }
  706. if(true)
  707. {
  708. pvalue = 0;
  709. ui->progress->setEnabled(true);
  710. ui->progress->setValue(pvalue);
  711. ui->GRA2Check->setEnabled(false);
  712. ui->GRA3Check->setEnabled(false);
  713. ui->RunButton->setEnabled(false);
  714. ui->CancelButton->setEnabled(true);
  715. if(ui->SyncDebugModeCheck->isChecked())
  716. {
  717. syncargs << "--debug";
  718. }
  719. QString pnum = portInfos[ui->SerialPortBox->currentIndex()].portName().mid(3);
  720. syncargs << "-p" << pnum;
  721. if(ui->GRADebugModeCheck->isChecked())
  722. {
  723. graargs << "--debug";
  724. }
  725. if(ui->OutPropDebugModeCheck->isChecked())
  726. {
  727. picoargs << "--debug";
  728. }
  729. if(ui->BelowCheck->isChecked())
  730. {
  731. picoargs << "--below";
  732. }
  733. syncproc.start(QDir::currentPath() + QString("\\Sync.exe"), syncargs);
  734. syncargs.clear();
  735. if(!syncproc.waitForStarted())
  736. {
  737. QMessageBox::critical(this, tr("LMRIgui App"), tr("Sync not started!"));
  738. cancelProgram();
  739. return -11;
  740. }
  741. qDebug() << hackrfargs;
  742. }
  743. return 0;
  744. }
  745. int MainWindow::analizeButtonPress()
  746. {
  747. QStringList plotargs;
  748. plotargs << QString("plot.py");
  749. plotproc.start("python.exe", plotargs);
  750. if(!plotproc.waitForStarted())
  751. {
  752. QMessageBox::critical(this, tr("LMRIgui App"), tr("Analize App not started!"));
  753. cancelProgram();
  754. return -11;
  755. }
  756. return 0;
  757. }
  758. void MainWindow::plotprocFinishedSlot()
  759. {
  760. std::string output = plotproc.readAllStandardOutput().toStdString();
  761. qDebug() << QString("C:\\test.py") << ": " << output;
  762. plotproc.close();
  763. }
  764. MainWindow::~MainWindow()
  765. {
  766. delete ui;
  767. }