前面我们已经实现了控制传输并进行了测试,但是每次8字节的SETUP内容都需要手动填入,一般可能记不住需要去翻规格书,所以我们可以优化下UI,实现对于标准请求可以设定提示,根据提示进行设置。我们通过子窗口来实现,需要将子窗口中设置的SETUP返回到主窗口中。以上子窗口和主窗口的信息交互我们通过信号槽来实现。
文件->New File... 按照如下添加Widget类Form
Form.ui设计如下
ui文件内容如下
<ui version="4.0">
<class>Formclass>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0x>
<y>0y>
<width>673width>
<height>377height>
rect>
property>
<property name="windowTitle">
<string>Formstring>
property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="10,0,0,0">
<item>
<layout class="QGridLayout" name="gridLayout" rowstretch="1,0,0" columnstretch="3,0,0,0,0,0,0">
<item row="1" column="4">
<widget class="QLineEdit" name="lineEdit_2"/>
item>
<item row="0" column="6">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>wLengthstring>
property>
widget>
item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Typestring>
property>
widget>
item>
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Directionstring>
property>
widget>
item>
<item row="0" column="2">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Recipientstring>
property>
widget>
item>
<item row="1" column="0">
<widget class="QComboBox" name="comboBox"/>
item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox_3"/>
item>
<item row="1" column="2">
<widget class="QComboBox" name="comboBox_4"/>
item>
layout>
item>
<item row="0" column="1">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>bRequeststring>
property>
widget>
item>
<item row="0" column="2">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>wValue_Lstring>
property>
widget>
item>
<item row="1" column="2">
<widget class="QLineEdit" name="lineEdit"/>
item>
<item row="0" column="4">
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>wIndex_Lstring>
property>
widget>
item>
<item row="0" column="3">
<widget class="QLabel" name="label_9">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>wValue_Hstring>
property>
widget>
item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>bmRequestTypestring>
property>
widget>
item>
<item row="1" column="6">
<widget class="QLineEdit" name="lineEdit_3"/>
item>
<item row="0" column="5">
<widget class="QLabel" name="label_10">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="text">
<string>wIndex_Hstring>
property>
widget>
item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox_2"/>
item>
<item row="1" column="5">
<widget class="QLineEdit" name="lineEdit_5"/>
item>
<item row="1" column="3">
<widget class="QLineEdit" name="lineEdit_4"/>
item>
layout>
item>
<item>
<widget class="QLabel" name="label_11">
<property name="text">
<string>提示string>
property>
widget>
item>
<item>
<widget class="QTextEdit" name="textEdit"/>
item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>确认string>
property>
widget>
item>
layout>
widget>
<resources/>
<connections/>
ui>
Form.h中
类中添加信号函数
signals:
void sendData(QString data);
Form.cpp中添加确认按键处理,发送信号数据
void Form::on_pushButton_clicked()
{
QString str = "800600020000FE00";
QByteArray setup_data;
setup_data.resize(8);
uint8_t Direction = ui->comboBox->currentIndex();
uint8_t Type = ui->comboBox_3->currentIndex();
uint8_t Recipient = ui->comboBox_4->currentIndex();
uint8_t bRequest = ui->comboBox_2->currentIndex();
uint8_t wValue_L = ui->lineEdit->text().toUInt();
uint8_t wValue_H = ui->lineEdit_4->text().toUInt();
uint8_t wIndex_L = ui->lineEdit_2->text().toUInt();
uint8_t wIndex_H = ui->lineEdit_5->text().toUInt();
uint16_t wLength = ui->lineEdit_3->text().toUInt();
setup_data[0] = (uint8_t)(Direction<<7) | (uint8_t)(Type<<4) | (uint8_t)(Recipient<<0);
setup_data[1] = bRequest;
setup_data[2] = wValue_L;
setup_data[3] = wValue_H;
setup_data[4] = wIndex_L;
setup_data[5] = wIndex_H;
setup_data[6] = wLength & 0xFF;
setup_data[7] = (wLength>>8) & 0xFF;
str = ByteArrayToHexString(setup_data);
emit sendData(str);
this->close();
}
主窗口类中添加成员
QWidget *setupform;
添加信号槽
private slots:
void reshow_setup(QString);
主窗口构造函数处,new Form实例,链接信号和槽
setupform = new Form;
connect(setupform,SIGNAL(sendData(QString)),this,SLOT(reshow_setup(QString)));
主窗口实现槽处理
void MainWindow::reshow_setup(QString str)
{
this->show();
ui->textEdit_5->setText(str);
}
form.cpp中
构造函数初始化如下,设置默认参数
Form::Form(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Form)
{
ui->setupUi(this);
ui->comboBox->clear();
ui->comboBox->addItems(QStringList("0=Host-to-device"));
ui->comboBox->addItems(QStringList("1=Device-to-host"));
ui->comboBox->setCurrentIndex(1);
ui->comboBox_3->clear();
ui->comboBox_3->addItems(QStringList("0=Standard"));
ui->comboBox_3->addItems(QStringList("1=Class"));
ui->comboBox_3->addItems(QStringList("2=Vendor"));
ui->comboBox_3->addItems(QStringList("3=Reserved"));
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->clear();
ui->comboBox_4->addItems(QStringList("0=Device"));
ui->comboBox_4->addItems(QStringList("1=Interface"));
ui->comboBox_4->addItems(QStringList("2=Endpoint"));
ui->comboBox_4->addItems(QStringList("3=Other"));
ui->comboBox_4->setCurrentIndex(0);
ui->comboBox_2->clear();
ui->comboBox_2->addItems(QStringList("0=GET_STATUS"));
ui->comboBox_2->addItems(QStringList("1=CLEAR_FEATURE"));
ui->comboBox_2->addItems(QStringList("2=Reserved"));
ui->comboBox_2->addItems(QStringList("3=SET_FEATURE"));
ui->comboBox_2->addItems(QStringList("4=Reserved"));
ui->comboBox_2->addItems(QStringList("5=SET_ADDRESS"));
ui->comboBox_2->addItems(QStringList("6=GET_DESCRIPTOR"));
ui->comboBox_2->addItems(QStringList("7=SET_DESCRIPTOR"));
ui->comboBox_2->addItems(QStringList("8=GET_CONFIGURATION"));
ui->comboBox_2->addItems(QStringList("9=SET_CONFIGURATION"));
ui->comboBox_2->addItems(QStringList("10=GET_INTERFACE"));
ui->comboBox_2->addItems(QStringList("11=SET_INTERFACE"));
ui->comboBox_2->addItems(QStringList("12=SYNCH_FRAME"));
ui->comboBox_2->setCurrentIndex(6);
ui->textEdit->setText(QString("wValue_L:填描述符索引\n")+
QString("wValue_H:填描述符类型\n")+
QString("DEVICE=1,CONFIGURATION=2,STRING=3,INTERFACE=4,ENDPOINT=5,DEVICE_QUALIFIER=6,OTHER_SPEED_CONFIGURATION=7,INTERFACE_POWER=8\n")+
QString("wIndex:填0/语言ID(04 09)\n")+
QString("wLength:填描述符长度"));
ui->lineEdit_3->setText("255");
ui->lineEdit->setText("0");
ui->lineEdit_4->setText("0");
ui->lineEdit_2->setText("0");
ui->lineEdit_5->setText("0");
ui->lineEdit_3->setText("255");
}
以bRequest为准进行解析
void Form::on_comboBox_2_activated(int index)
{
//uint8_t bRequest = ui->comboBox_2->currentIndex();
switch(index)
{
case 0:
/* GET_STATUS */
ui->comboBox->setCurrentIndex(1);
ui->textEdit->setText("wIndex_L:填0/接口号/端点号");
ui->lineEdit_3->setText("2");
break;
case 1:
/* CLEAR_FEATURE */
ui->comboBox->setCurrentIndex(0);
ui->comboBox_3->setCurrentIndex(0);
ui->textEdit->setText(QString("wIndex_L:填0/接口号/端点号\n")+
QString("wValue_L:\n")+
QString("ENDPOINT_HALT=0(Recipient:Endpoint),\n")+
QString("DEVICE_REMOTE_WAKEUP=1(Recipient:Device),\n")+
QString("TEST_MODE=2(Recipient:Device)"));
ui->lineEdit_3->setText("0");
break;
case 3:
/* SET_FEATURE */
ui->comboBox->setCurrentIndex(0);
ui->comboBox_3->setCurrentIndex(0);
ui->textEdit->setText(QString("wIndex_L:填0/接口号/端点号\n")+
QString("wValue_L:")+
QString("ENDPOINT_HALT=0(Recipient:Endpoint),\n")+
QString("DEVICE_REMOTE_WAKEUP=1(Recipient:Device),\n")+
QString("TEST_MODE=2(Recipient:Device)"));
ui->lineEdit_3->setText("0");
break;
case 5:
/* SET_ADDRESS */
ui->comboBox->setCurrentIndex(0);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(0);
ui->textEdit->setText("wValue_L:填地址");
ui->lineEdit_3->setText("0");
break;
case 6:
/* GET_DESCRIPTOR */
ui->comboBox->setCurrentIndex(1);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(0);
ui->textEdit->setText(QString("wValue_L:填描述符索引\n")+
QString("wValue_H:填描述符类型\n")+
QString("DEVICE=1,CONFIGURATION=2,STRING=3,INTERFACE=4,ENDPOINT=5,DEVICE_QUALIFIER=6,OTHER_SPEED_CONFIGURATION=7,INTERFACE_POWER=8\n")+
QString("wIndex:填0/语言ID(04 09)\n")+
QString("wLength:填描述符长度"));
ui->lineEdit_3->setText("255");
break;
case 7:
/* SET_DESCRIPTOR */
ui->comboBox->setCurrentIndex(0);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(0);
ui->textEdit->setText(QString("wValue_L:填描述符索引\n")+
QString("wValue_H:填描述符类型\n")+
QString("DEVICE=1,CONFIGURATION=2,STRING=3,INTERFACE=4,ENDPOINT=5,DEVICE_QUALIFIER=6,OTHER_SPEED_CONFIGURATION=7,INTERFACE_POWER=8\n")+
QString("wIndex:填0/语言ID(04 09)\n")+
QString("wLength:填描述符长度"));
break;
case 8:
/* GET_CONFIGURATION*/
ui->comboBox->setCurrentIndex(1);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(0);
ui->textEdit->setText(QString("wValue:填0\n")+
QString("wIndex:填0\n")+
QString("wLength:填1"));
ui->lineEdit_3->setText("1");
break;
case 9:
/* SET_CONFIGURATION*/
ui->comboBox->setCurrentIndex(0);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(0);
ui->textEdit->setText(QString("wValue_L:填配置值\n")+
QString("wIndex:填0\n")+
QString("wLength:填0"));
ui->lineEdit_3->setText("0");
break;
case 10:
/* GET_INTERFACE */
ui->comboBox->setCurrentIndex(1);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(1);
ui->textEdit->setText(QString("wValue:填0\n")+
QString("wIndex_L:填接口号\n")+
QString("wLength:填1"));
ui->lineEdit_3->setText("1");
break;
case 11:
/* SET_INTERFACE */
ui->comboBox->setCurrentIndex(0);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(1);
ui->textEdit->setText(QString("wValue_L:填Alternate Setting\n")+
QString("wIndex_L:填接口号\n")+
QString("wLength:填0"));
ui->lineEdit_3->setText("0");
break;
case 12:
/* GET_INTERFACE */
ui->comboBox->setCurrentIndex(1);
ui->comboBox_3->setCurrentIndex(0);
ui->comboBox_4->setCurrentIndex(2);
ui->textEdit->setText(QString("wValue:填0\n")+
QString("wIndex_L:填端点号\n")+
QString("wLength:填2\n")+
QString("Data:填2字节Frame Number"));
ui->lineEdit_3->setText("2");
break;
}
}
按setup设置,进入子窗口
根据提示获取设备描述符,再点击确认
点击开始获取设备描述符
测试获取配置描述符
测试获取字符串描述符
以上实现了标准请求的解析和提示,可以方便用户使用进行测试控制传输的标准请求,后面还可以继续完善除了标准请求的其他请求。