#include "mainwidget.h" #include "plotwidget.h" #include "ctlwidget.h" #include "anawidget.h" #include #include #include #include #include #include #include #include #include #include #include #include MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { createUI(); } MainWidget::~MainWidget() { } void MainWidget::createUI() { m_customPlotWidget = new PlotWidget(this); m_customRealWidget = new RealWidget(this); m_stackLayout = new QStackedLayout(); m_stackLayout->addWidget(m_customPlotWidget); m_stackLayout->addWidget(m_customRealWidget); CtlWidget *ctlWidget = new CtlWidget(this); ///ctlWidget->setStyleSheet("background-color:rgba(50, 50, 50, 50)"); ///anaWidget->setGeometry(200,200,800,800); ctlWidget->resize(400, 400); /*QTreeView *m_treeView = new QTreeView(this); m_treeView->resize(400, 400); QFileSystemModel *m_model=new QFileSystemModel(this); // 设置根目录 m_model->setRootPath(QDir::currentPath()); // 设置过滤器,只过滤出.ana QStringList filter; filter << ".ana"; // 使用过滤器 m_model->setNameFilters(filter); m_model->setNameFilterDisables(false); // 设置数据模型 m_treeView->setModel(m_model);*/ AnaWidget *anaWidget = new AnaWidget(this); ///anaWidget->setStyleSheet("background-color:rgba(0, 0, 100, 100)"); ///anaWidget->setGeometry(200,200,800,800); anaWidget->resize(400, 400); ///anaWidget->hide(); QGridLayout *baseLayout = new QGridLayout(); baseLayout->addWidget(ctlWidget, 0, 0, 1, 5); baseLayout->addWidget(anaWidget, 1, 0, 10, 1); baseLayout->addLayout(m_stackLayout, 1, 1, 10, 1); ///baseLayout->addWidget(m_customPlotWidget, 1, 1, 10, 1 ); //列比 第0列与第1列1:7 baseLayout -> setColumnStretch(0, 1); baseLayout -> setColumnStretch(1, 6);; baseLayout -> setRowStretch(0, 1); baseLayout -> setRowStretch(1, 8); setLayout(baseLayout); connect(ctlWidget->m_pushbutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(replot_xlsx_data())); connect(ctlWidget->m_pushbutton, SIGNAL(clicked()), ctlWidget, SLOT(disable_load_button())); connect(m_customPlotWidget, SIGNAL(calcu_button_enable()), anaWidget, SLOT(enable_calcu_button())); connect(anaWidget->m_realbutton, SIGNAL(clicked()), this, SLOT(showStackCPRealWidget())); connect(anaWidget->m_anabutton, SIGNAL(clicked()), this, SLOT(showStackCPAnaWidget())); connect(anaWidget->m_startbutton, SIGNAL(clicked()), m_customRealWidget, SLOT(start_plot_real_line())); connect(anaWidget->m_stopbutton, SIGNAL(clicked()), m_customRealWidget, SLOT(stop_plot_real_line())); ///connect(ctlWidget->m_openbutton, SIGNAL(clicked()), SLOT(refresh_)); connect(anaWidget->m_calcubutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(get_coordinate_calcu())); connect(anaWidget->m_choosebutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(show_line_cursor())); connect(anaWidget->m_cancelbutton, SIGNAL(clicked()), m_customPlotWidget, SLOT(hide_line_cursor())); connect(m_customPlotWidget, SIGNAL(show_area()), anaWidget, SLOT(show_area_edit())); connect(m_customPlotWidget, SIGNAL(show_tang()), anaWidget, SLOT(show_tang_edit())); } void MainWidget::showStackCPRealWidget() { m_stackLayout->setCurrentIndex(1); // uint16_t crcres = 0x1606; // ///m_stackLayout->setCurrentIndex(0); // QByteArray sendArray; // sendArray.resize(9); // sendArray[0] = 0x5A; //帧头 // sendArray[1] = 0xA5; // sendArray[2] = 0x06; //数据总长度 // sendArray[3] = 0x83; // sendArray[4] = 0x00; // sendArray[5] = 0x28; // sendArray[6] = 0x01; // sendArray[7] = crcres>>8; // sendArray[8] = crcres; // unsigned char *objbuf = NULL; // int lenbuf = sendArray.size(); // objbuf = (unsigned char*)std::malloc(lenbuf); // std::memcpy(objbuf, (unsigned char*)sendArray.data(), lenbuf); // for(int i=0; i(crcdataBuf), len); // ///std::memcpy((unsigned char*)array.data(), crcdataBuf, len);//复制数据 // uint16_t crcrec = ModbusCRC16(array); */ //转化数据错误导致计算错误 // QByteArray crcArray; // crcArray.resize(7); // crcArray[0] = 0x5A; //帧头 // crcArray[1] = 0xA5; // crcArray[2] = 0x06; //数据总长度 // crcArray[3] = 0x83; // crcArray[4] = 0x00; // crcArray[5] = 0x28; // crcArray[6] = 0x01; // uint16_t crcret = ModbusCRC16(crcArray); // for(int i=0; isetCurrentIndex(0); } uint16_t MainWidget::ModbusCRC16(QByteArray senddata) { int len=senddata.size(); uint16_t wcrc=0XFFFF;//预置16位crc寄存器,初值全部为1 uint8_t temp;//定义中间变量 int i=0,j=0;//定义计数 for(i=0;i>=1;//先将数据右移一位 wcrc^=0XA001;//与上面的多项式进行异或 } else//如果不是1,则直接移出 wcrc>>=1;//直接移出 } } temp=wcrc;//crc的值 return wcrc; } uint16_t MainWidget::CRC16_MODBUS(unsigned char *data, unsigned int datalen) { unsigned short wCRCin = 0xFFFF; unsigned short wCPoly = 0x8005; InvertUint16(&wCPoly,&wCPoly); while (datalen--) { wCRCin ^= *(data++); for(int i = 0;i < 8;i++) { if(wCRCin & 0x01) wCRCin = (wCRCin >> 1) ^ wCPoly; else wCRCin = wCRCin >> 1; } } return (wCRCin); } void MainWidget::InvertUint16(unsigned short *dBuf,unsigned short *srcBuf) { int i; unsigned short tmp[4]={0}; for(i=0;i< 16;i++) { if(srcBuf[0]& (1 << i)) tmp[0]|=1<<(15 - i); } dBuf[0] = tmp[0]; }