Skip to content

Commit bfc3c4c

Browse files
committed
Merge and resolve conflicts
2 parents 712a818 + b19ef5d commit bfc3c4c

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

usbtmc/usbtmc.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070

7171
USBTMC_HEADER_SIZE = 12
7272

73+
RIGOL_QUIRK_PIDS = [0x04ce, 0x0588]
74+
7375

7476
def parse_visa_resource_string(resource_string):
7577
# valid resource strings:
@@ -173,6 +175,8 @@ def __init__(self, *args, **kwargs):
173175
self.term_char = None
174176
self.advantest_quirk = False
175177
self.advantest_locked = False
178+
self.rigol_quirk = False
179+
self.rigol_quirk_ieee_block = False
176180

177181
self.bcdUSBTMC = 0
178182
self.support_pulse = False
@@ -326,6 +330,12 @@ def open(self):
326330
self.max_transfer_size = 63
327331
self.advantest_quirk = True
328332

333+
if self.device.idVendor == 0x1ab1 and self.device.idProduct in RIGOL_QUIRK_PIDS:
334+
self.rigol_quirk = True
335+
336+
if self.device.idProduct == 0x04ce:
337+
self.rigol_quirk_ieee_block = True
338+
329339
self.connected = True
330340

331341
self.clear()
@@ -495,21 +505,50 @@ def read_raw(self, num=-1):
495505
read_data = b''
496506

497507
while not eom:
498-
req = self.pack_dev_dep_msg_in_header(read_len, term_char)
499-
self.bulk_out_ep.write(req)
508+
if not self.rigol_quirk or read_data == b'':
509+
510+
# if the rigol sees this again, it will restart the transfer
511+
# so only send it the first time
500512

513+
req = self.pack_dev_dep_msg_in_header(read_len, term_char)
514+
self.bulk_out_ep.write(req)
515+
501516
resp = self.bulk_in_ep.read(read_len+USBTMC_HEADER_SIZE+3, timeout = int(self.timeout*1000))
502517

503518
if sys.version_info >= (3, 0):
504519
resp = resp.tobytes()
505520
else:
506521
resp = resp.tostring()
522+
523+
if self.rigol_quirk and read_data:
524+
pass # do nothing, the packet has no header if it isn't the first
525+
else:
526+
msgid, btag, btaginverse, transfer_size, transfer_attributes, data = self.unpack_dev_dep_resp_header(resp)
507527

508-
msgid, btag, btaginverse, transfer_size, transfer_attributes, data = self.unpack_dev_dep_resp_header(resp)
509528

510-
eom = transfer_attributes & 1
529+
if self.rigol_quirk:
530+
# rigol devices only send the header in the first packet, and they lie about whether the transaction is complete
531+
if read_data:
532+
read_data += resp
533+
else:
534+
if self.rigol_quirk_ieee_block and data.startswith(b"#"):
535+
536+
# ieee block incoming, the transfer_size usbtmc header is lying about the transaction size
537+
l = int(chr(data[1]))
538+
n = int(data[2:l+2])
539+
540+
transfer_size = n + (l+2) # account for ieee header
511541

512-
read_data += data
542+
read_data += data
543+
544+
if len(read_data) >= transfer_size:
545+
read_data = read_data[:transfer_size] # as per usbtmc spec section 3.2 note 2
546+
eom = True
547+
else:
548+
eom = False
549+
else:
550+
eom = transfer_attributes & 1
551+
read_data += data
513552

514553
# Advantest devices never signal EOI and may only send one read packet
515554
if self.advantest_quirk:

0 commit comments

Comments
 (0)