使用libusb开发库可以很方便的与usb通信。
在使用这个库之前需要做几件事情
1.libusb的安装
在ubuntu中使用sudo apt-get isntall libusb* 可以快速的安装,如果想选择性的安装则使用sudo apt-cache search libusb查看要安装的软件包,然后sudo apt-get install ...进行安装
2.确定供应商id 和 产品id,通过这两个id可以确定我们的usb设备
使用lsusb命令查看,然后使用libusb -v查看具体信息
3 确定传输方式、输入端点、输出端点
命令同2
我的usb是bulk传输方式、输入端点0x86,输出端点0x02
4.在项目中添加添加库 -lusb
不添加的话会出现找不到函数的问题,如果提示找不到usb.h文件很可能是你没安装好libusb库
5.软件完成之后要使用sudo运行程序,不然usb_bulk_write之类的函数会调用失败。
6.附上官网地址和帮助文档
官网:http://www.libusb.org/
帮助文档:http://libusb.sourceforge.net/doc/
下面看一下bulk的使用例子:
usbhelper.h
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <usb.h> 4 #include <QtCore/QObject> 5 #include <QtCore/QString> 6 #include <QtCore/QStringList> 7 class UsbHelper : public QObject 8 { 9 Q_OBJECT 10 public: 11 UsbHelper(); 12 ~UsbHelper(); 13 14 bool InitUsb(); 15 16 bool WriteData(const QString& pBuffer); 17 18 bool ReadData(QString &pBuffer); 19 20 bool WaitResponse(int pTimeout, QStringList &pResponseList); 21 22 void CloseUsb(); 23 24 bool SendCommand(const QString &pCommand, QStringList &pResponseList); 25 private: 26 usb_dev_handle *mUsbHandle; 27 struct usb_device *mUsbDev; 28 };
usbhelper.cpp
#include "usbhelper.h" #include <QtCore/QThread> #include <QtCore/QByteArray> #include <QtCore/QTime> #define IDVENDOR 0x10EE #define IDPRODUCT 0xAA55 #define ENDPOINT_OUT 0x02 #define ENDPOINT_IN 0x86 UsbHelper::UsbHelper() : mUsbHandle(NULL), mUsbDev(NULL) { } UsbHelper::~UsbHelper() { CloseUsb(); } bool UsbHelper::InitUsb() { usb_init(); usb_find_busses(); usb_find_devices(); struct usb_bus *pbus = NULL; struct usb_device *pdev = NULL; usb_set_debug(255); for(pbus=usb_busses;pbus!=NULL;pbus=pbus->next){ for(pdev=pbus->devices;pdev!=NULL;pdev=pdev->next){ struct usb_device_descriptor *desc=&pdev->descriptor; if(desc->idVendor==0x10EE && desc->idProduct==0xAA55){ mUsbDev = pdev; } } } if(mUsbDev) { mUsbHandle = usb_open(mUsbDev); if(mUsbHandle) { usb_claim_interface(mUsbHandle,0); return true; } } return false; } void UsbHelper::CloseUsb() { if(mUsbHandle) { usb_release_interface(mUsbHandle,0); usb_close(mUsbHandle); } } bool UsbHelper::ReadData(QString &pBuffer) { return true; } bool UsbHelper::WriteData(const QString &pBuffer) { char buff[1024]; memset(buff, 0, sizeof(buff)); int ret = usb_bulk_read(mUsbHandle, ENDPOINT_IN, buff, sizeof(buff), 100); /* if(ret < 0) { qDebug("usb_bulk_read error"); return false; } */ qDebug("pre read:"); char buffer[1024]; QString command = pBuffer; while(command.length() > 0) { QString sendcommand = command.left(sizeof(buffer)); command.remove(0, sizeof(buffer)); int sendlen = sendcommand.length(); QByteArray ba = sendcommand.toLatin1(); memcpy(buffer, ba.data(), sendcommand.length()); ret = usb_bulk_write(mUsbHandle, ENDPOINT_OUT, buffer, sendlen, 500); if(ret < 0) { qDebug("usb_bulk_write failed"); return false; } else { qDebug("write %s success", buffer); } // QThread::sleep(1); } return true; } bool UsbHelper::WaitResponse(int pTimeout, QStringList &pResponseList) { qDebug("Enter WaitResponse"); pResponseList.clear(); QString line; QTime time; time.start(); // while (time.elapsed() < pTimeout) { char readedbuffer[1000]; int readedSize = 1000; int ret = usb_bulk_read(mUsbHandle, ENDPOINT_IN, readedbuffer, sizeof(readedbuffer), 300); if (ret < 0) { qDebug("WaitResponse sub_bulk_read error"); return false; } for (int i=0; i<readedSize; i++) { quint8 byte = readedbuffer[i]; if (byte == 0x0D) { if (! line.isEmpty()) { pResponseList.append(line); line = ""; } } else { line += byte; } } if (! line.isEmpty()) { pResponseList.append(line); } //´òÓ¡ÊÕµ½µÄÐÅÏ¢¡£ foreach (QString command, pResponseList) { qDebug("**> %s", qPrintable(command)); } return pResponseList.count() > 0; } bool UsbHelper::SendCommand(const QString &pCommand, QStringList &pResponseList) { if(! WriteData(pCommand)) { qDebug("Write Data failed"); return false; } if(! WaitResponse(100, pResponseList)) { qDebug("WaitResponse failed"); return false; } if (pResponseList[0] != pCommand) { return false; } return true; }
main.cpp
#include <stdio.h> #include <stdlib.h> #include <usb.h> #include <QtCore/QCoreApplication> #include "usbhelper.h" int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); UsbHelper *helper = new UsbHelper; if(! helper->InitUsb()) { qDebug("InitUsb failed"); return -1; } QStringList strList; if(! helper->SendCommand("W", strList)) { qDebug("Send W failed"); return -1; } if(! helper->SendCommand("X", strList)) { qDebug("Send X failed"); return -1; } if(! helper->SendCommand("Y", strList)) { qDebug("Send Y failed"); return -1; } QString command = "C1,00,01,FF,FF,00,00,20,02,20,02,00,04,08,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,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,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,FF,FF,FF,00,00,FD,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00"; if(! helper->WriteData(command)) { qDebug("Write Data failed"); return false; } if(! helper->WaitResponse(100, strList)) { qDebug("WaitResponse failed"); return false; } if (strList[0] != "OK") { qDebug("send brigntness error"); return false; } else { qDebug("send brigntness success"); } app.exec(); return 0; }
usb.pro
TEMPLATE = app LANGUAGE = c++ QT += gui core LIBS += -lusb -ldl HEADERS = pvcmbasethread.h pvdvdbstardevicecommunicatethread.h SOURCES = main.cpp pvcmbasethread.cpp pvdvdbstardevicecommunicatethread.cpp unix { MOC_DIR = .moc OBJECTS_DIR = .obj }
时间: 2024-10-05 23:50:14