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或设备硬件?感谢您提供有关如何出问题所在的提⽰。
顺便说⼀句,第⼆个值是ad(0x81,7)多少?读取的字节数?好吧,通常我不知道设备将发送多少字节。我期待命令在超时时间内读
取直到换⾏或其他终⽌符。在哪⾥可以到有关PyUSB的“万⽆⼀失”的⽂档,教程和⽰例?
码:
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:",ad(0x81,7))
来⾃的输出print(dev):
DEVICE
ID5345:1234onBus000Address001=================bLength:0x12(18bytes)bDescriptorType:0x1DevicebcdUSB:0x200USB interface
bDeviceSubClass:0x0bDeviceProtocol:0x0bMaxPacketSize0:0x40(64bytes)idVendor:0x5345idProduct:0x1234bcdDevice:0x2
iProduct:0x2OscilloscopeiSerialNumber:0x3SERIAL
bNumConfigurations:0x1CONFIGURATION1:500mA==================================bLength:0x9(9bytes)bDesc
bmAttributes:0x2BulkwMaxPacketSize:0x200(512bytes)bInterval:0x0ENDPOINT0x3:BulkOUT========================
bmAttributes:0x2BulkwMaxPacketSize:0x200(512bytes)bInterval:0x0
错误信息:
Traceback(most recent call
last):File"Osci.py",line15,inprint("Read:",ad(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,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 (importusb.util
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=
(ad(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-
python怎么读取串口数据8')returnheaderdefsave_data(ffname,data):f=open(ffname,'w')f.write('\n'.join(map(str,data)))f.close()print(get_id())header=get_heade end of code
Result: (using gnuplot)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论