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

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值