| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QLineEdit>
- #include <QTextEdit>
- #include "QDebug"
- #include <QThread>
- #include <malloc.h>
- #include "modbus/modbus.h"
- #include "modbus/modbus-private.h"
- #include "QFile"
- #include <QMessageBox>
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- , m_channel(new ChannelInfo[MAX_CHANNELS])
- , keyboard(new SoftKeyBoard)
- , port(nullptr)
- , land(nullptr)
- , m_timer(new QTimer(this))
- , m_thread(new QThread(this))
- , m_read(new ReadModbus)
- , m_key(new GpioButten)
- {
- ui->setupUi(this);
- keyboard->setFocusPolicy(Qt::NoFocus);
- keyboard->hide();
- //land = new Landscape(this);
- for(int i = 0; i < MAX_CHANNELS ;i++)
- {
- m_channel[i].chid = i+1;
- m_channel[i].chV = 0.00;
- m_channel[i].chStatus = 0;
- m_channel[i].chI = 0.00;
- }
- modbus_init();
- m_read->set_main(this);
- m_read->moveToThread(m_thread);
- // ui->stackedWidget->insertWidget(0,port);
- // ui->stackedWidget->insertWidget(1,land);
- port = new Portraitmode (this);
- land = new Landscape(this);
- //ui->verticalLayout->addWidget(port);
- ui->stackedWidget->insertWidget(0,port);
- ui->stackedWidget->insertWidget(1,land);
- connect(land,&Landscape::stop_signal,this,&MainWindow::opt_stop_or_start_signal);
- connect(port,&Portraitmode::stop_signal,this,&MainWindow::opt_stop_or_start_signal);
- connect(this,&MainWindow::Notice,land,&Landscape::test_modbus_info);
- connect(this,&MainWindow::Notice,port,&Portraitmode::test_modbus_info);
- connect(m_thread, &QThread::started, m_read, &ReadModbus::ReadData);
- connect(m_key,&GpioButten::pressed,this,&MainWindow::route);
- sysTime_zone = QTimeZone::systemTimeZone();
- qDebug() << sysTime_zone.id().data();
- if(QByteArray("UTC") == sysTime_zone.id().data())
- {
- qDebug() << "UTC ---> GMT-8";
- system("ln -sf /usr/share/zoneinfo/GMT-8 /etc/localtime");
- sysTime_zone = QTimeZone::systemTimeZone();
- }
- m_sence = new QGraphicsScene;
- m_view = new QGraphicsView(m_sence);
- m_screen = QApplication::primaryScreen();
- m_proxy = m_sence->addWidget(this);
- screen_info = m_screen->geometry();
- rotation = 90;
- qDebug() << screen_info.width() << " " << screen_info.height();
- ui->gridLayout->setContentsMargins(0,0,0,0);
- //ui->stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
- if(rotation == 0 || rotation == 180)
- {
- m_proxy->setRotation(rotation);
- resize(screen_info.width(),screen_info.height());
- m_view->setSceneRect(0, 0, screen_info.width(), screen_info.height());
- if(rotation == 0)
- {
- m_proxy->setPos(0, 0);
- }else
- m_proxy->setPos(screen_info.width(),screen_info.height());
- land->block();
- port->reset_widget(page);
- ui->stackedWidget->setCurrentIndex(0);
- }
- else
- {
- resize(screen_info.height(),screen_info.width());
- if(rotation == 90)
- {
- m_proxy->setPos(screen_info.width(), 0);
- }else
- {
- m_proxy->setPos(0, screen_info.height());
- }
- m_proxy->setRotation(rotation);
- port->block();
- land->reset_widget(page);
- ui->stackedWidget->setCurrentIndex(1);
- }
- m_view->setFrameStyle(QFrame::NoFrame);
- m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- m_view->showFullScreen();
- suspend_flag = 0;
- m_thread->start();
- }
- const SoftKeyBoard* MainWindow :: get_keyboard() const
- {
- return keyboard;
- }
- QTimeZone MainWindow :: get_zone()
- {
- return sysTime_zone;
- }
- ChannelInfo* MainWindow :: get_channel_info()
- {
- return m_channel;
- }
- void MainWindow:: modbus_init()
- {
- strcpy(modbus.serial_name, "ttyUSB0");
- modbus.serial_parity = 'N';
- modbus.serial_banud = 115200;
- modbus.serial_stop = 1;
- modbus.serial_data_bit = 8;
- modbus.slave_addr = 1;
- char buff [32] = {0};
- sprintf(buff,"/dev/%s",modbus.serial_name);
- m_mb = modbus_new_rtu(buff,modbus.serial_banud, modbus.serial_parity, modbus.serial_data_bit, modbus.serial_data_bit);
- modbus_connect(m_mb);
- modbus_rtu_set_rts(m_mb, MODBUS_RTU_RTS_NONE);
- modbus_set_error_recovery(m_mb,MODBUS_ERROR_RECOVERY_PROTOCOL);
- modbus_set_response_timeout(m_mb, 0,200*1000);
- modbus_set_byte_timeout(m_mb,0,50000);
- modbus_set_slave(m_mb, 1);
- }
- int MainWindow::modbus_set(modbus_info_t &md_info)
- {
- modbus = md_info;
- if(m_mb)
- {
- modbus_close(m_mb);
- modbus_free(m_mb);
- m_mb = NULL;
- }
- char buff [32] = {0};
- sprintf(buff,"/dev/%s",modbus.serial_name);
- m_mb = modbus_new_rtu(buff,modbus.serial_banud, modbus.serial_parity, modbus.serial_data_bit, modbus.serial_data_bit);
- modbus_rtu_set_rts(m_mb, MODBUS_RTU_RTS_NONE);
- modbus_set_error_recovery(m_mb,MODBUS_ERROR_RECOVERY_PROTOCOL);
- modbus_set_response_timeout(m_mb, 0,200*1000);
- modbus_set_byte_timeout(m_mb,0,50000);
- modbus_set_slave(m_mb, modbus.slave_addr);
- int ret = modbus_connect(m_mb);
- return ret;
- }
- uint8_t MainWindow::get_suspend_flag()
- {
- return suspend_flag;
- }
- void MainWindow::route()
- {
- qDebug() << screen_info.width() << " " << screen_info.height();
- rotation += 90;
- if(rotation >= 360)
- rotation = 0;
- ui->gridLayout->setContentsMargins(0,0,0,0);
- if(rotation == 0 || rotation == 180)
- {
- resize(screen_info.width(),screen_info.height());
- m_view->setSceneRect(0, 0, screen_info.width(), screen_info.height());
- if(rotation == 0)
- {
- m_proxy->setPos(0, 0);
- }else
- m_proxy->setPos(screen_info.width(),screen_info.height());
- m_proxy->setRotation(rotation);
- ui->stackedWidget->setCurrentIndex(0);
- land->block();
- port->reset_widget(page);
- //port->show();
- //qApp->installEventFilter(port);
- }
- else
- {
- resize(screen_info.height(),screen_info.width());
- m_view->setSceneRect(0, 0, screen_info.width(), screen_info.height());
- //m_view->setSceneRect(0, 0, screen_info.height(), screen_info.width());
- if(rotation == 90)
- {
- m_proxy->setPos(screen_info.width(), 0);
- }else
- {
- m_proxy->setPos(0, screen_info.height());
- }
- m_proxy->setRotation(rotation);
- ui->stackedWidget->setCurrentIndex(1);
- port->block();
- land->reset_widget(page);
- }
- //ui->stackedWidget->setGeometry(centralWidget()->rect());
- m_view->setFrameStyle(QFrame::NoFrame);
- m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- m_view->showFullScreen();
- // qDebug() << "Window geometry:" << this->geometry();
- // qDebug() << "StackedWidget geometry:" << ui->stackedWidget->geometry();
- // qDebug() << "StackedWidget pos:" << ui->stackedWidget->pos();
- //m_view->showFullScreen();
- // m_view->update();
- }
- void MainWindow::test_modbus()
- {
- int ret = 0;
- uint16_t data[128] = {0};
- QString send = "";
- QString recived = "";
- uint8_t send_flag = 0;
- uint8_t revice_flag = 0;
- ret = modbus_read_registers(m_mb,_SWITCH_T_AC_TYPE_CHN_INFO,1,data);
- send = QString("发送: addr:%1,reg:%2,cnt:%3").arg(m_mb->slave).arg(_SWITCH_T_AC_TYPE_CHN_INFO).arg(1);
- send_flag = 1;
- if(ret > 0)
- {
- pdu_all_info.AllChs = data[0] & 0XFF;
- pdu_all_info.Type =(data[0] >> 8) & 0XFF;
- 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'));
- revice_flag = 1;
- }else
- {
- pdu_all_info.AllChs = 0;
- pdu_all_info.Type = 0;
- }
- emit Notice(send,recived,send_flag,revice_flag);
- }
- const modbus_t* MainWindow:: get_mb() const
- {
- return m_mb;
- }
- void MainWindow::read_pdu_data()
- {
- int ret = 0;
- uint16_t data[128] = {0};
- ret = modbus_read_registers(m_mb,_SWITCH_T_AC_TYPE_CHN_INFO,1,data);
- if(ret > 0)
- {
- pdu_all_info.AllChs = data[0] & 0XFF;
- pdu_all_info.Type =(data[0] >> 8) & 0XFF;
- }else
- {
- pdu_all_info.AllChs = 0;
- pdu_all_info.Type = 0;
- }
- QThread::msleep(30);
- ret = modbus_read_registers(m_mb,_SWITCH_T_AC_IN_L1_INFO,24,data);
- if(ret > 0)
- {
- pdu_all_info.AllVoltage_L1 = ((data[1] << 16) + data[0] )/1000.0f; // Low byte
- pdu_all_info.AllCurrent_L1 = ((data[3] << 16) + data[2] )/1000.0f; // Low byte
- pdu_all_info.AllPower_L1 = ((data[5] << 16) + data[4] )/1000.0f; // Low byte
- pdu_all_info.AllConsumption_L1 = ((data[7] << 16)+ data[6])/1000.0f; // Low byte
- pdu_all_info.AllVoltage_L2 = ((data[9] << 16) + data[8] )/1000.0f; // Low byte
- pdu_all_info.AllCurrent_L2 = ((data[11] << 16) + data[10] )/1000.0f; // Low byte
- pdu_all_info.AllPower_L2 = ((data[13] << 16) + data[12] )/1000.0f; // Low byte
- pdu_all_info.AllConsumption_L2 = ((data[15] << 16)+ data[14])/1000.0f; // Low byte
- pdu_all_info.AllVoltage_L3 = ((data[17] << 16) + data[16] )/1000.0f; // Low byte
- pdu_all_info.AllCurrent_L3 = ((data[19] << 16) + data[18] )/1000.0f; // Low byte
- pdu_all_info.AllPower_L3 = ((data[21] << 16) + data[20] )/1000.0f; // Low byte
- pdu_all_info.AllConsumption_L3 = ((data[23] << 16)+ data[22])/1000.0f; // Low byte
- //qDebug() << pdu_all_info.AllVoltage_L1 << " " << pdu_all_info.AllVoltage_L2 << " " << pdu_all_info.AllVoltage_L3;
- }else
- {
- pdu_all_info.AllVoltage_L1 =0; // Low byte
- pdu_all_info.AllCurrent_L1 = 0; // Low byte
- pdu_all_info.AllPower_L1 = 0; // Low byte
- pdu_all_info.AllConsumption_L1 = 0; // Low byte
- pdu_all_info.AllVoltage_L2 = 0; // Low byte
- pdu_all_info.AllCurrent_L2 = 0; // Low byte
- pdu_all_info.AllPower_L2 = 0; // Low byte
- pdu_all_info.AllConsumption_L2 = 0; // Low byte
- pdu_all_info.AllVoltage_L3 = 0; // Low byte
- pdu_all_info.AllCurrent_L3 = 0; // Low byte
- pdu_all_info.AllPower_L3 = 0; // Low byte
- pdu_all_info.AllConsumption_L3 = 0; // Low byte
- }
- QThread::msleep(30);
- for(int i = 0; i < MAX_CHANNELS;i++)
- {
- float VL1 = 0;
- float IL1 = 0;
- float PL1 = 0;
- float CL1 = 0;
- float VL2 = 0;
- float IL2 = 0;
- float PL2 = 0;
- float CL2 = 0;
- float VL3 = 0;
- float IL3 = 0;
- float PL3 = 0;
- float CL3 = 0;
- ret = modbus_read_registers(m_mb,_SWITCH_T_AC_CH_PHASE_INFO+(i*30),30,data);
- if(ret > 0)
- {
- VL1=((data[1] << 16) + data[0] )/1000.0f;
- IL1=((data[3] << 16) + data[2] )/1000.0f;
- PL1=((data[5] << 16) + data[4] )/1000.0f;
- CL1=((data[7] << 16) + data[6] )/1000.0f;
- VL2=((data[11] << 16) + data[10] )/1000.0f;
- IL2=((data[13] << 16) + data[12] )/1000.0f;
- PL2=((data[15] << 16) + data[14] )/1000.0f;
- CL2=((data[17] << 16) + data[16] )/1000.0f;
- VL3=((data[21] << 16) + data[20] )/1000.0f;
- IL3=((data[23] << 16) + data[22] )/1000.0f;
- PL3=((data[25] << 16) + data[24] )/1000.0f;
- CL3=((data[27] << 16) + data[26] )/1000.0f;
- }
- if(VL1>=VL2 && VL1>=VL3)
- {
- pdu_ch_info[i].CHVoltage=VL1;
- pdu_ch_info[i].CHCurrent=IL1;
- pdu_ch_info[i].CHPower=PL1;
- pdu_ch_info[i].CHConsumption=CL1;
- }
- if(VL2>=VL1 && VL2>=VL3)
- {
- pdu_ch_info[i].CHVoltage=VL2;
- pdu_ch_info[i].CHCurrent=IL2;
- pdu_ch_info[i].CHPower=PL2;
- pdu_ch_info[i].CHConsumption=CL2;
- }
- if(VL3>=VL1 && VL3>=VL2)
- {
- pdu_ch_info[i].CHVoltage=VL3;
- pdu_ch_info[i].CHCurrent=IL3;
- pdu_ch_info[i].CHPower=PL3;
- pdu_ch_info[i].CHConsumption=CL3;
- }
- QThread::msleep(20);
- }
- ret = modbus_read_registers(m_mb,_SWITCH_T_AC_CH_STATUS,MAX_CHANNELS,data);
- if(ret > 0)
- {
- for(int i = 0; i < MAX_CHANNELS;i++)
- pdu_ch_info[i].CHStatus= data[i] & 0xFFFF; // Low byte
- }else
- {
- for(int i = 0; i < MAX_CHANNELS;i++)
- pdu_ch_info[i].CHStatus= 0; // Low byte
- }
- QThread::msleep(20);
- ret = modbus_read_registers(m_mb,_SWITCH_T_AC_SENSOR_INFO,16,data);
- if(ret > 0)
- {
- pdu_all_info.Sensor_Temperature = ((data[1] << 16) + data[0] )/1000.0f;
- pdu_all_info.Sensor_Humidity = ((data[3] << 16) + data[2] )/1000.0f;
- pdu_all_info.Sensor_LeakFlag=((data[5] << 16) + data[4] );
- pdu_all_info.Sensor_INTemperature = ((data[9] << 16)+ data[8])/1000.0f;
- pdu_all_info.Sensor_IN_Pressure = ((data[11] << 16)+ data[10])/1000.0f;
- pdu_all_info.Sensor_OUTTemperature = ((data[13] << 16)+ data[12])/1000.0f;
- pdu_all_info.Sensor_OUT_Pressure = ((data[11] << 16)+ data[10])/1000.0f;
- }else
- {
- pdu_all_info.Sensor_Temperature = 0;
- pdu_all_info.Sensor_Humidity = 0;
- pdu_all_info.Sensor_LeakFlag=0;
- pdu_all_info.Sensor_INTemperature = 0;
- pdu_all_info.Sensor_IN_Pressure = 0;
- pdu_all_info.Sensor_OUTTemperature = 0;
- pdu_all_info.Sensor_OUT_Pressure = 0;
- }
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- delete [] m_channel;
- delete land;
- modbus_close(m_mb);
- modbus_free(m_mb);
- m_mb = NULL;
- delete m_key;
- }
- modbus_info_t MainWindow:: get_modbus_info()
- {
- return modbus;
- }
- void MainWindow:: opt_stop_or_start_signal(bool val)
- {
- if(val == true)
- {
- suspend_flag = 1;
- // m_timer->stop();
- }else
- {
- suspend_flag = 0;
- // m_timer->start();
- }
- }
|