Skip to content

Commit

Permalink
added code to handle rigol quirks in read_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyherbert committed Jul 27, 2015
1 parent a4c8970 commit b19ef5d
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions usbtmc/usbtmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
USB488_GOTO_LOCAL = 161
USB488_LOCAL_LOCKOUT = 162

RIGOL_QUIRK_PIDS = [0x04ce, 0x0588]

def parse_visa_resource_string(resource_string):
# valid resource strings:
# USB::1234::5678::INSTR
Expand Down Expand Up @@ -165,6 +167,8 @@ def __init__(self, *args, **kwargs):
self.term_char = None
self.advantest_quirk = False
self.advantest_locked = False
self.rigol_quirk = False
self.rigol_quirk_ieee_block = False

self.bcdUSBTMC = 0
self.support_pulse = False
Expand Down Expand Up @@ -276,6 +280,12 @@ def __init__(self, *args, **kwargs):
self.max_recv_size = 63
self.advantest_quirk = True

if self.device.idVendor == 0x1ab1 and self.device.idProduct in RIGOL_QUIRK_PIDS:
self.rigol_quirk = True

if self.device.idProduct == 0x04ce:
self.rigol_quirk_ieee_block = True

# find endpoints
for ep in self.iface:
ep_at = ep.bmAttributes
Expand Down Expand Up @@ -431,16 +441,45 @@ def read_raw(self, num=-1):
read_data = b''

while not eom:
req = self.pack_dev_dep_msg_in_header(read_len, term_char)
self.bulk_out_ep.write(req)
if not self.rigol_quirk or read_data == b'':

# if the rigol sees this again, it will restart the transfer
# so only send it the first time

req = self.pack_dev_dep_msg_in_header(read_len, term_char)
self.bulk_out_ep.write(req)

resp = self.bulk_in_ep.read(read_len+12, timeout = self.timeout)

msgid, btag, btaginverse, transfer_size, transfer_attributes, data = self.unpack_dev_dep_resp_header(resp.tostring())

eom = transfer_attributes & 1

read_data += data
if self.rigol_quirk and read_data:
pass # do nothing, the packet has no header if it isn't the first
else:
msgid, btag, btaginverse, transfer_size, transfer_attributes, data = self.unpack_dev_dep_resp_header(resp.tostring())


if self.rigol_quirk:
# rigol devices only send the header in the first packet, and they lie about whether the transaction is complete
if read_data:
read_data += resp.tostring()
else:
if self.rigol_quirk_ieee_block and data.startswith(b"#"):

# ieee block incoming, the transfer_size usbtmc header is lying about the transaction size
l = int(chr(data[1]))
n = int(data[2:l+2])

transfer_size = n + (l+2) # account for ieee header

read_data += data

if len(read_data) >= transfer_size:
read_data = read_data[:transfer_size] # as per usbtmc spec section 3.2 note 2
eom = True
else:
eom = False
else:
eom = transfer_attributes & 1
read_data += data

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

0 comments on commit b19ef5d

Please sign in to comment.