日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python读取usb数据_PyUSB:从USB设备读取

發布時間:2023/12/8 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取usb数据_PyUSB:从USB设备读取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是一個更新和縮短的問題。

通過PyUSB與USB設備進行通訊應該很容易。因此,我試圖在Win10下使用PyUSB從USB設備(示波器)讀取數據。顯然,由于找到了設備,因此USB驅動程序(libusb-win32 v1.2.6.0)正確安裝了,并且我得到了一些響應print(dev)(請參見下文)。從中我可以看到輸出端點地址是0x3和輸入端點地址是0x81

根據示波器手冊,我應該發送:SDSLSCPI#到設備以將其設置為SCPI模式,并應獲得響應“:SCPION”。但是,:SDSLSCPI#以可復制的方式發送示波器的監視器時,將凍結并重新啟動。

如果我發送,*IDN?我應該得到答復,P1337,1842237,V2.4.0->。但僅當設備已經處于SCPI模式時。顯然不是,并且我收到超時錯誤(請參見下文)。

那么,我在這里做錯了什么?我在PyUSB教程中缺少什么信息。我使用的是錯誤的PyUSB命令/參數,還是缺少其他驅動程序,還是關于Win10或設備硬件?感謝您提供有關如何找出問題所在的提示。

順便說一句,第二個值是dev.read(0x81,7)多少?讀取的字節數?好吧,通常我不知道設備將發送多少字節。我期待命令在超時時間內讀取直到換行或其他終止符。在哪里可以找到有關PyUSB的“萬無一失”的文檔,教程和示例?

碼:

importusb.coreimportusb.util

dev=usb.core.find(idVendor=0x5345,idProduct=0x1234)ifdevisNone:raiseValueError('Device is not found')# device is found :-)print(dev)dev.set_configuration()msg=':SDSLSCPI#'print("Write:",msg,dev.write(3,msg))print("Read:",dev.read(0x81,7))

來自的輸出print(dev):

DEVICE ID5345:1234onBus000Address001=================bLength:0x12(18bytes)bDescriptorType:0x1DevicebcdUSB:0x200USB2.0bDeviceClass:0x0Specifiedat interface

bDeviceSubClass:0x0bDeviceProtocol:0x0bMaxPacketSize0:0x40(64bytes)idVendor:0x5345idProduct:0x1234bcdDevice:0x294Device2.94iManufacturer:0x1SystemCPU

iProduct:0x2OscilloscopeiSerialNumber:0x3SERIAL

bNumConfigurations:0x1CONFIGURATION1:500mA==================================bLength:0x9(9bytes)bDescriptorType:0x2ConfigurationwTotalLength:0x20(32bytes)bNumInterfaces:0x1bConfigurationValue:0x1iConfiguration:0x5BulkDataConfigurationbmAttributes:0xc0SelfPoweredbMaxPower:0xfa(500mA)INTERFACE0:Physical==================================bLength:0x9(9bytes)bDescriptorType:0x4InterfacebInterfaceNumber:0x0bAlternateSetting:0x0bNumEndpoints:0x2bInterfaceClass:0x5PhysicalbInterfaceSubClass:0x6bInterfaceProtocol:0x50iInterface:0x4BulkDataInterfaceENDPOINT0x81:BulkIN===============================bLength:0x7(7bytes)bDescriptorType:0x5EndpointbEndpointAddress:0x81IN

bmAttributes:0x2BulkwMaxPacketSize:0x200(512bytes)bInterval:0x0ENDPOINT0x3:BulkOUT===============================bLength:0x7(7bytes)bDescriptorType:0x5EndpointbEndpointAddress:0x3OUT

bmAttributes:0x2BulkwMaxPacketSize:0x200(512bytes)bInterval:0x0

錯誤信息:

Traceback(most recent call last):File"Osci.py",line15,inprint("Read:",dev.read(0x81,7))File"C:\Users\Test\Programs\Python3.7.4\lib\site-packages\usb\core.py",line988,inread

self.__get_timeout(timeout))File"C:\Users\Test\Programs\Python3.7.4\lib\site-packages\usb\backend\libusb0.py",line542,inbulk_read

timeout)File"C:\Users\Test\Programs\Python3.7.4\lib\site-packages\usb\backend\libusb0.py",line627,in__read

timeoutFile"C:\Users\Test\Programs\Python3.7.4\lib\site-packages\usb\backend\libusb0.py",line431,in_checkraiseUSBError(errmsg,ret)usb.core.USBError:[ErrnoNone]b'libusb0-dll:err [_usb_reap_async] timeout error\n'

更新:

我從供應商那里得到了答復。并且他確認發送命令時,示波器(或至少是該特定系列)崩潰了:SDSLSCPI#。他將與開發商聯系,開發商將在下周返回。好的,到目前為止,我似乎沒有機會讓它與該特定設備和可用的文檔一起運行:-(。

解決方案

I guess there was no chance to answer this question unless somebody already went through the very same problems.

I'm sorry for all of you (@Alex P., @Turbo J, @igrinis, @2xB) who took your time to make suggestions to help.

My findings: (I hope they will be useful to others):

Everything seems to be OK with PyUSB.

the vendor has provided outdated and wrong documentation. I hope very much that they will soon update the documentation on their homepage.

Sending the command :SDSLSCPI# is not necessary to enter SCPI-mode (but actually leads to a crash/restart)

For example: :CHAN1:SCAL 10v is wrong, it has to be :CH1:SCALe 10v (commands apparenty can't be abbreviated, although mentioned in the documentation that :CH1:SCAL 10v should also work.)

the essential command to get data :DATA:WAVE:SCREen:CH1? was missing in the manual.

The way it is working for me (so far):

The following would have been the minimal code I expected from the vendor/manufacturer. But instead I wasted a lot of time debugging their documentation.

However, still some strange things are going on, e.g. it seems you get data only if you ask for the header beforehand. But, well, this is not the topic of the original question.

Code:

### read data from a Peaktech 1337 Oscilloscope (OWON)importusb.coreimportusb.util

dev=usb.core.find(idVendor=0x5345,idProduct=0x1234)ifdevisNone:raiseValueError('Device not found')else:print(dev)dev.set_configuration()defsend(cmd):# address taken from results of print(dev): ENDPOINT 0x3: Bulk OUTdev.write(3,cmd)# address taken from results of print(dev): ENDPOINT 0x81: Bulk INresult=(dev.read(0x81,100000,1000))returnresultdefget_id():returnsend('*IDN?').tobytes().decode('utf-8')defget_data(ch):# first 4 bytes indicate the number of data bytes followingrawdata=send(':DATA:WAVE:SCREen:CH{}?'.format(ch))data=[]foridxinrange(4,len(rawdata),2):# take 2 bytes and convert them to signed integer using "little-endian"point=int().from_bytes([rawdata[idx],rawdata[idx+1]],'little',signed=True)data.append(point/4096)# data as 12 bitreturndatadefget_header():# first 4 bytes indicate the number of data bytes followingheader=send(':DATA:WAVE:SCREen:HEAD?')header=header[4:].tobytes().decode('utf-8')returnheaderdefsave_data(ffname,data):f=open(ffname,'w')f.write('\n'.join(map(str,data)))f.close()print(get_id())header=get_header()data=get_data(1)save_data('Osci.dat',data)### end of code

Result: (using gnuplot)

總結

以上是生活随笔為你收集整理的python读取usb数据_PyUSB:从USB设备读取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。