Qt for Android : 使用libusb做CH340x串口传输的底层USB库

简介

Qt for Android自带的串口方案并没有适用在高的API版本中, 会出现permission denied的访问问题, 所以就需要使用Android API, 也就是在CPP中使用JNI方式进行调用, 为了开发的方便, 使用libusb库作为替代的底层usb传输是比较合适的, 这里基于 Qt for android 串口库使用 这篇所说的java 编写的ch340x的库简单修改成调用libusb进行串口通讯的方法验证。

代码

别说一上来就是代码, 其实翻译有啥步骤, 上来就是撸代码

ch340x.h

#ifndef CH340X_H
#define CH340X_H#include "libusb/libusb.h"
#include <QSerialPort>class CH340X
{
public:enum ControlLine { RTS, CTS,  DTR, DSR,  CD, RI };enum DataBits{DATABITS_5 = 5,DATABITS_6 = 6,DATABITS_7 = 7,DATABITS_8 = 8,};enum Parity{PARITY_NONE = 0,PARITY_ODD = 1,PARITY_EVEN = 2,PARITY_MARK = 3,PARITY_SPACE = 4,};enum STOPBITS{STOPBITS_1 = 1,STOPBITS_1_5 = 3,STOPBITS_2 = 2,};CH340X(int fd=-1);~CH340X();inline bool isValid() { return m_isValid; }int send(unsigned char *src, int length, int timeout);private:int controlOut(int request, int value, int index);int controlIn(int request, int value, int index, unsigned char *buffer, uint16_t length);;int setControlLines();int getStatus(char &status);int checkState(int request, int value,char *expected,unsigned int length);int setBaudRate(int baudRate);int setParameters(int baudRate, int dataBits, int stopBits, int parity);int init_ch34x(int fd);private:bool m_isValid = false;struct libusb_device_handle *devh = NULL;
};#endif // CH340X_H

ch340x.cpp

