MAC上读出usb
来源:互联网 发布:vb多线程实例 编辑:程序博客网 时间:2024/05/16 01:33
原文链接
mac上使用qt平台开发,读串口的话有QSerialPortinfo、QSerialPort
但是mac上没有串口,只有usb口,与串口通信也是通过usb口。使用libusb库
一、安装libusb
正常源码编译安装即可。
二、libusb动态库加入QT项目
QT项目中新建文件夹lib。,将/usr/local/lib/libusb-1.0.0.dylib复制到lib/下。
将第一步解压出来的libusb-1.0.21/libusb/libusb.h拷贝到Qt项目中lib目录下。
项目中.pro文件,加入
LIBS +=\
-L$$PWD/lib/ -lusb-1.0.0
测试代码如下
getusbinfo.h
#ifndef GETUSBINFO#define GETUSBINFO#include <QString>#include <QObject>#include <QList>#include <QThread>#include <libusb.h>struct STUUSBDevices{ QString idProduct; QString idVendor; QString iManufacturer; QString iSerialNumber;};class GetUsbInfo : QThread{public: GetUsbInfo(QObject *parent); ~GetUsbInfo(); int initUsbDevices(); QString getVidPid(libusb_device **devs); void showAllUsbDevices(QList<STUUSBDevices> lst); void setRunStatus(); void run(); bool isStop;};#endif // GETUSBINFO
getusbinfo.cpp
#include "getusbinfo.h"#include <QThread>#include <QDebug>#include <QString>GetUsbInfo::GetUsbInfo(QObject *parent) : QThread(parent),isStop(false){}GetUsbInfo::~GetUsbInfo(){ qDebug()<<"GetUsbInfo::~GetUsbInfo "<<endl;}int GetUsbInfo::initUsbDevices(){ libusb_device **devs; int r; ssize_t cnt; r = libusb_init(NULL); if (r < 0) return r; cnt = libusb_get_device_list(NULL, &devs); if (cnt < 0) return (int) cnt; getVidPid(devs); libusb_free_device_list(devs, 1); libusb_exit(NULL); return 0;}QString GetUsbInfo::getVidPid(libusb_device **devs){ libusb_device *dev; int i = 0; QList<STUUSBDevices> lstUsb; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { qDebug()<<"failed to get device descriptor"<<stderr; return ""; } printf("%04x:%04x (bus %d, device %d)\n", desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); STUUSBDevices stu; stu.idProduct = QString::number(desc.idProduct); stu.idVendor = QString::number(desc.idVendor); stu.iManufacturer = QString::number(desc.iManufacturer); stu.iSerialNumber = QString::number(desc.iSerialNumber); lstUsb.append(stu); } showAllUsbDevices(lstUsb); return QString(lstUsb[0].idProduct);}void GetUsbInfo::showAllUsbDevices(QList<STUUSBDevices> lst){ for(int i=0;i<lst.count();i++) { qDebug()<<"vid: "<<lst.at(i).idVendor<<"\n" <<"pid:"<<lst.at(i).idProduct<<"\n" <<"serNumber:"<<lst.at(i).iSerialNumber<<"\n" <<"Manufacturer:"<<lst.at(i).iManufacturer<<"\n"; }}void GetUsbInfo::setRunStatus(){ isStop = true;}void GetUsbInfo::run(){ qDebug()<<"GetUsbInfo::run() "<<endl; while (!isStop) { initUsbDevices(); sleep(10); }}
main.cpp
#include "mainwindow.h"#include <QApplication>#include "getusbinfo.h"#include <QLibrary>int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); QThread t; GetUsbInfo info(&t); info.initUsbDevices(); return a.exec();}
阅读全文
0 0
- MAC上读出usb
- 让MAC读出代码
- Mac上制作Ubuntu USB启动盘
- 【转】Mac 上制作 Ubuntu USB 启动盘
- MAC OS 系统 macbook上使用USB转串口线
- MAC 上 OSX 安装成usb启动盘的命令
- 从ext2文件系统上读出超级块
- USB device for mac
- 在Mac OS X苹果lion系统上制作USB启动盘
- Raspberry - MAC上使用USB转串口线连接访问树莓派
- OSX: Scripts,让BootCamp在任何Mac上支持生成Windows7的USB安装盘
- MAC上配置搭配CH340/CH341 USB转串口芯片的Arduino
- 在 QT 中使用 libusb 检测 MAC 上的 USB 设备
- Mac osx 10.10 苹果机上用DD制作Ubuntu的USB启动盘
- 树梅派应用39: Mac OS X上使用USB转串口线连接树莓派
- MAC上
- usb上的Linux
- USB基础知识(上)
- Spring Boot 热部署
- 常用命令总结
- 数据挖掘中的关联规则
- Android中常见的内存泄漏汇总
- sas统计分析学习笔记(五)
- MAC上读出usb
- Lucene搜索引擎+HDFS+MR完成垂直搜索
- 匈牙利算法
- Flask-hello程序
- Super Jumping! Jumping! Jumping!
- 今天写了一条把我两个月职业生涯学到的SQL知识全用上了的语句
- 关闭服务器windows server的IE浏览器的增强安全配置
- 文本相关性排序
- 用javadoc命令生成api帮助文档