#include "mainwindow.h" #include "ui_mainwindow.h" #define LED_ON setStyleSheet("background-color:lightGreen;") #define LED_OFF setStyleSheet("background-color:lightBlack;") MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { SingleBoardTestFlag = 0x00; dnData_MsgLen = 0; //下发数据长度 dnFlag_PowerOff = 0; //主机关机中 dnFlag_DisableWalk = 0; //升降、行走、支脚禁用信号 dnFlag_LargeColumnLimit = 0; //大立柱限位有效标志 dnFlag_LargeColumnProgram = 0; //大立柱TWINCAT程序控制标志 dnFlag_LargeColumnUp = 0; //大立柱向上运动标志 dnFlag_LargeColumnDown = 0; //大立柱向下运动标志 dnFlag_LaserLight = 0; //十字激光点亮标志 dnFlag_ParkBrake = 0; //支脚电机升降控制 dnData_LargeColumnUpLimitVal = 0; //大立柱上限值 dnData_LargeColumnDownLimitVal = 0;//大立柱下限值 dnData_LargeColumnProgramVal = 0; //大立柱TWINCAT程序控制目标值 dnState_SystemReset = 0; //复位按钮灯状态(无效值常零) dnFlag_LargeColumnCanMove = 0; //横梁立柱动作使能标志 rvFlag_PowerKey = 0; //开关机按键信号(按下为1、抬起为0) rvData_BMS_SOC = 0; //电池电量 rvState_PowerSta = 0; //1-市电供电且电池满电 2-电池供电 3电池充电状态 rvFlag_LeftRunMotorEn = 0; //左行走驱动器状态 rvFlag_RightRunMotorEn = 0; //右行走驱动器状态 rvFlag_LeftStandMotorEn = 0; //左支脚驱动器状态 rvFlag_RightStandMotorEn = 0; //右支脚驱动器状态 rvFlag_ColumMotorEn = 0; //大立柱驱动器状态 rvFlag_ColumnMoveFlag = 0; //大立柱动作标志 rvData_ColumPosition = 0; //大立柱当前位置 rvData_beam_FrBkValue = 0; //大横梁三轴电位器前后位置 rvData_beam_LeRiValue = 0; //大横梁三轴电位器左右位置 rvData_beam_RotaValue = 0; //大横梁三轴电位器旋转位置 rvFlag_RunMoveFlag = 0; //行走电机工作标识 rvData_ErrCode = 0; //错误代码 rvFlag_UseLineFlag = 0; //线控手柄启用标识 rvData_HandKeyValue = 0; //线控手柄按键值 rvFlag_LeftStandSwitch = 0; //左侧支脚电机光电开关信号 rvFlag_RightStandSwitch = 0; //右侧支脚电机光电开关信号 ui -> setupUi(this); this -> setWindowTitle("2.0项目台车调试V00-产品电控部"); my_socket = new QTcpSocket(); connectstat = false; connect(my_socket,&QTcpSocket::readyRead,this,&MainWindow::readyReadSlots); connect( &m_tData, SIGNAL(timeout()), this, SLOT(SLOT_TimerOver()) ); m_tData.start(10); } MainWindow::~MainWindow() { delete ui; } void MainWindow::readyReadSlots() { int RecvLen; uint8_t TCPRecvBuff[28] = {0}; QByteArray RecvBuff; RecvLen = my_socket->bytesAvailable(); RecvBuff = my_socket->read(RecvLen>65535?65535:RecvLen); memcpy(TCPRecvBuff,RecvBuff,RecvLen); if((TCPRecvBuff[0]==0xEB)&&(TCPRecvBuff[1]==0x90)) { rvFlag_PowerKey = TCPRecvBuff[6]; //开关机按键信号(按下为1、抬起为0) rvState_PowerSta = TCPRecvBuff[7]; //1-市电供电且电池满电 2-电池供电 3电池充电状态 rvData_BMS_SOC = TCPRecvBuff[8]; //电量 rvFlag_LeftRunMotorEn = TCPRecvBuff[9]; //左行走驱动器状态 rvFlag_RightRunMotorEn = TCPRecvBuff[10]; //右行走驱动器状态 rvFlag_LeftStandMotorEn = TCPRecvBuff[12]; //左支脚驱动器状态 rvFlag_RightStandMotorEn = TCPRecvBuff[13]; //右支脚驱动器状态 rvFlag_ColumMotorEn = TCPRecvBuff[11]; //大立柱驱动器状态 rvFlag_ColumnMoveFlag = TCPRecvBuff[14]; memcpy(&rvData_ColumPosition,TCPRecvBuff+15,2); //大立柱当前位置 rvData_beam_FrBkValue = TCPRecvBuff[17]; //大横梁三轴电位器前后位置 rvData_beam_LeRiValue = TCPRecvBuff[18]; //大横梁三轴电位器左右位置 rvData_beam_RotaValue = TCPRecvBuff[19]; //大横梁三轴电位器旋转位置 rvFlag_RunMoveFlag = TCPRecvBuff[21]; //行走电机工作标识 rvData_ErrCode = TCPRecvBuff[22]; //错误代码 rvFlag_UseLineFlag = TCPRecvBuff[23]; //线控手柄启用标识 memcpy(&rvData_HandKeyValue,TCPRecvBuff+24,2); //线控手柄按键值 rvFlag_LeftStandSwitch = TCPRecvBuff[26]; //左侧支脚电机光电开关信号 rvFlag_RightStandSwitch = TCPRecvBuff[27]; //右侧支脚电机光电开关信号 } } void MainWindow::ShowRecvState() { QString strShow; ui->lcdnumber->setSegmentStyle(QLCDNumber::Flat); ui->lcdnumber->display(rvData_BMS_SOC); switch(rvState_PowerSta) { case 0x01: ui->label_PowerSta->setText("供电:市电"); break; case 0x02: ui->label_PowerSta->setText("供电:电池"); break; case 0x03: ui->label_PowerSta->setText("供电:充电中"); break; } rvFlag_PowerKey ? ui->LED1->LED_ON : ui->LED1->LED_OFF; rvFlag_LeftRunMotorEn ? ui->LED4->LED_ON : ui->LED4->LED_OFF; rvFlag_RightRunMotorEn ? ui->LED5->LED_ON : ui->LED5->LED_OFF; rvFlag_LeftStandSwitch ? ui->LED8_4->LED_ON: ui->LED8_4->LED_OFF; rvFlag_RightStandSwitch ? ui->LED8_3->LED_ON: ui->LED8_3->LED_OFF; rvFlag_LeftStandMotorEn ? ui->LED8->LED_ON : ui->LED8->LED_OFF; rvFlag_RightStandMotorEn? ui->LED8_2->LED_ON: ui->LED8_2->LED_OFF; rvFlag_ColumMotorEn ? ui->LED7->LED_ON : ui->LED7->LED_OFF; rvFlag_ColumnMoveFlag ? ui->LED6->LED_ON : ui->LED6->LED_OFF; rvFlag_RunMoveFlag ? ui->LED2->LED_ON : ui->LED2->LED_OFF; rvFlag_UseLineFlag ? ui->LED3->LED_ON : ui->LED3->LED_OFF; strShow.setNum(rvData_beam_FrBkValue); ui->label_20->setText(strShow); strShow.setNum(rvData_beam_LeRiValue); ui->label_23->setText(strShow); strShow.setNum(rvData_beam_RotaValue); ui->label_24->setText(strShow); strShow.setNum(rvData_ColumPosition); ui->label_26->setText(strShow); ui->lineEdit_ColumCurPosi->setText(strShow); } void MainWindow::on_pushButton_clicked() { QString ip = ui -> lineEdit -> text(); qint16 port = ui -> lineEdit_2-> text().toInt(); QHostAddress address = QHostAddress(ip); if(!connectstat)//如果没有连接 { my_socket -> connectToHost(address,port); if(!my_socket -> waitForConnected(5000)) { qDebug()<<my_socket -> errorString(); QMessageBox::critical(this, "连接错误", "以太网连接失败!"); return ; } else { ui->pushButton->setText("断开连接"); connectstat=true; } } else { ui->pushButton->setText("建立连接"); connectstat=false; } } uint16_t GetCRC16(unsigned char * pszBuf, uint16_t unLength) { uint16_t i, j, CurVal; uint16_t CrcReg = 0xFFFF; for (i = 0; i < unLength; i++) { CurVal = pszBuf[i] << 8; for (j = 0; j < 8; j++) { if ((short)(CrcReg ^ CurVal) < 0) CrcReg = (CrcReg << 1) ^ 0x1021; else CrcReg <<= 1; CurVal <<= 1; } } return CrcReg; } void MainWindow::TCP_SendData(QByteArray Data,uint8_t Len) { //Data = res.toLocal8Bit(); my_socket->write(QByteArray::fromHex(Data),Len); // my_socket->write(Data.toHex(),Len); } void MainWindow::SLOT_TimerOver() { QString StrSend; uint16_t CRCData = 0; uint8_t SendBuff[24] = {0}; SendBuff[ 0 ] = 0xEB; SendBuff[ 1 ] = 0x90; SendBuff[ 2 ] = 0xFF; SendBuff[ 3 ] = 0xFF; SendBuff[ 4 ] = 0xFF; SendBuff[ 5 ] = 0x10; SendBuff[ 6 ] = dnFlag_PowerOff; SendBuff[ 7 ] = dnFlag_DisableWalk; //动作禁用 SendBuff[ 8 ] = dnFlag_LargeColumnLimit; //立柱限位有效 SendBuff[ 9 ] = dnFlag_LargeColumnProgram; //立柱程控标识 SendBuff[ 10] = dnFlag_LargeColumnUp; //立柱向上 SendBuff[ 11] = dnFlag_LargeColumnDown; //立柱向下 SendBuff[ 12] = dnFlag_LaserLight; //激光点亮 SendBuff[ 13] = dnFlag_ParkBrake; //支脚升降 memcpy( SendBuff + 14, &dnData_LargeColumnUpLimitVal, 2 ); //大立柱上限位 memcpy( SendBuff + 16, &dnData_LargeColumnDownLimitVal,2 ); //大立柱下限位 memcpy( SendBuff + 18, &dnData_LargeColumnProgramVal, 2 ); //大立柱程控目标值 SendBuff[ 20] = 0x00; SendBuff[ 21] = dnFlag_LargeColumnCanMove; //梁柱使能标识 CRCData = GetCRC16(SendBuff,22); SendBuff[ 22] = CRCData & 0xFF; SendBuff[ 23] = CRCData >> 8; for(uint8_t i=0;i<sizeof(SendBuff);i++) { StrSend += QString(SendBuff[i]); } if(connectstat == true) { my_socket->write(StrSend.toLatin1(),24); } ShowRecvState(); } void MainWindow::on_pushButton_Laser_pressed() { dnFlag_LaserLight = 0x01; } void MainWindow::on_pushButton_Laser_released() { dnFlag_LaserLight = 0x00; } void MainWindow::on_checkBox_DisMove_clicked(bool checked) { dnFlag_DisableWalk = checked; } /*****************************驻车********************************************************/ void MainWindow::on_pushButton_ParkKey_clicked() { dnFlag_ParkBrake = 0x01; } void MainWindow::on_pushButton_DisPark_clicked() //驻车取消 { dnFlag_ParkBrake = 0x00; } /***************************************************************************************/ //关机 void MainWindow::on_pushButton_PowerOff_pressed() { dnFlag_PowerOff = 0x01; } void MainWindow::on_pushButton_PowerOff_released() { dnFlag_PowerOff = 0x00; } //----------------------------立柱---------------------------------------------------------- void MainWindow::on_checkBox_3_clicked(bool checked) { dnFlag_LargeColumnCanMove = checked; } void MainWindow::on_checkBox_clicked(bool checked) //选择程序控制 { dnFlag_LargeColumnProgram = checked; qDebug("LargeColumnUp:%d",dnFlag_LargeColumnProgram); } void MainWindow::on_checkBox_2_clicked(bool checked)//立柱限位有效标识 { dnFlag_LargeColumnLimit = checked; } void MainWindow::on_pushButton_CulumUp_pressed() { dnFlag_LargeColumnUp = 0x01; qDebug("LargeColumnUp:%d",dnFlag_LargeColumnUp); } void MainWindow::on_pushButton_CulumUp_released() { dnFlag_LargeColumnUp = 0x00; qDebug("LargeColumnUp:%d",dnFlag_LargeColumnUp); } void MainWindow::on_pushButton_CulumUp_2_pressed() { dnFlag_LargeColumnDown = 0x01; } void MainWindow::on_pushButton_CulumUp_2_released() { dnFlag_LargeColumnDown = 0x00; }
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> #include <QHostAddress> #include <QMessageBox> #include <QTimer> #include <QKeyEvent> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void SLOT_TimerOver(); private slots: void readyReadSlots(); void on_pushButton_clicked(); void on_pushButton_ParkKey_clicked(); void on_pushButton_DisPark_clicked(); void on_checkBox_clicked(bool checked); void on_checkBox_2_clicked(bool checked); void on_pushButton_CulumUp_pressed(); void on_pushButton_CulumUp_released(); void on_pushButton_CulumUp_2_pressed(); void on_pushButton_CulumUp_2_released(); void on_pushButton_PowerOff_pressed(); void on_pushButton_PowerOff_released(); void on_checkBox_3_clicked(bool checked); void on_checkBox_DisMove_clicked(bool checked); void on_pushButton_Laser_pressed(); void on_pushButton_Laser_released(); private: Ui::MainWindow *ui; QTcpSocket *my_socket; bool connectstat; QTimer m_tData; uint8_t SingleBoardTestFlag; //单板测试标识 uint8_t dnData_MsgLen; //下发数据长度 uint8_t dnFlag_PowerOff; //主机关机中 uint8_t dnFlag_DisableWalk; //升降、行走、支脚禁用信号 uint8_t dnFlag_LargeColumnLimit; //大立柱限位有效标志 uint8_t dnFlag_LargeColumnProgram; //大立柱TWINCAT程序控制标志 uint8_t dnFlag_LargeColumnUp; //大立柱向上运动标志 uint8_t dnFlag_LargeColumnDown ; //大立柱向下运动标志 uint8_t dnFlag_LaserLight; //十字激光点亮标志 uint8_t dnFlag_ParkBrake; //支脚电机升降控制 uint16_t dnData_LargeColumnUpLimitVal; //大立柱上限值 uint16_t dnData_LargeColumnDownLimitVal;//大立柱下限值 uint16_t dnData_LargeColumnProgramVal; //大立柱TWINCAT程序控制目标值 uint8_t dnState_SystemReset; //复位按钮灯状态(无效值常零) uint8_t dnFlag_LargeColumnCanMove; //横梁立柱动作使能标志 uint8_t rvFlag_PowerKey; //开关机按键信号(按下为1、抬起为0) uint8_t rvData_BMS_SOC; //电池电量 uint8_t rvState_PowerSta; //1-市电供电且电池满电 2-电池供电 3电池充电状态 uint8_t rvFlag_LeftRunMotorEn; //左行走驱动器状态 uint8_t rvFlag_RightRunMotorEn; //右行走驱动器状态 uint8_t rvFlag_LeftStandMotorEn; //左支脚驱动器状态 uint8_t rvFlag_RightStandMotorEn; //右支脚驱动器状态 uint8_t rvFlag_ColumMotorEn; //大立柱驱动器状态 uint8_t rvFlag_ColumnMoveFlag; //大立柱动作标志 uint8_t rvData_ColumPosition; //大立柱当前位置 uint8_t rvData_beam_FrBkValue; //大横梁三轴电位器前后位置 uint8_t rvData_beam_LeRiValue; //大横梁三轴电位器左右位置 uint8_t rvData_beam_RotaValue; //大横梁三轴电位器旋转位置 uint8_t rvFlag_RunMoveFlag; //行走电机工作标识 uint8_t rvData_ErrCode; //错误代码 uint8_t rvFlag_UseLineFlag; //线控手柄启用标识 uint16_t rvData_HandKeyValue; //线控手柄按键值 uint8_t rvFlag_LeftStandSwitch; //左侧支脚电机光电开关信号 uint8_t rvFlag_RightStandSwitch; //右侧支脚电机光电开关信号 void TCP_SendData(QByteArray Data,uint8_t Len); void ShowRecvState(); }; #endif // MAINWINDOW_H
标签:code,void,uint8,dnFlag,rvFlag,ui,MainWindow From: https://www.cnblogs.com/einstein-2014731/p/17332900.html