python-ivi / python-usbtmc

Provides a USBTMC driver for controlling instruments over USB
MIT License
162 stars 70 forks source link

How can I destroy the connection? #19

Closed israelcass closed 8 years ago

israelcass commented 9 years ago

There's a way to destroy the object and close the communication ? I need to communicate with others devices that use another protocol with serial/usb and this protocol is in conflict with another.

alexforencich commented 8 years ago

Should be fixed in the latest version of my fork. Let me know if it works for you. The connection can be cleaned up either by calling the close method or by triggering the destructor.

alexforencich commented 8 years ago

I also added some more updates to restore the configuration and attached kernel drivers when disconnected. Test it out on my fork and let me know if it works.

alexforencich commented 8 years ago

This should be fixed in the latest version.

SimplicityGuy commented 8 years ago

I moved my code over to using a call to open() and then close(), however if I try running the code twice, after opening the connection the second time, I get:

Traceback (most recent call last):
  File "/home/rwlodarc/Code/scratch-projects/powermeter/usbtmc_test.py", line 44, in <module>
    results = rsnrp.ask(cmd['command'])
  File "/usr/local/lib/python2.7/dist-packages/usbtmc/usbtmc.py", line 576, in ask
    return self.read(num, encoding)
  File "/usr/local/lib/python2.7/dist-packages/usbtmc/usbtmc.py", line 559, in read
    return self.read_raw(num).decode(encoding).rstrip('\r\n')
  File "/usr/local/lib/python2.7/dist-packages/usbtmc/usbtmc.py", line 508, in read_raw
    resp = self.bulk_in_ep.read(read_len+USBTMC_HEADER_SIZE+3, timeout = self.timeout)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 364, in read
    return self.device.read(self, size_or_buffer, timeout)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 918, in read
    self.__get_timeout(timeout))
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 769, in bulk_read
    timeout)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 872, in __read
    _check(retval)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 552, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 110] Operation timed out

FWIW, the python script is invoked twice, and open() and close() are invoked at the start and end of the script.

SimplicityGuy commented 8 years ago

Ah, interesting... I swapped setting the timeout to before the call to open() and this solved the issue:

rsnrp = usbtmc.Instrument("USB::0x0aad::0x00e2::INSTR")
rsnrp.timeout = 10000L
rsnrp.open()
alexforencich commented 8 years ago

You shouldn't have to call open and close explicitly. Open will be called on the first access, unless you want to open the connection earlier for some reason. Close will be called automatically in the destructor, unless you need to release the device before the script terminates or the usbtmc object is not garbage collected.