#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include "QDebug" #include #include #include "modbus/modbus.h" #include "modbus/modbus-private.h" #include "QFile" #include 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(); } }