NDK USB-HID 转串口通信参数设置代码

本文提供了一个使用hidapi库进行USB设备交互的C语言示例代码。该代码展示了如何通过发送特征报告来设置设备参数,读取设备状态,并执行如设置波特率等操作。通过解析示例,读者可以了解如何实现USB设备的读写功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


#include <jni.h>
#include <android/log.h>


#include "hidapi.h"




void hid_read_test(hid_device *handle)
{
pthread_detach( pthread_self());


int res = 0;
unsigned char buf[65] = {0};
int i;


while(1)
{
//printf("USB READ ....\n");


buf[0] = 0x41; // First byte is report number
buf[1] = 0x00;
res = hid_send_feature_report(handle, buf, 2);

buf[1] = 0x01;
res = hid_send_feature_report(handle, buf, 2);


buf[0] = 25;
// Read requested state
res = hid_read(handle, buf,26);
if (res < 0){
printf("Unable to read() res=%d \n", res);
sleep(1);
continue;
//return ;
}
else if(res == 0)
{
continue;
//return;
}


printf(" read() res=%d \n", res);
// Print out the returned buffer.
for (i = 0; i < res; i++)
printf("buf[%d]: %02X\n", i, buf[i]);
printf("\n\n");

}
//pthread_exit(0);
return 0;
}


int setBRate(hid_device *handle)
{
unsigned char buf[65];
int res = 0;
int i = 0;

buf[0] = 0x50; // First byte is report number


#if 0
buf[1] = 0x00;//brate 38400
buf[2] = 0x01;
buf[3] = 0xc2;
buf[4] = 0x00;
#endif
buf[1] = 0x00;//brate 38400
buf[2] = 0x00;
buf[3] = 0x96;
buf[4] = 0x00;

buf[5] = 0x00;
buf[6] = 0x00;
buf[7] = 0x03;
buf[8] = 0x00;



res = hid_send_feature_report(handle, buf, 9);
printf("res=%d \n", res);


memset(buf, 0, sizeof(buf));
buf[0] = 0x50; 
res = hid_get_feature_report(handle, buf,10);
printf("get report: %d \n", res);
for (i = 0; i < res; i++)
printf("buf[%d]: %02X\n", i, buf[i]);
printf("\n\n");
}


int hid_set_reset(hid_device *handle)
{
unsigned char buf[65];
int res = 0;
int i = 0;
buf[0] = 0x40; 
buf[1] = 0x00; 

res = hid_send_feature_report(handle, buf,2);
printf("res: %d \n", res);
for (i = 0; i < res; i++)
printf("buf[%d]: %02X\n", i, buf[i]);
printf("\n\n");
}


void GET_UART_STATUS(hid_device *handle)
{
unsigned char buf[65] = {0};
int res = 0;
int i = 0;
buf[0] = 0x42; 

res = hid_get_feature_report(handle, buf,17);
printf("GET_UART_STATUS res: %d \n", res);
for (i = 0; i < res; i++)
printf("buf[%d]: %02X\n", i, buf[i]);
printf("\n\n");
}


void SET_TRANSFER_MODE(hid_device *handle)
{
unsigned char buf[65] = {0};
int res = 0;
int i = 0;
buf[0] = 0x48; 
buf[1] = 0x0; 
buf[2] = 0x0; 
res = hid_send_feature_report(handle, buf,3);


res = hid_get_feature_report(handle, buf,17);
printf("GET_TRANSFER_MODE res: %d \n", res);
for (i = 0; i < res; i++)
printf("buf[%d]: %02X\n", i, buf[i]);
printf("\n\n");
}


void SET_SUSPEND_OUTPUT_CONFIG(hid_device *handle)
{
unsigned char buf[65] = {0};
int res = 0;
int i = 0;
buf[0] = 0x4a; 
buf[1] = 0x80; 
buf[2] = 0x00; 
buf[1] = 0x80; 
buf[2] = 0x00; 

//res = hid_send_feature_report(handle, buf,5);


memset(buf, 0, sizeof(buf));
buf[0] = 0x4a; 
res = hid_get_feature_report(handle, buf,17);
printf("SET_SUSPEND_OUTPUT_CONFIG res: %d \n", res);
for (i = 0; i < res; i++)
printf("buf[%d]: %02X\n", i, buf[i]);
printf("\n\n");




return;
}
CH9326 是一款HID 串口芯片。CH9326 支持双向数据传输,用于接收串口数据,并按照HID 类设备规范,将数据打包通过USB口上传给计算机,或者从计算机接收符合HID 类设备的USB数据包,并从串口进行发送。通过提供的上位机软件,用户也可自行配置芯片的VID、PID,以及各种字符串描述符。 下图为其一般应用框图: HID串口评估板电路 PCB、原理图截图,见“相关文件”下载工程文件: 说明: P1是USB端口,USB总线包括一对5V电源线和一对数据信号线,通常,+5V电源线是红色,接地线是黑色,D+信号线是绿色,D-信号线是白色。USB总线提供的电源电流最大可以达到500mA,一般情况下,CH9326芯片和低功耗的USB产品可以直接使用USB总线提供的5V电源。 如果USB产品通过其它供电方式提供常备电源,那么CH9326也应该使用该常备电源,如果需要同时使用USB总线的电源,那么可以通过阻值约为1Ω的电阻连接USB总线的5V电源线与USB产品的5V常备电源,并且两者的接地线直接相连接。 P2是TTL串口,TXD是CH9326的串行发送引脚;RXD是CH9326的串行接收引脚。 P3是预留的用户可自行配置使用的4个普通IO引脚。 C3容量为4700pF到0.02μF,用于CH9326内部电源节点退耦,C2容量为0.1μF,用于外部电源退耦。晶体X1、电阻R1和电容C4用于时钟振荡电路。X1是频率为12MHz的石英晶体或者陶瓷晶体,C4和 C5是容量为20pF~47pF的独石或高频瓷片电容。如果X1选用低成本的陶瓷晶体,那么C4和C5的容量必须用该晶体厂家的推荐值,一般情况下是47pF。 附件内容包括:CH9326评估板说明及原理图和参考例程,包含各平台下应用程序库与操作例程。如截图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值