本帖最后由 6091820503 于 2014-7-29 13:58 编辑
最近在学习libusb-win32,因此拿STM32F4写了个下位机,然后和上位机一起进行测试
这个是HID的测试程序,上位机和下位机都测试通过
通信测试.png (22.98 KB, 下载次数: 0)
下载附件
通信测试通过
2014-7-29 13:55 上传
libusb-win32的安装调试过程,后面会补充
HID_pc_demo.rar
(5.01 MB, 下载次数: 388)
2014-7-29 13:54 上传
点击文件名下载附件
HID_PC_DEMO
HID_demo.rar
(46.25 KB, 下载次数: 291)
2014-7-29 13:54 上传
点击文件名下载附件
HID_STM32_DEMO
//STM32主程序,非常的简答
- int main(void)
- {
- uint8_t buf[16] = {0,1,2,3};
- RCC_ClocksTypeDef RCC_Clocks;
-
- /* SysTick end of count event each 10ms */
- RCC_GetClocksFreq(&RCC_Clocks);
- SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
- //ÅäÖÃUSB HID
- USBD_Init(&USB_OTG_dev,
- USB_OTG_FS_CORE_ID,
- &USR_desc,
- &USBD_HID_cb,
- &USR_cb);
-
- while(1)
- {
- Delay(1);
- buf[0]++;
- USBD_HID_SendReport (&USB_OTG_dev, buf, 4);
- }
-
- }
复制代码
//PC端程序 也是非常的简单
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include "conio.h"
- #include "lusb0_usb.h"
- #define m_dev_VENDOR 0x0483 /* Vendor ID of the m_dev */
- #define m_dev_PRODUCT 0x5710 /* Product ID of the m_dev */
- #define m_dev_DATA_SIZE 4 /* Number of bytes to get at once */
- int main(int argc, char *argv[])
- {
- int ret, i;
- char data[m_dev_DATA_SIZE] = {0};
- struct usb_bus *bus;
- struct usb_device * m_dev = NULL;
- struct usb_dev_handle *m_dev_handle;
- char str[64];
- usb_init();
- ret = usb_find_busses();
- if(ret<0)
- {
- printf("usb_find_busses failed - returned: %i
", ret);
- return ret;
- }
- ret = usb_find_devices();
- if(ret<0)
- {
- printf("usb_find_devices failed - returned: %i
", ret);
- return ret;
- }
-
- for(bus = usb_get_busses(); bus; bus = bus->next)
- {
- struct usb_device *dev;
- for(dev = bus->devices; dev; dev = dev->next)
- {
- if(dev->descriptor.idVendor == m_dev_VENDOR && dev->descriptor.idProduct == m_dev_PRODUCT)
- m_dev=dev;
- }
- }
- if(!m_dev)
- {
- printf("m_dev not found
");
- return 1;
- }
-
- m_dev_handle = usb_open(m_dev);
- if(!m_dev_handle)
- {
- printf("Could not open m_dev
");
- return 1;
- }
-
- printf("m_dev opened: %s
", m_dev->filename);
- printf("Device Protocol : %d
",m_dev->descriptor.bDeviceProtocol);
- printf("Report Length : %d
",m_dev->descriptor.bLength);
- printf("Decriptor Type : %d
",m_dev->descriptor.bDescriptorType);
- printf("End Points : %d
",m_dev->config->interface->altsetting->bNumEndpoints);
- printf("Interface Class : %d
",m_dev->config->interface->altsetting->bInterfaceClass);
- printf("Protocol : %d
",m_dev->config->interface->altsetting->bInterfaceProtocol);
- printf("Interface Number: %d
",m_dev->config->interface->altsetting->bInterfaceNumber);
- printf("Device Filename : %s
",m_dev->filename);
-
- usb_get_string_simple(m_dev_handle, m_dev->descriptor.iManufacturer, str, sizeof(str));
- printf("Device Manfucaturer : %s
",str);
- usb_get_string_simple(m_dev_handle,m_dev->descriptor.iProduct,str,sizeof(str));
- printf("Product Name : %s
",str);
- usb_get_string_simple(m_dev_handle,m_dev->descriptor.iSerialNumber,str,sizeof(str));
- printf("Device Serial Number: %s
",str);
- printf("End point addresses : 0x%x
",m_dev->config->interface->altsetting->endpoint->bEndpointAddress);
- //return 0;
- if(usb_set_configuration(m_dev_handle, 1) < 0)
- {
- printf("Could not set configuration
");
- usb_close(m_dev_handle);
- return 1;
- }
- if(usb_claim_interface(m_dev_handle, 0) < 0) //claim_interface 0指向第一个设备
- {
- printf("Could not claim interface
");
- usb_close(m_dev_handle);
- return 1;
- }
-
- while(1)
- {
- ret = usb_interrupt_read(m_dev_handle, 0x81, data, m_dev_DATA_SIZE, 0);
- //ret = usb_bulk_read(m_dev_handle, 0x81, data, m_dev_DATA_SIZE, 0);
- printf("ret: %i: ", ret);
- for(i = 0; i< ret; i++)
- {
- printf("%hhd ", (unsigned char)(data[i]));
- }
- printf("
");
- }
-
- usb_release_interface(m_dev_handle, 0);
- usb_close(m_dev_handle);
- printf("m_dev closed
");
-
- return 0;
- }
复制代码
VS2010
但是我觉得QT应该也是支持的
一周热门 更多>