完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
问题描述 : 使用stm32f1作为usbdevice 然后只开启了一个winusb接口。 list_device device type ref count -------- -------------------- ---------- rtc RTC 0 winusb Miscellaneous Device 1 usbd USB Slave Device 0 可以看到生成出了usbd和winusb设备。 然后我写代码打开设备,进行读写,并使用python写了上位机测试代码。 python上位机可以收到stm32发过来的数据,但是python写给stm32的数据却一直没有收到 python上位机代码 from usb.backend import libusb1 import usb.core import usb.util # USB_ID_VENDOR = 0x1234 USB_ID_PRODUCT = 0x5678 #print(usb.core.show_devices()) #usb_dev = usb.core.find(find_all=True,idVendor=USB_ID_VENDOR,idProduct=USB_ID_PRODUCT,backend=libusb1.get_backend() ) #for dev in usb_dev : # print(dev) usb_dev = usb.core.find(idVendor=USB_ID_VENDOR,idProduct=USB_ID_PRODUCT,backend=libusb1.get_backend() ) print(usb_dev) usb_dev.write(0x01,"1") while True: data = usb_dev.read(0x81, 64,-1) print(data) usb_dev.write(0x01,data) rtthread代码 #include "stdint.h" #include "stdio.h" #include "string.h" #include "rtthread.h" #include "rtdevice.h" #define DBG_TAG "usb1" #define DBG_LVL DBG_LOG #include #define USB_DEVICE_NAME "winusb" static rt_device_t usb1_dev; /* usb 设备句柄 */ static struct rt_semaphore usb1_rx_sem; /* 用于接收消息的信号量 */ /* 接收数据回调函数 */ static rt_err_t usb1_rx_call(rt_device_t dev, rt_size_t size) { /* USB 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */ rt_kprintf("usb1 rx call :%dn",size); rt_sem_release(&usb1_rx_sem); return RT_EOK; } static rt_err_t usb1_tx_done(rt_device_t dev,void *buffer) { rt_kprintf("usb1 tx done :%pn",buffer); return RT_EOK; } void usb1_send_0x123(void) { char *text = "123"; int size = rt_device_write(usb1_dev, 0,text, 3); if (size < 0) { rt_kprintf("usb1 dev write data failed rc:%dn",size); } } static void usb1_thread(void *param) { int rc = 0; usb1_dev = rt_device_find(USB_DEVICE_NAME); if (!usb1_dev) { rt_kprintf("find %s failed!n", USB_DEVICE_NAME); return ; }else { rt_kprintf("find %s ok!n", USB_DEVICE_NAME); } rt_sem_init(&usb1_rx_sem, "usb1_rx_sem", 0, RT_IPC_FLAG_FIFO); /* 打开 USB 设备 */ rc = rt_device_open(usb1_dev, 0); RT_ASSERT(rc == RT_EOK); /* 设置回调函数 */ rt_device_set_rx_indicate(usb1_dev, usb1_rx_call); rt_device_set_tx_complete(usb1_dev, usb1_tx_done); uint8_t temp_buff[32] = {0}; while(1) { /* 循环读取数据,直到读取数据小于0表示缓存为空了 */ do{ rt_memset(temp_buff, 0, 32); rc = rt_device_read(usb1_dev, 0, temp_buff, 32); if(rc > 0) { rt_kprintf("usb read:%dn",rc); for(int i=0; i < rc; i++) { rt_kprintf("0x%02X ",temp_buff); } rt_kprintf("n"); } }while(rc <= 0); rt_kprintf("usb read waitn"); /* 阻塞等待接收信号量 */ rt_sem_take(&usb1_rx_sem, RT_WAITING_FOREVER); rt_kprintf("usb readn"); } } int usb1_test_init(void) { static int usb1_init = 0; rt_thread_t tid; if(usb1_init > 0) { return 0; } usb1_init = 1; tid = rt_thread_create("usb1",usb1_thread, NULL,1024,20, 10); if (tid != RT_NULL) rt_thread_startup(tid); return 0; } void usb1_test(uint8_t argc, char **argv) { rt_kprintf("usb1 testn"); usb1_send_0x123(); } MSH_CMD_EXPORT(usb1_test, usb1 test); void usb1_debug(uint8_t argc, char **argv) { if(argc > 1) { int val = atoi(argv[1]); rt_kprintf("usb1 debug set %dn",val); } } MSH_CMD_EXPORT(usb1_debug, usb1 debug); 配置 |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
683 浏览 0 评论
AI模型部署边缘设备的奇妙之旅:如何在边缘端部署OpenCV
2521 浏览 0 评论
tms320280021 adc采样波形,为什么adc采样频率上来波形就不好了?
1314 浏览 0 评论
1915 浏览 0 评论
1494 浏览 0 评论
74906 浏览 21 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 08:51 , Processed in 0.605142 second(s), Total 67, Slave 50 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号