-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwidget.cpp
130 lines (126 loc) · 3.86 KB
/
widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTextCodec>
#include <QByteArray>
#include <QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget),hotplugMonitor(NULL),usbReceive(NULL),recvBuffer(NULL)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
if(recvBuffer)
{
free(recvBuffer);
}
}
//列出当前接入到系统的所有usb设备
void Widget::on_pushButton_clicked()
{
UsbComm usbComm;
usbComm.findUsbDevices();
}
//打开指定的usb设备,并发送通信数据
void Widget::on_pushButton_2_clicked()
{
UsbComm usbComm;
//这里对应的是我们用的USB接口的热敏打印机设备
QMultiMap<quint16, quint16> vpidMap;
vpidMap.insert(0x0483,0x5748);
if(usbComm.openUsbDevice(vpidMap))
{
if(usbComm.claimUsbInterface(usbComm.getDeviceHandleFromIndex(0),0))
{
QByteArray array0("hello printers\n");
qDebug()<<usbComm.bulkTransfer(usbComm.getDeviceHandleFromIndex(0),0x07,
(quint8 *)array0.data(),array0.size(),0);
QTextCodec *codec = QTextCodec::codecForName("GBK");
QByteArray array1 = codec->fromUnicode(QString::fromUtf8("我看看能不能打印中文!\n"));
qDebug()<<usbComm.bulkTransfer(usbComm.getDeviceHandleFromIndex(0),0x07,
(quint8 *)array1.data(),array1.size(),0);
QByteArray array2;//ESC控制命令(打印并走纸1行)
array2.append(0x1b);
array2.append(0x64);
array2.append(0x01);
qDebug()<<usbComm.bulkTransfer(usbComm.getDeviceHandleFromIndex(0),0x07,
(quint8 *)array2.data(),array2.size(),0);
}
}
}
//注册热插拔监测服务
void Widget::on_pushButton_3_clicked()
{
if(hotplugMonitor == NULL)
{
hotplugMonitor = new UsbMonitor(this);
connect(hotplugMonitor,SIGNAL(deviceHotplugSig(bool)),this,SLOT(deviceHotplugSlot(bool)));
}
hotplugMonitor->deregisterHotplugMonitorService();
hotplugMonitor->registerHotplugMonitorService();
}
//设备插拔信号响应槽
void Widget::deviceHotplugSlot(bool isAttached)
{
if(isAttached)//插入
{
qDebug()<<"usb device attached";
}
else//拔出
{
qDebug()<<"usb device detached";
}
}
//打开指定的usb设备,并接收通信数据
void Widget::on_pushButton_4_clicked()
{
static int flag = 0;
const int package_len = 8192;
if(usbReceive == NULL)
{
usbReceive = new UsbComm(this);
//这里对应的是我们用的USB接口的4k相机
QMultiMap<quint16, quint16> vpidMap;
vpidMap.insert(0x04b4,0x00f1);
if(usbReceive->openUsbDevice(vpidMap))
{
if(usbReceive->claimUsbInterface(usbReceive->getDeviceHandleFromIndex(0),0))
{
flag = 1;
recvBuffer = (unsigned char *)malloc(package_len);
}
}
if(flag != 1)
{
usbReceive->deleteLater();
usbReceive = NULL;
}
}
if(flag == 1)
{
memset(recvBuffer,0,package_len);
int len = usbReceive->bulkTransfer(usbReceive->getDeviceHandleFromIndex(0),0x81,recvBuffer,package_len,1);
if(len < 0)
{
qDebug()<<"bulkTransfer error"<<len;
}
else
{
QByteArray array;
array.append((char *)recvBuffer,len);
qDebug()<<QString("array[%1]:").arg(len)<<array.toHex();
}
}
}
//重置接收设备(重置会清空当前缓冲区的数据)
void Widget::on_pushButton_5_clicked()
{
if(usbReceive != NULL)
{
bool resetFlag = usbReceive->resetUsbDevice(usbReceive->getDeviceHandleFromIndex(0));
qDebug()<<"resetFlag============"<<resetFlag;
}
}