#include "ch340x.h"#include <QDebug>#include "libusb/libusb.h"static int LCR_ENABLE_RX   = 0x80;
static int LCR_ENABLE_TX   = 0x40;
static int LCR_MARK_SPACE  = 0x20;
static int LCR_PAR_EVEN    = 0x10;
static int LCR_ENABLE_PAR  = 0x08;
static int LCR_STOP_BITS_2 = 0x04;
static int LCR_CS8         = 0x03;
static int LCR_CS7         = 0x02;
static int LCR_CS6         = 0x01;
static int LCR_CS5         = 0x00;static int GCL_CTS = 0x01;
static int GCL_DSR = 0x02;
static int GCL_RI  = 0x04;
static int GCL_CD  = 0x08;
static int SCL_DTR = 0x20;
static int SCL_RTS = 0x40;int USB_TIMEOUT_MILLIS = 5000;
int DEFAULT_BAUD_RATE = 9600;
bool dtr = false;
bool rts = false;#define USB_TYPE_VENDOR 0x40
#define USB_DIR_OUT 0x0
// #define EP_DATA_IN        (0x2|LIBUSB_ENDPOINT_IN)
#define EP_DATA_OUT       (0x2|LIBUSB_ENDPOINT_OUT)
#define EP_INTR			(1 | LIBUSB_ENDPOINT_IN)
#define EP_DATA			(2 | LIBUSB_ENDPOINT_IN)
#define CTRL_IN			(LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
#define CTRL_OUT		(LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
#define USB_RQ			0x04
#define INTR_LENGTH		64CH340X::CH340X(int fd)
{m_isValid = (init_ch34x(fd) == 0);
}CH340X::~CH340X()
{// if (recv_bulk_transfer)// {//     libusb_cancel_transfer(recv_bulk_transfer);//     libusb_free_transfer(recv_bulk_transfer);// }if (devh){libusb_release_interface(devh, 0);libusb_close(devh);libusb_exit(NULL);}
}int CH340X::controlOut(int request, int value, int index)
{return libusb_control_transfer(devh, CTRL_OUT, request, value, index, 0, 0, USB_TIMEOUT_MILLIS);
}int CH340X::controlIn(int request, int value, int index, unsigned char *buffer, uint16_t length)
{return libusb_control_transfer(devh, CTRL_IN, request, value, index, buffer, length, USB_TIMEOUT_MILLIS);
}int CH340X::setControlLines()
{return controlOut(0xa4, ~((dtr ? SCL_DTR : 0) | (rts ? SCL_RTS : 0)), 0);
}int CH340X::getStatus(char &status)
{char buffer[2];int ret = controlIn(0x95, 0x0706, 0, (unsigned char*)buffer, 2);status = buffer[0];return ret;
}int CH340X::checkState(int request, int value,char *expected,unsigned int length)
{char *buffer = new char[length];int ret = controlIn(request, value, 0, (unsigned char*)buffer, length);if (ret < 0) {return ret;}if (ret != length) {// qDebug("Expected " [ length] + " bytes, but get " + ret + " [" + msg + "]");return ret;}for (int i = 0; i < length; i++){if (expected[i] == -1){continue;}int current = buffer[i] & 0xff;if (expected[i] != current){// qDebug("Expected 0x" + Integer.toHexString(expected[i]) + " byte, but get 0x" + Integer.toHexString(current) + " [" + msg + "]");return -1;}}return 0;
}int CH340X::setBaudRate(int baudRate)
{long factor;long divisor;if (baudRate == 921600){divisor = 7;factor = 0xf300;}else{long BAUDBASE_FACTOR = 1532620800;int BAUDBASE_DIVMAX = 3;factor = BAUDBASE_FACTOR / baudRate;divisor = BAUDBASE_DIVMAX;while ((factor > 0xfff0) && divisor > 0){factor >>= 3;divisor--;}if (factor > 0xfff0) { // 波特率不支持return -1;}factor = 0x10000 - factor;}divisor |= 0x0080; // else ch341a waits until buffer fullint val1 = (int) ((factor & 0xff00) | divisor);int val2 = (int) (factor & 0xff);// Log.d(TAG, String.format("baud rate=%d, 0x1312=0x%04x, 0x0f2c=0x%04x", baudRate, val1, val2));int ret = controlOut(0x9a, 0x1312, val1);if (ret < 0) {return ret;// throw new IOException("Error setting baud rate: #1)");}ret = controlOut(0x9a, 0x0f2c, val2);if (ret < 0) {return ret;// throw new IOException("Error setting baud rate: #2");}return 0;
}int CH340X::setParameters(int baudRate, int dataBits, int stopBits, int parity)
{if(baudRate <= 0){return -1;// throw new IllegalArgumentException("Invalid baud rate: " + baudRate);}setBaudRate(baudRate);int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX;switch (dataBits) {case DATABITS_5:lcr |= LCR_CS5;break;case DATABITS_6:lcr |= LCR_CS6;break;case DATABITS_7:lcr |= LCR_CS7;break;case DATABITS_8:lcr |= LCR_CS8;break;default:return -1;// throw new IllegalArgumentException("Invalid data bits: " + dataBits);}switch (parity) {case PARITY_NONE:break;case PARITY_ODD:lcr |= LCR_ENABLE_PAR;break;case PARITY_EVEN:lcr |= LCR_ENABLE_PAR | LCR_PAR_EVEN;break;case PARITY_MARK:lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE;break;case PARITY_SPACE:lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN;break;default:return -1;// throw new IllegalArgumentException("Invalid parity: " + parity);}switch (stopBits) {case STOPBITS_1:break;case STOPBITS_1_5:return -1;// throw new UnsupportedOperationException("Unsupported stop bits: 1.5");case STOPBITS_2:lcr |= LCR_STOP_BITS_2;break;default:return -1;// throw new IllegalArgumentException("Invalid stop bits: " + stopBits);}int ret = controlOut(0x9a, 0x2518, lcr);if (ret < 0) {return ret;// throw new IOException("Error setting control byte");}return 0;
}int CH340X::init_ch34x(int fd)
{int r = 0;libusb_context *ctx = NULL;r = libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);if (r != LIBUSB_SUCCESS) {qDebug("libusb_set_option failed: %d\n", r);return r;}r = libusb_init(&ctx);if (r < 0) {qDebug("libusb_init failed: %d\n", r);return r;}r = libusb_wrap_sys_device(ctx, (intptr_t)fd, &devh);if (r < 0) {qDebug("libusb_wrap_sys_device failed: %d\n", r);return r;} else if (devh == NULL) {qDebug("libusb_wrap_sys_device returned invalid handle\n");return r;}r = libusb_claim_interface(devh, 0); // 独占if (r < 0) {qDebug("usb_claim_interface error %d\n", r);return r;}char stateCmd[2] = {static_cast<char>(-1), 0};checkState(0x5f, 0, stateCmd, 2);if (controlOut(0xa1, 0, 0) < 0) {return -1;// throw new IOException("Init failed: #2");}setBaudRate(DEFAULT_BAUD_RATE);checkState(0x95, 0x2518, stateCmd, 2);if (controlOut(0x9a, 0x2518, LCR_ENABLE_RX | LCR_ENABLE_TX | LCR_CS8) < 0) {return -1;// throw new IOException("Init failed: #5");}stateCmd[1] = -1;checkState(0x95, 0x2518, stateCmd, 2);if (controlOut(0xa1, 0x501f, 0xd90a) < 0) {return -1;// throw new IOException("Init failed: #7");}setBaudRate(DEFAULT_BAUD_RATE);setControlLines();checkState(0x95, 0x2518, stateCmd, 2);r = setParameters(9600, DATABITS_8, STOPBITS_1, PARITY_NONE);return r;
}int CH340X::send(unsigned char *src, int length, int timeout)
{if (!m_isValid)return -1;int realSendLength = 0;return libusb_bulk_transfer(devh, EP_DATA_OUT, src, length, &realSendLength, timeout);}

mainwindows.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ch340x.h"
#include <QJniObject>
#include <QJniEnvironment>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);connect(ui->btnRefreshPortNames, &QPushButton::clicked,this, [=](){ui->cbPortNames->clear();ui->cbPortNames->addItems(getSerialPorts());});connect(ui->btnTest, &QPushButton::clicked,this, &MainWindow::test);emit ui->btnRefreshPortNames->clicked(); // 借势初始化
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::logger(const QString &text)
{ui->textEdit->append(text);
}QList<QString> MainWindow::getSerialPorts()
{QList<QString> names;
#ifdef Q_OS_ANDROIDQJniObject usbService = QJniObject::getStaticObjectField("android/content/Context","USB_SERVICE","Ljava/lang/String;");if (!usbService.isValid()){logger("fail to get usb service");return names;}QJniObject activity = QJniObject(QNativeInterface::QAndroidApplication::context());QJniObject usbManager = activity.callObjectMethod("getSystemService","(Ljava/lang/String;)Ljava/lang/Object;",usbService.object<jstring>());if (!usbManager.isValid()){logger("fail to get usb manager");return names;}QJniObject usbDeviceListHashMap = usbManager.callObjectMethod("getDeviceList","()Ljava/util/HashMap;");QJniObject devListKeySet = usbDeviceListHashMap.callObjectMethod("keySet","()Ljava/util/Set;");QJniObject devListIter = devListKeySet.callObjectMethod("iterator","()Ljava/util/Iterator;");jint devListSize = usbDeviceListHashMap.callMethod<jint>("size", "()I");QJniObject usbDevObj;QJniObject usbDevObjIter;int vid = 0, pid = 0;QString devName;for (int i = 0; i < devListSize; ++i){usbDevObjIter = devListIter.callObjectMethod("next","()Ljava/lang/Object;");usbDevObj = usbDeviceListHashMap.callObjectMethod("get","(Ljava/lang/Object;)Ljava/lang/Object;",usbDevObjIter.object());vid = usbDevObj.callMethod<jint>("getVendorId", "()I");pid = usbDevObj.callMethod<jint>("getProductId", "()I");devName = usbDevObj.callMethod<jstring>("getDeviceName", "()Ljava/lang/String;").toString();logger(QString("Name: %1, VID: %2, PID: %3").arg(devName).arg(vid).arg(pid));names.append(devName);}
#elsefor (QSerialPortInfo &info : QSerialPortInfo::availablePorts()){names.append(info.portName());}
#endifreturn names;
}void MainWindow::test()
{QJniObject str = QJniObject::fromString(ui->cbPortNames->currentText());jboolean res = QJniObject::callStaticMethod<jboolean>("usb/USBListActivity","requestUSBPermission","(Ljava/lang/String;)Z",str.object<jstring>());if (!res){logger("permission denied");return;}QJniObject usbService = QJniObject::getStaticObjectField("android/content/Context","USB_SERVICE","Ljava/lang/String;");if (!usbService.isValid()){logger("fail to get usb service");return;}QJniObject activity = QJniObject(QNativeInterface::QAndroidApplication::context());QJniObject usbManager = activity.callObjectMethod("getSystemService","(Ljava/lang/String;)Ljava/lang/Object;",usbService.object<jstring>());if (!usbManager.isValid()){logger("fail to get usb manager");return;}QJniObject usbDeviceListHashMap = usbManager.callObjectMethod("getDeviceList","()Ljava/util/HashMap;");QJniObject devListKeySet = usbDeviceListHashMap.callObjectMethod("keySet","()Ljava/util/Set;");QJniObject devListIter = devListKeySet.callObjectMethod("iterator","()Ljava/util/Iterator;");jint devListSize = usbDeviceListHashMap.callMethod<jint>("size", "()I");QJniObject usbDevObj;QJniObject usbDevObjIter;QString devName;QJniObject selectedUsbDevice;for (int i = 0; i < devListSize; ++i){usbDevObjIter = devListIter.callObjectMethod("next","()Ljava/lang/Object;");usbDevObj = usbDeviceListHashMap.callObjectMethod("get","(Ljava/lang/Object;)Ljava/lang/Object;",usbDevObjIter.object());devName = usbDevObj.callMethod<jstring>("getDeviceName", "()Ljava/lang/String;").toString();if (devName != ui->cbPortNames->currentText()){continue;}selectedUsbDevice = usbDevObj;break;}if (!selectedUsbDevice.isValid()){logger("device not found");return;}QJniObject usbConnection = usbManager.callObjectMethod("openDevice","(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;",selectedUsbDevice.object());if (!usbConnection.isValid()){logger("fail to get usb connection");return;}jint fd = usbConnection.callMethod<jint>("getFileDescriptor", "()I");CH340X ch340X(fd);const char buf[] = {"Hello World!!!\n"};if (!ch340X.isValid()){logger("fail to init ch340x.");return;}ch340X.send((unsigned char*)buf, sizeof(buf), 1000);// unrooted_usb_description(fd);logger("libusb test finished7");
}

共赏

Qt for Android :使用 libusb做ch340x串口传输

其他

沁恒微 官网CH34x Linux驱动
沁恒微 官网CH34x Android驱动

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/20578.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SpringBoot注解--10--@Bean,对象注入的三种方法

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 Bean一、如何使用方法注解注意Bean 的命名规则&#xff0c;当没有设置 name 属性时&#xff0c;那么 bean 默认的名称就是方法名&#xff0c;当设置了 name 属性之后…

解析Java中1000个常用类:Runnable 类,你学会了吗?

在 Java 编程中,处理并发和多线程是一个重要的主题。为了简化多线程编程,Java 提供了多种工具和类,其中最基本的一个工具就是 Runnable 接口。 Runnable 接口为创建和管理线程提供了一种标准的方式。本文将详细介绍 Runnable 接口的定义、实现原理、应用场景,并通过示例展…

33【Aseprite 作图】树——拆解

1 树叶 画树叶真累啊&#xff0c;可以先画一个轮廓&#xff0c;细节一点点修 2 1 2 &#xff1b;2 2 2 &#xff08;横着横&#xff09;&#xff0c;这样一点点画树叶 填充颜色&#xff0c;用了喷雾工具 2 树干部分 轮廓部分&#xff0c;左边的是3 3 3 &#xff1b;上下都是…

网页音频提取在线工具有哪些 网页音频提取在线工具下载

别再到处去借会员账号啦。教你一招&#xff0c;无视版权和地区限制&#xff0c;直接下载网页中的音频文件。没有复杂的操作步骤&#xff0c;也不用学习任何代码。只要是网页中播放的音频文件&#xff0c;都可以把它下载到本地保存。 一、网页音频提取在线工具有哪些 市面上的…

【数据结构】二叉树:简约和复杂的交织之美

专栏引入&#xff1a; 哈喽大家好&#xff0c;我是野生的编程萌新&#xff0c;首先感谢大家的观看。数据结构的学习者大多有这样的想法&#xff1a;数据结构很重要&#xff0c;一定要学好&#xff0c;但数据结构比较抽象&#xff0c;有些算法理解起来很困难&#xff0c;学的很累…

Transformer中的位置编码PE(position encoding)

Transformer中的位置编码PE(position encoding) 1.提出背景 transformer模型的attention机制并没有包含位置信息&#xff0c;即一句话中词语在不同的位置时在transformer中是没有区别的 2.解决背景 给encoder层和decoder层的输入添加了一个额外的向量Positional Encoding&a…

平移数据c++

题目描述 将a数组中第一个元素移到数组末尾,其余数据依次往前平移一个位置。 输入 第一行为数组a的元素个数n&#xff1b; 第二行为n个小于1000的正整数。 输出 平移后的数组元素&#xff0c;每个数用一个空格隔开。 样例输入 10 1 2 3 4 5 6 7 8 9 10 样例输出 2 3 …

【专利 超音速】一种光伏检测系统

申请号CN202410053901.0公开号&#xff08;公开&#xff09;CN118032774A申请日2024.01.12申请人&#xff08;公开&#xff09;超音速人工智能科技股份有限公司发明人&#xff08;公开&#xff09;张俊峰(总); 叶长春(总); 许春夏 摘要 本发明公开一种光伏检测系统&#xff0…

iotdb时序库在火电设备锅炉场景下的实践【原创文字,IoTDB社区可进行使用与传播】

一.概述 1.1 说明 本文章主要介绍iotdb数据库在电站锅炉工业场景下&#xff0c;对辅助智能分析与预警的使用介绍。 【原创文字&#xff0c;IoTDB社区可进行使用与传播】 1.2 项目背景 随着人工智能算法在电力领域的发展&#xff0c;以及燃煤锅炉设备精细化调整需求的增加&…

Java基础八股

Java基础八股 Java语言Java语言有什么特点Java与C区别Java如何实现跨平台JVMvsJDKvsJRE标识符和关键字的区别是什么自增自减运算符移位运算符continue,break,return的区别是什么final,finally,finalize的区别final关键字的作用时什么 变量 Java语言 Java语言有什么特点 Java是…

LED灯编程:一步步探索光的魔法

LED灯编程&#xff1a;一步步探索光的魔法 在数字时代&#xff0c;LED灯早已超越了传统的照明功能&#xff0c;成为编程与创意结合的完美载体。那么&#xff0c;LED灯怎么编程呢&#xff1f;本文将分四个方面、五个方面、六个方面和七个方面&#xff0c;带您走进LED灯编程的奇…

如何在Python中管理内存

在Python中&#xff0c;内存管理主要是由解释器自动处理的&#xff0c;这包括对象的分配和回收。Python使用引用计数和垃圾回收机制来管理内存&#xff0c;这大大简化了开发者的工作&#xff0c;因为他们通常不需要手动管理内存。 然而&#xff0c;尽管Python自动管理内存&…

数据结构——经典链表OJ(二)

乐观学习&#xff0c;乐观生活&#xff0c;才能不断前进啊&#xff01;&#xff01;&#xff01; 我的主页&#xff1a;optimistic_chen 我的专栏&#xff1a;c语言 点击主页&#xff1a;optimistic_chen和专栏&#xff1a;c语言&#xff0c; 创作不易&#xff0c;大佬们点赞鼓…

chatgpt之api的调用问题

1.调用api过程中&#xff0c;出现如下报错内容 先写一个测试样例 import openaiopenai.api_key "OPEN_AI_KEY" openai.api_base"OPEN_AI_BASE_URL" # 是否需要base根据自己所在地区和key情况进行completion openai.ChatCompletion.create(model"g…

【intro】GNN中异构图(heterogeneous graph)综述

本篇博客内容是读两篇论文&#xff0c;两篇论文连接如下&#xff1a; Heterogeneous graph neural networks analysis: a survey of techniques, evaluations and applications A Survey on Heterogeneous Graph Embedding: Methods, Techniques, Applications and Sources …

瓦罗兰特国际服 外服游玩教程 瓦罗兰特外服下载注册游玩指南

瓦罗兰特国际服 外服游玩教程 瓦罗兰特外服下载注册游玩指南 瓦罗兰特作为当今游戏圈顶流的一款热门FPS。游戏&#xff0c;作为拳头游戏公司划时代的一款游戏。游戏不仅延续了传统FPS游戏的玩法&#xff0c;还添加许多新玩法&#xff0c;这也是游戏可以吸引大批量玩家的原因之…

Flink面试整理-对Flink的高级特性如CEP(复杂事件处理)、状态后端选择和调优等有所了解

Apache Flink 提供了一系列高级特性,使其成为一个强大的实时数据处理框架,特别适用于复杂的数据处理场景。其中,复杂事件处理(CEP)、状态后端的选择和调优是其中重要的几个方面。 复杂事件处理(CEP) CEP 概念:CEP 是用于在数据流中识别复杂模式的技术。它允许用户指定事…

基于电导增量MPPT控制算法的光伏发电系统simulink建模与仿真

目录 1.课题概述 2.系统仿真结果 3.核心程序与模型 4.系统原理简介 5.完整工程文件 1.课题概述 基于电导增量MPPT控制算法的光伏发电系统simulink建模与仿真。输出MPPT跟踪后的系统电流&#xff0c;电压以及功率。 2.系统仿真结果 3.核心程序与模型 版本&#xff1a;MAT…

cocos creator 3.x实现手机虚拟操作杆

简介 在许多移动游戏中&#xff0c;虚拟操纵杆是一个重要的用户界面元素&#xff0c;用于控制角色或物体的移动。本文将介绍如何在Unity中实现虚拟操纵杆&#xff0c;提供了一段用于移动控制的代码。我们将讨论不同类型的虚拟操纵杆&#xff0c;如固定和跟随&#xff0c;以及如…

Go常见语法题目解析

1、写出下面代码输出内容。 package mainimport ("fmt" )func main() {defer_call() }func defer_call() {defer func() { fmt.Println("打印前") }()defer func() { fmt.Println("打印中") }()defer func() { fmt.Println("打印后")…