mainwindow.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QLineEdit>
  4. #include <QTextEdit>
  5. #include "QDebug"
  6. #include <QThread>
  7. #include <malloc.h>
  8. #include "modbus/modbus.h"
  9. #include "modbus/modbus-private.h"
  10. #include "QFile"
  11. #include <QMessageBox>
  12. MainWindow::MainWindow(QWidget *parent)
  13. : QMainWindow(parent)
  14. , ui(new Ui::MainWindow)
  15. , m_channel(new ChannelInfo[MAX_CHANNELS])
  16. , keyboard(new SoftKeyBoard)
  17. , port(nullptr)
  18. , land(nullptr)
  19. , m_timer(new QTimer(this))
  20. , m_thread(new QThread(this))
  21. , m_read(new ReadModbus)
  22. , m_key(new GpioButten)
  23. {
  24. ui->setupUi(this);
  25. keyboard->setFocusPolicy(Qt::NoFocus);
  26. keyboard->hide();
  27. //land = new Landscape(this);
  28. for(int i = 0; i < MAX_CHANNELS ;i++)
  29. {
  30. m_channel[i].chid = i+1;
  31. m_channel[i].chV = 0.00;
  32. m_channel[i].chStatus = 0;
  33. m_channel[i].chI = 0.00;
  34. }
  35. modbus_init();
  36. m_read->set_main(this);
  37. m_read->moveToThread(m_thread);
  38. // ui->stackedWidget->insertWidget(0,port);
  39. // ui->stackedWidget->insertWidget(1,land);
  40. port = new Portraitmode (this);
  41. land = new Landscape(this);
  42. //ui->verticalLayout->addWidget(port);
  43. ui->stackedWidget->insertWidget(0,port);
  44. ui->stackedWidget->insertWidget(1,land);
  45. connect(land,&Landscape::stop_signal,this,&MainWindow::opt_stop_or_start_signal);
  46. connect(port,&Portraitmode::stop_signal,this,&MainWindow::opt_stop_or_start_signal);
  47. connect(this,&MainWindow::Notice,land,&Landscape::test_modbus_info);
  48. connect(this,&MainWindow::Notice,port,&Portraitmode::test_modbus_info);
  49. connect(m_thread, &QThread::started, m_read, &ReadModbus::ReadData);
  50. connect(m_key,&GpioButten::pressed,this,&MainWindow::route);
  51. sysTime_zone = QTimeZone::systemTimeZone();
  52. qDebug() << sysTime_zone.id().data();
  53. if(QByteArray("UTC") == sysTime_zone.id().data())
  54. {
  55. qDebug() << "UTC ---> GMT-8";
  56. system("ln -sf /usr/share/zoneinfo/GMT-8 /etc/localtime");
  57. sysTime_zone = QTimeZone::systemTimeZone();
  58. }
  59. m_sence = new QGraphicsScene;
  60. m_view = new QGraphicsView(m_sence);
  61. m_screen = QApplication::primaryScreen();
  62. m_proxy = m_sence->addWidget(this);
  63. screen_info = m_screen->geometry();
  64. rotation = 90;
  65. qDebug() << screen_info.width() << " " << screen_info.height();
  66. ui->gridLayout->setContentsMargins(0,0,0,0);
  67. //ui->stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  68. if(rotation == 0 || rotation == 180)
  69. {
  70. m_proxy->setRotation(rotation);
  71. resize(screen_info.width(),screen_info.height());
  72. m_view->setSceneRect(0, 0, screen_info.width(), screen_info.height());
  73. if(rotation == 0)
  74. {
  75. m_proxy->setPos(0, 0);
  76. }else
  77. m_proxy->setPos(screen_info.width(),screen_info.height());
  78. land->block();
  79. port->reset_widget(page);
  80. ui->stackedWidget->setCurrentIndex(0);
  81. }
  82. else
  83. {
  84. resize(screen_info.height(),screen_info.width());
  85. if(rotation == 90)
  86. {
  87. m_proxy->setPos(screen_info.width(), 0);
  88. }else
  89. {
  90. m_proxy->setPos(0, screen_info.height());
  91. }
  92. m_proxy->setRotation(rotation);
  93. port->block();
  94. land->reset_widget(page);
  95. ui->stackedWidget->setCurrentIndex(1);
  96. }
  97. m_view->setFrameStyle(QFrame::NoFrame);
  98. m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  99. m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  100. m_view->showFullScreen();
  101. suspend_flag = 0;
  102. m_thread->start();
  103. }
  104. const SoftKeyBoard* MainWindow :: get_keyboard() const
  105. {
  106. return keyboard;
  107. }
  108. QTimeZone MainWindow :: get_zone()
  109. {
  110. return sysTime_zone;
  111. }
  112. ChannelInfo* MainWindow :: get_channel_info()
  113. {
  114. return m_channel;
  115. }
  116. void MainWindow:: modbus_init()
  117. {
  118. strcpy(modbus.serial_name, "ttyUSB0");
  119. modbus.serial_parity = 'N';
  120. modbus.serial_banud = 115200;
  121. modbus.serial_stop = 1;
  122. modbus.serial_data_bit = 8;
  123. modbus.slave_addr = 1;
  124. char buff [32] = {0};
  125. sprintf(buff,"/dev/%s",modbus.serial_name);
  126. m_mb = modbus_new_rtu(buff,modbus.serial_banud, modbus.serial_parity, modbus.serial_data_bit, modbus.serial_data_bit);
  127. modbus_connect(m_mb);
  128. modbus_rtu_set_rts(m_mb, MODBUS_RTU_RTS_NONE);
  129. modbus_set_error_recovery(m_mb,MODBUS_ERROR_RECOVERY_PROTOCOL);
  130. modbus_set_response_timeout(m_mb, 0,200*1000);
  131. modbus_set_byte_timeout(m_mb,0,50000);
  132. modbus_set_slave(m_mb, 1);
  133. }
  134. int MainWindow::modbus_set(modbus_info_t &md_info)
  135. {
  136. modbus = md_info;
  137. if(m_mb)
  138. {
  139. modbus_close(m_mb);
  140. modbus_free(m_mb);
  141. m_mb = NULL;
  142. }
  143. char buff [32] = {0};
  144. sprintf(buff,"/dev/%s",modbus.serial_name);
  145. m_mb = modbus_new_rtu(buff,modbus.serial_banud, modbus.serial_parity, modbus.serial_data_bit, modbus.serial_data_bit);
  146. modbus_rtu_set_rts(m_mb, MODBUS_RTU_RTS_NONE);
  147. modbus_set_error_recovery(m_mb,MODBUS_ERROR_RECOVERY_PROTOCOL);
  148. modbus_set_response_timeout(m_mb, 0,200*1000);
  149. modbus_set_byte_timeout(m_mb,0,50000);
  150. modbus_set_slave(m_mb, modbus.slave_addr);
  151. int ret = modbus_connect(m_mb);
  152. return ret;
  153. }
  154. uint8_t MainWindow::get_suspend_flag()
  155. {
  156. return suspend_flag;
  157. }
  158. void MainWindow::route()
  159. {
  160. qDebug() << screen_info.width() << " " << screen_info.height();
  161. rotation += 90;
  162. if(rotation >= 360)
  163. rotation = 0;
  164. ui->gridLayout->setContentsMargins(0,0,0,0);
  165. if(rotation == 0 || rotation == 180)
  166. {
  167. resize(screen_info.width(),screen_info.height());
  168. m_view->setSceneRect(0, 0, screen_info.width(), screen_info.height());
  169. if(rotation == 0)
  170. {
  171. m_proxy->setPos(0, 0);
  172. }else
  173. m_proxy->setPos(screen_info.width(),screen_info.height());
  174. m_proxy->setRotation(rotation);
  175. ui->stackedWidget->setCurrentIndex(0);
  176. land->block();
  177. port->reset_widget(page);
  178. //port->show();
  179. //qApp->installEventFilter(port);
  180. }
  181. else
  182. {
  183. resize(screen_info.height(),screen_info.width());
  184. m_view->setSceneRect(0, 0, screen_info.width(), screen_info.height());
  185. //m_view->setSceneRect(0, 0, screen_info.height(), screen_info.width());
  186. if(rotation == 90)
  187. {
  188. m_proxy->setPos(screen_info.width(), 0);
  189. }else
  190. {
  191. m_proxy->setPos(0, screen_info.height());
  192. }
  193. m_proxy->setRotation(rotation);
  194. ui->stackedWidget->setCurrentIndex(1);
  195. port->block();
  196. land->reset_widget(page);
  197. }
  198. //ui->stackedWidget->setGeometry(centralWidget()->rect());
  199. m_view->setFrameStyle(QFrame::NoFrame);
  200. m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  201. m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  202. m_view->showFullScreen();
  203. // qDebug() << "Window geometry:" << this->geometry();
  204. // qDebug() << "StackedWidget geometry:" << ui->stackedWidget->geometry();
  205. // qDebug() << "StackedWidget pos:" << ui->stackedWidget->pos();
  206. //m_view->showFullScreen();
  207. // m_view->update();
  208. }
  209. void MainWindow::test_modbus()
  210. {
  211. int ret = 0;
  212. uint16_t data[128] = {0};
  213. QString send = "";
  214. QString recived = "";
  215. uint8_t send_flag = 0;
  216. uint8_t revice_flag = 0;
  217. ret = modbus_read_registers(m_mb,_SWITCH_T_AC_TYPE_CHN_INFO,1,data);
  218. send = QString("发送: addr:%1,reg:%2,cnt:%3").arg(m_mb->slave).arg(_SWITCH_T_AC_TYPE_CHN_INFO).arg(1);
  219. send_flag = 1;
  220. if(ret > 0)
  221. {
  222. pdu_all_info.AllChs = data[0] & 0XFF;
  223. pdu_all_info.Type =(data[0] >> 8) & 0XFF;
  224. recived = QString("接收: addr:%1,reg:%2,cnt:%3,val:%4").arg(m_mb->slave).arg(_SWITCH_T_AC_TYPE_CHN_INFO).arg(1).arg((uint32_t)data[0],4,16,QChar('0'));
  225. revice_flag = 1;
  226. }else
  227. {
  228. pdu_all_info.AllChs = 0;
  229. pdu_all_info.Type = 0;
  230. }
  231. emit Notice(send,recived,send_flag,revice_flag);
  232. }
  233. const modbus_t* MainWindow:: get_mb() const
  234. {
  235. return m_mb;
  236. }
  237. void MainWindow::read_pdu_data()
  238. {
  239. int ret = 0;
  240. uint16_t data[128] = {0};
  241. ret = modbus_read_registers(m_mb,_SWITCH_T_AC_TYPE_CHN_INFO,1,data);
  242. if(ret > 0)
  243. {
  244. pdu_all_info.AllChs = data[0] & 0XFF;
  245. pdu_all_info.Type =(data[0] >> 8) & 0XFF;
  246. }else
  247. {
  248. pdu_all_info.AllChs = 0;
  249. pdu_all_info.Type = 0;
  250. }
  251. QThread::msleep(30);
  252. ret = modbus_read_registers(m_mb,_SWITCH_T_AC_IN_L1_INFO,24,data);
  253. if(ret > 0)
  254. {
  255. pdu_all_info.AllVoltage_L1 = ((data[1] << 16) + data[0] )/1000.0f; // Low byte
  256. pdu_all_info.AllCurrent_L1 = ((data[3] << 16) + data[2] )/1000.0f; // Low byte
  257. pdu_all_info.AllPower_L1 = ((data[5] << 16) + data[4] )/1000.0f; // Low byte
  258. pdu_all_info.AllConsumption_L1 = ((data[7] << 16)+ data[6])/1000.0f; // Low byte
  259. pdu_all_info.AllVoltage_L2 = ((data[9] << 16) + data[8] )/1000.0f; // Low byte
  260. pdu_all_info.AllCurrent_L2 = ((data[11] << 16) + data[10] )/1000.0f; // Low byte
  261. pdu_all_info.AllPower_L2 = ((data[13] << 16) + data[12] )/1000.0f; // Low byte
  262. pdu_all_info.AllConsumption_L2 = ((data[15] << 16)+ data[14])/1000.0f; // Low byte
  263. pdu_all_info.AllVoltage_L3 = ((data[17] << 16) + data[16] )/1000.0f; // Low byte
  264. pdu_all_info.AllCurrent_L3 = ((data[19] << 16) + data[18] )/1000.0f; // Low byte
  265. pdu_all_info.AllPower_L3 = ((data[21] << 16) + data[20] )/1000.0f; // Low byte
  266. pdu_all_info.AllConsumption_L3 = ((data[23] << 16)+ data[22])/1000.0f; // Low byte
  267. //qDebug() << pdu_all_info.AllVoltage_L1 << " " << pdu_all_info.AllVoltage_L2 << " " << pdu_all_info.AllVoltage_L3;
  268. }else
  269. {
  270. pdu_all_info.AllVoltage_L1 =0; // Low byte
  271. pdu_all_info.AllCurrent_L1 = 0; // Low byte
  272. pdu_all_info.AllPower_L1 = 0; // Low byte
  273. pdu_all_info.AllConsumption_L1 = 0; // Low byte
  274. pdu_all_info.AllVoltage_L2 = 0; // Low byte
  275. pdu_all_info.AllCurrent_L2 = 0; // Low byte
  276. pdu_all_info.AllPower_L2 = 0; // Low byte
  277. pdu_all_info.AllConsumption_L2 = 0; // Low byte
  278. pdu_all_info.AllVoltage_L3 = 0; // Low byte
  279. pdu_all_info.AllCurrent_L3 = 0; // Low byte
  280. pdu_all_info.AllPower_L3 = 0; // Low byte
  281. pdu_all_info.AllConsumption_L3 = 0; // Low byte
  282. }
  283. QThread::msleep(30);
  284. for(int i = 0; i < MAX_CHANNELS;i++)
  285. {
  286. float VL1 = 0;
  287. float IL1 = 0;
  288. float PL1 = 0;
  289. float CL1 = 0;
  290. float VL2 = 0;
  291. float IL2 = 0;
  292. float PL2 = 0;
  293. float CL2 = 0;
  294. float VL3 = 0;
  295. float IL3 = 0;
  296. float PL3 = 0;
  297. float CL3 = 0;
  298. ret = modbus_read_registers(m_mb,_SWITCH_T_AC_CH_PHASE_INFO+(i*30),30,data);
  299. if(ret > 0)
  300. {
  301. VL1=((data[1] << 16) + data[0] )/1000.0f;
  302. IL1=((data[3] << 16) + data[2] )/1000.0f;
  303. PL1=((data[5] << 16) + data[4] )/1000.0f;
  304. CL1=((data[7] << 16) + data[6] )/1000.0f;
  305. VL2=((data[11] << 16) + data[10] )/1000.0f;
  306. IL2=((data[13] << 16) + data[12] )/1000.0f;
  307. PL2=((data[15] << 16) + data[14] )/1000.0f;
  308. CL2=((data[17] << 16) + data[16] )/1000.0f;
  309. VL3=((data[21] << 16) + data[20] )/1000.0f;
  310. IL3=((data[23] << 16) + data[22] )/1000.0f;
  311. PL3=((data[25] << 16) + data[24] )/1000.0f;
  312. CL3=((data[27] << 16) + data[26] )/1000.0f;
  313. }
  314. if(VL1>=VL2 && VL1>=VL3)
  315. {
  316. pdu_ch_info[i].CHVoltage=VL1;
  317. pdu_ch_info[i].CHCurrent=IL1;
  318. pdu_ch_info[i].CHPower=PL1;
  319. pdu_ch_info[i].CHConsumption=CL1;
  320. }
  321. if(VL2>=VL1 && VL2>=VL3)
  322. {
  323. pdu_ch_info[i].CHVoltage=VL2;
  324. pdu_ch_info[i].CHCurrent=IL2;
  325. pdu_ch_info[i].CHPower=PL2;
  326. pdu_ch_info[i].CHConsumption=CL2;
  327. }
  328. if(VL3>=VL1 && VL3>=VL2)
  329. {
  330. pdu_ch_info[i].CHVoltage=VL3;
  331. pdu_ch_info[i].CHCurrent=IL3;
  332. pdu_ch_info[i].CHPower=PL3;
  333. pdu_ch_info[i].CHConsumption=CL3;
  334. }
  335. QThread::msleep(20);
  336. }
  337. ret = modbus_read_registers(m_mb,_SWITCH_T_AC_CH_STATUS,MAX_CHANNELS,data);
  338. if(ret > 0)
  339. {
  340. for(int i = 0; i < MAX_CHANNELS;i++)
  341. pdu_ch_info[i].CHStatus= data[i] & 0xFFFF; // Low byte
  342. }else
  343. {
  344. for(int i = 0; i < MAX_CHANNELS;i++)
  345. pdu_ch_info[i].CHStatus= 0; // Low byte
  346. }
  347. QThread::msleep(20);
  348. ret = modbus_read_registers(m_mb,_SWITCH_T_AC_SENSOR_INFO,16,data);
  349. if(ret > 0)
  350. {
  351. pdu_all_info.Sensor_Temperature = ((data[1] << 16) + data[0] )/1000.0f;
  352. pdu_all_info.Sensor_Humidity = ((data[3] << 16) + data[2] )/1000.0f;
  353. pdu_all_info.Sensor_LeakFlag=((data[5] << 16) + data[4] );
  354. pdu_all_info.Sensor_INTemperature = ((data[9] << 16)+ data[8])/1000.0f;
  355. pdu_all_info.Sensor_IN_Pressure = ((data[11] << 16)+ data[10])/1000.0f;
  356. pdu_all_info.Sensor_OUTTemperature = ((data[13] << 16)+ data[12])/1000.0f;
  357. pdu_all_info.Sensor_OUT_Pressure = ((data[11] << 16)+ data[10])/1000.0f;
  358. }else
  359. {
  360. pdu_all_info.Sensor_Temperature = 0;
  361. pdu_all_info.Sensor_Humidity = 0;
  362. pdu_all_info.Sensor_LeakFlag=0;
  363. pdu_all_info.Sensor_INTemperature = 0;
  364. pdu_all_info.Sensor_IN_Pressure = 0;
  365. pdu_all_info.Sensor_OUTTemperature = 0;
  366. pdu_all_info.Sensor_OUT_Pressure = 0;
  367. }
  368. }
  369. MainWindow::~MainWindow()
  370. {
  371. delete ui;
  372. delete [] m_channel;
  373. delete land;
  374. modbus_close(m_mb);
  375. modbus_free(m_mb);
  376. m_mb = NULL;
  377. delete m_key;
  378. }
  379. modbus_info_t MainWindow:: get_modbus_info()
  380. {
  381. return modbus;
  382. }
  383. void MainWindow:: opt_stop_or_start_signal(bool val)
  384. {
  385. if(val == true)
  386. {
  387. suspend_flag = 1;
  388. // m_timer->stop();
  389. }else
  390. {
  391. suspend_flag = 0;
  392. // m_timer->start();
  393. }
  394. }