libusb示例
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <stdint.h>
#include <string.h> void processMessage(const uint8_t*); /*----------------------------------------------------------------------*/
int main(int argc, char*argv[])
{
int res = ; /* return codes from libusb functions */
libusb_device_handle* handle = ; /* handle for USB device */
int kernelDriverDetached = ; /* Set to 1 if kernel driver detached */
int numBytes = ; /* Actual bytes transferred. */
uint8_t buffer[]; /* 64 byte transfer buffer */ /* Initialise libusb. */
res = libusb_init();
if (res != )
{
fprintf(stderr, "Error initialising libusb.\n");
return ;
} /* Get the first device with the matching Vendor ID and Product ID. If
* intending to allow multiple demo boards to be connected at once, you
* will need to use libusb_get_device_list() instead. Refer to the libusb
* documentation for details. */
handle = libusb_open_device_with_vid_pid(, 0x04d8, 0x0070);
if (!handle)
{
fprintf(stderr, "Unable to open device.\n");
return ;
} /* Check whether a kernel driver is attached to interface #0. If so, we'll
* need to detach it.
*/
if (libusb_kernel_driver_active(handle, ))
{
res = libusb_detach_kernel_driver(handle, );
if (res == )
{
kernelDriverDetached = ;
}
else
{
fprintf(stderr, "Error detaching kernel driver.\n");
return ;
}
} /* Claim interface #0. */
res = libusb_claim_interface(handle, );
if (res != )
{
fprintf(stderr, "Error claiming interface.\n");
return ;
} /* We can now send and receive messages. For example, set normal mode. */
memset(buffer, , );
buffer[] = ; /* CANCTRL = Normal mode. */
buffer[] = 0x02; /* SPI command = Write. */
buffer[] = 0x0f; /* Register = CANCTRL */
buffer[] = ; /* Data = 0 (normal mode) */ /* Send the message to endpoint 1 with a 100ms timeout. */
res = libusb_interrupt_transfer(handle, , buffer, , &numBytes, );
if (res == )
{
printf("%d bytes transmitted successfully.\n", numBytes);
}
else
{
fprintf(stderr, "Error sending message to device.\n");
} /* Listen for a message. Note that for a normal application you'll need
* to use asynchronous mode because we can't predict when messages will be
* available. This involves setting up a callback function to handle incoming
* messages - refer to libusb documentation. */ /* Wait up to 5 seconds for a message to arrive on endpoint 0x81. */
res = libusb_interrupt_transfer(handle, 0x81, buffer, , &numBytes, );
if ( == res)
{
if (numBytes == )
{
processMessage(buffer);
}
else
{
printf("Received %d bytes, expected 64.\n", numBytes);
}
}
else
{
fprintf(stderr, "Error receiving message.\n");
} /* Release interface #0. */
res = libusb_release_interface(handle, );
if ( != res)
{
fprintf(stderr, "Error releasing interface.\n");
} /* If we detached a kernel driver from interface #0 earlier, we'll now
* need to attach it again. */
if (kernelDriverDetached)
{
libusb_attach_kernel_driver(handle, );
} /* Shutdown libusb. */
libusb_exit(); return ;
} /*----------------------------------------------------------------------*/
void processMessage(const uint8_t* buffer)
{
unsigned index = ; /* Most significant bit set indicates a CAN message is present. */
while(buffer[index] & 0x80)
{
unsigned extendedID = buffer[index] & 0x20;
unsigned rtr = buffer[index] & 0x10;
unsigned dataLength = buffer[index] & 0x0f;
unsigned canID = ; ++index; if (extendedID) /* 29 bit identifier */
{
canID = buffer[index] << ;
++index;
canID |= (((buffer[index] & 0xe0 >> ) |
(buffer[index] & 0x03)) << );
++index;
canID |= (buffer[index] << );
++index;
canID |= (buffer[index]);
++index;
}
else /* standard 11 bit identifier */
{
canID = buffer[index] << ;
++index;
canID |= ((buffer[index] >> ) & );
++index;
} printf("CAN ID: 0x%x [%s] ", canID,
extendedID ? "extended" : "standard"); if (rtr)
{
printf("RTR\n");
}
else
{
unsigned i = ;
for (i = ; i < dataLength; ++i)
{
printf("0x%02x ", buffer[index]);
++index;
}
printf("\n");
}
} printf("CAN Status: 0x%02x\n", buffer[]);
printf("Transmit Errors: %u\n", buffer[]);
printf("Receive Errors: %u\n", buffer[]); /* If the command was read, we have received the result. */
if (buffer[] == 0x03)
{
printf("Read from register 0x%02x returned 0x%02x\n",
buffer[], buffer[]);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include </usr/local/include/libusb-1.0/libusb.h> #define BULK_EP_OUT 0x82
#define BULK_EP_IN 0x08 int interface_ref = ;
int alt_interface,interface_number; int print_configuration(struct libusb_device_handle *hDevice,struct libusb_config_descriptor *config)
{
char *data;
int index; data = (char *)malloc();
memset(data,,); index = config->iConfiguration; libusb_get_string_descriptor_ascii(hDevice,index,data,); printf("\nInterface Descriptors: ");
printf("\n\tNumber of Interfaces : %d",config->bNumInterfaces);
printf("\n\tLength : %d",config->bLength);
printf("\n\tDesc_Type : %d",config->bDescriptorType);
printf("\n\tConfig_index : %d",config->iConfiguration);
printf("\n\tTotal length : %lu",config->wTotalLength);
printf("\n\tConfiguration Value : %d",config->bConfigurationValue);
printf("\n\tConfiguration Attributes : %d",config->bmAttributes);
printf("\n\tMaxPower(mA) : %d\n",config->MaxPower); free(data);
data = NULL;
return ;
} struct libusb_endpoint_descriptor* active_config(struct libusb_device *dev,struct libusb_device_handle *handle)
{
struct libusb_device_handle *hDevice_req;
struct libusb_config_descriptor *config;
struct libusb_endpoint_descriptor *endpoint;
int altsetting_index,interface_index=,ret_active;
int i,ret_print; hDevice_req = handle; ret_active = libusb_get_active_config_descriptor(dev,&config);
ret_print = print_configuration(hDevice_req,config); for(interface_index=;interface_index<config->bNumInterfaces;interface_index++)
{
const struct libusb_interface *iface = &config->interface[interface_index];
for(altsetting_index=;altsetting_index<iface->num_altsetting;altsetting_index++)
{
const struct libusb_interface_descriptor *altsetting = &iface->altsetting[altsetting_index]; int endpoint_index;
for(endpoint_index=;endpoint_index<altsetting->bNumEndpoints;endpoint_index++)
{
const struct libusb_endpoint_desriptor *ep = &altsetting->endpoint[endpoint_index];
endpoint = ep;
alt_interface = altsetting->bAlternateSetting;
interface_number = altsetting->bInterfaceNumber;
} printf("\nEndPoint Descriptors: ");
printf("\n\tSize of EndPoint Descriptor : %d",endpoint->bLength);
printf("\n\tType of Descriptor : %d",endpoint->bDescriptorType);
printf("\n\tEndpoint Address : 0x0%x",endpoint->bEndpointAddress);
printf("\n\tMaximum Packet Size: %x",endpoint->wMaxPacketSize);
printf("\n\tAttributes applied to Endpoint: %d",endpoint->bmAttributes);
printf("\n\tInterval for Polling for data Tranfer : %d\n",endpoint->bInterval);
}
}
libusb_free_config_descriptor(NULL);
return endpoint;
} int main(void)
{
int r = ;
struct libusb_device **devs;
struct libusb_device_handle *handle = NULL, *hDevice_expected = NULL;
struct libusb_device *dev,*dev_expected; struct libusb_device_descriptor desc;
struct libusb_endpoint_descriptor *epdesc;
struct libusb_interface_descriptor *intdesc; ssize_t cnt;
int e = ,config2;
int i = ,index;
char str1[], str2[];
char found = ; // Init libusb
r = libusb_init(NULL);
if(r < )
{
printf("\nfailed to initialise libusb\n");
return ;
}
else
printf("\nInit Successful!\n"); // Get a list os USB devices
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < )
{
printf("\nThere are no USB devices on bus\n");
return -;
}
printf("\nDevice Count : %d\n-------------------------------\n",cnt); while ((dev = devs[i++]) != NULL)
{
r = libusb_get_device_descriptor(dev, &desc);
if (r < )
{
printf("failed to get device descriptor\n");
libusb_free_device_list(devs,);
libusb_close(handle);
break;
} e = libusb_open(dev,&handle);
if (e < )
{
printf("error opening device\n");
libusb_free_device_list(devs,);
libusb_close(handle);
break;
} printf("\nDevice Descriptors: ");
printf("\n\tVendor ID : %x",desc.idVendor);
printf("\n\tProduct ID : %x",desc.idProduct);
printf("\n\tSerial Number : %x",desc.iSerialNumber);
printf("\n\tSize of Device Descriptor : %d",desc.bLength);
printf("\n\tType of Descriptor : %d",desc.bDescriptorType);
printf("\n\tUSB Specification Release Number : %d",desc.bcdUSB);
printf("\n\tDevice Release Number : %d",desc.bcdDevice);
printf("\n\tDevice Class : %d",desc.bDeviceClass);
printf("\n\tDevice Sub-Class : %d",desc.bDeviceSubClass);
printf("\n\tDevice Protocol : %d",desc.bDeviceProtocol);
printf("\n\tMax. Packet Size : %d",desc.bMaxPacketSize0);
printf("\n\tNo. of Configuraions : %d\n",desc.bNumConfigurations); e = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, (unsigned char*) str1, sizeof(str1));
if (e < )
{
libusb_free_device_list(devs,);
libusb_close(handle);
break;
}
printf("\nManufactured : %s",str1); e = libusb_get_string_descriptor_ascii(handle, desc.iProduct, (unsigned char*) str2, sizeof(str2));
if(e < )
{
libusb_free_device_list(devs,);
libusb_close(handle);
break;
}
printf("\nProduct : %s",str2);
printf("\n----------------------------------------"); if(desc.idVendor == 0xffff && desc.idProduct == 0x4)
{
found = ;
break;
}
}//end of while
if(found == )
{
printf("\nDevice NOT found\n");
libusb_free_device_list(devs,);
libusb_close(handle);
return ;
}
else
{
printf("\nDevice found");
dev_expected = dev;
hDevice_expected = handle;
} e = libusb_get_configuration(handle,&config2);
if(e!=)
{
printf("\n***Error in libusb_get_configuration\n");
libusb_free_device_list(devs,);
libusb_close(handle);
return -;
}
printf("\nConfigured value : %d",config2); if(config2 != )
{
libusb_set_configuration(handle, );
if(e!=)
{
printf("Error in libusb_set_configuration\n");
libusb_free_device_list(devs,);
libusb_close(handle);
return -;
}
else
printf("\nDevice is in configured state!");
} libusb_free_device_list(devs, ); if(libusb_kernel_driver_active(handle, ) == )
{
printf("\nKernel Driver Active");
if(libusb_detach_kernel_driver(handle, ) == )
printf("\nKernel Driver Detached!");
else
{
printf("\nCouldn't detach kernel driver!\n");
libusb_free_device_list(devs,);
libusb_close(handle);
return -;
}
} e = libusb_claim_interface(handle, );
if(e < )
{
printf("\nCannot Claim Interface");
libusb_free_device_list(devs,);
libusb_close(handle);
return -;
}
else
printf("\nClaimed Interface\n"); active_config(dev_expected,hDevice_expected); // Communicate char *my_string, *my_string1;
int transferred = ;
int received = ;
int length = ; my_string = (char *)malloc(nbytes + );
my_string1 = (char *)malloc(nbytes + ); memset(my_string,'\0',);
memset(my_string1,'\0',); strcpy(my_string,"prasad divesd");
length = strlen(my_string); printf("\nTo be sent : %s",my_string); e = libusb_bulk_transfer(handle,BULK_EP_IN,my_string,length,&transferred,);
if(e == && transferred == length)
{
printf("\nWrite successful!");
printf("\nSent %d bytes with string: %s\n", transferred, my_string);
}
else
printf("\nError in write! e = %d and transferred = %d\n",e,transferred); sleep();
i = ; for(i = ; i < length; i++)
{
e = libusb_bulk_transfer(handle,BULK_EP_OUT,my_string1,,&received,); //64 : Max Packet Lenght
if(e == )
{
printf("\nReceived: ");
printf("%c",my_string1[i]); //will read a string from lcp2148
sleep();
}
else
{
printf("\nError in read! e = %d and received = %d\n",e,received);
return -;
}
} e = libusb_release_interface(handle, ); libusb_close(handle);
libusb_exit(NULL); printf("\n");
return ;
}
libusb示例的更多相关文章
- libusb 示例
#include <usb.h> #include <stdio.h> #define VERSION "0.1.0" #define VENDOR_ID ...
- 基于libUSB的USB设备固件更新程序(下载数据)(转)
源:基于libUSB的USB设备固件更新程序(下载数据) 本文紧接上一篇日志:基于libUSB-Win32的USB设备固件更新程序(前言),相关背景以及起因等,此处不再赘述,如感兴趣请移步. libU ...
- libusb: android上集成libusb库
1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...
- Ubuntu15下Qt+libusb开发
下载和安装libusb-1.0 在Ubuntu15中可以从软件仓库安装libusb,当前的libusb版本为1.0.可以使用如下命令安装libusb的全部内容. $sudo apt-get insta ...
- 树莓派Zero 2 W(ubuntu-22.04)通过.NET6和libusb操作USB读写
有这个想法的初衷 喜欢电子和DIY硬件的朋友对稚晖君应该都不陌生,他定期都会分享一些自己做的好玩的硬件,他之前做了一个ElectronBot桌面机器人我就很感兴趣,所以就自己也做了一个. 起初我只是自 ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
随机推荐
- Windows 上安装 Scala
在安装 Scala 之前需要先安装 Java 环境,具体安装的详细方法就不在这里描述了. 您可以自行搜索我们网站中的内容获得其他网站的帮助来获得如何安装 Java 环境的方法. 接下来,我们可以从 S ...
- python模块--time & datetime
time模块 #获取当前时间的时间戳 import time >>> time.time() 1535004894.0959966 #日期字符串转化成时间戳 >>> ...
- 3 爬虫解析 Xpath 和 BeautifulSoup
1.正则表达式 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [-] \D : 非数字 \w :数字.字母.下划线.中文 \W : 非\w ...
- stark 组件 url 二级分发的实现
模拟 admin 组件url设计思路 项目urls 文件中: from django.contrib import admin from django.urls import path from st ...
- Nim or not Nim? HDU - 3032
题意:给定n堆石子,两人轮流操作,每次选一堆石子,取任意石子或则将石子分成两个更小的堆(非0),取得最后一个石子的为胜. 题解:比较裸的SG定理,用sg定理打表,得到表1,2,4,3,5,6,8,7, ...
- 『TensorFlow』第十弹_队列&多线程_道路多坎坷
一.基本队列: 队列有两个基本操作,对应在tf中就是enqueue&dequeue tf.FIFOQueue(2,'int32') import tensorflow as tf '''FIF ...
- 【其他】【服务器】【4】删除Windows系统中不想要的服务
步骤: 1,开始菜单栏查找“服务”,打开后找到想要删除的服务 2,右键单击想要删除的服务,选择“属性”-“常规”-“服务名称”,记下服务名称(AA) 3,开始菜单栏输入“cmd”打开命令行窗口,输入s ...
- [洛谷 P3239] [HNOI2015]亚瑟王
[HNOI2015]亚瑟王 题目描述 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑.他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知, ...
- 在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true
Boolean 表达式 一个值为 true 或者 false 的表达式.如果需要,非 Boolean 表达式也可以被转换为 Boolean 值,但是要遵循下列规则: 所有的对象都被当作 true. 当 ...
- linux文件软链接、硬链接
在linux ext2文件系统中,一个文件的属性存放在inode中,而数据存放在block中.每个文件占用一个inode,inode中记录了文件的权限和block地址,通过inode可以定位到bloc ...