xyphro / UsbGpib

Versatile, cheap and portable USB to GPIB converter (USBTMC class based)
MIT License
288 stars 51 forks source link

unpack_from requires a buffer #68

Open paulwunner opened 1 week ago

paulwunner commented 1 week ago

I can't read out anything from the usbgpib adapter. When using the script from #55 without any arguments I get back an error which ends with:

File "/home/*user*/anaconda3/lib/python3.12/site-packages/usbtmc/usbtmc.py", line 462, in unpack_bulk_in_header msgid, btag, btaginverse = struct.unpack_from('BBBx', data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ struct.error: unpack_from requires a buffer of at least 4 bytes for unpacking 4 bytes at offset 0 (actual buffer size is 0) I can however communicate with the GPIB-Device (a Keithley 195A Multimeter) itself without this error.

I am trying this on Ubuntu24

Greetings

jimthouston commented 1 week ago

It is taking me some time to remember what I was doing in April. I have not played with usbgpib since then.

I just installed a Ubuntu 24.04.1 LTS and I got the usbgpib_conf.py script to work. I ran into different errors. Here are the install steps I used:

sudo apt install python3-pip
sudo apt-get install python3-full
python3 -m venv venv
source venv/bin/activate
pip3 install python-usbtmc
pip3 install PyUSB

Add /etc/udev/rules.d/9-usbtmc.rules:
SUBSYSTEM=="usb", ATTR{idVendor}=="03eb", ATTR{idProduct}=="2065", MODE="0660", GROUP="plugdev"
KERNEL=="usbtmc*", MODE="0660", GROUP="plugdev"

Running usbgpib_conf.py gave me the correct information and then crashed in libusb_open(). I fixed this by calling inst.close() before exiting the script.

Paul if you can detail how you installed I would try to reproduce and debug the problem you are seeing.

I also tried usbgpib_gui.py and have a new problem which I'm still chasing. It looks like something has the nicegui event handling.

Jim

paulwunner commented 1 week ago

Thanks for the answer Jim, I can't remember exactly how I installed python. However I am in a virtual environment, just like you, and have python-usbtmc and PyUSB installed. I even tried it in a fresh venv with just those 2 packages. I also added the rules. Unlike you, I still get the libusb_open() crash the first time I run the script, even though I added the inst.close() into the script.

Most of the time it crashes on the inst.ask() before even printing the !autoid?. However sometimes it prints the !autoid? and even prints the measurement result of my DMM. When this happens it crashes in the inst.pulse() function. So I can read something! Just not the right thing.

Here is the whole error message that pops up most of the time:

python3 usbgpib_conf.py
Usage: usbgpib_conf command
Traceback (most recent call last):
  File "/home/*user*/anaconda3/usbgpib_conf.py", line 10, in <module>
    print(cmd, inst.ask(cmd))
               ^^^^^^^^^^^^^
  File "/home/*user*/anaconda3/lib/python3.12/site-packages/usbtmc/usbtmc.py", line 630, in ask
    return self.read(num, encoding)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/*user*/anaconda3/lib/python3.12/site-packages/usbtmc/usbtmc.py", line 613, in read
    return self.read_raw(num).decode(encoding).rstrip('\r\n')
           ^^^^^^^^^^^^^^^^^^
  File "/home/*user*/anaconda3/lib/python3.12/site-packages/usbtmc/usbtmc.py", line 542, in read_raw
    msgid, btag, btaginverse, transfer_size, transfer_attributes, data = self.unpack_dev_dep_resp_header(resp) 
                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/*user*/anaconda3/lib/python3.12/site-packages/usbtmc/usbtmc.py", line 466, in unpack_dev_dep_resp_header
    msgid, btag, btaginverse = self.unpack_bulk_in_header(data)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/*user*/anaconda3/lib/python3.12/site-packages/usbtmc/usbtmc.py", line 462, in unpack_bulk_in_header
    msgid, btag, btaginverse = struct.unpack_from('BBBx', data)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
struct.error: unpack_from requires a buffer of at least 4 bytes for unpacking 4 bytes at offset 0 (actual buffer size is 0)
libusb: debug [libusb_open] open 1.14
Speicherzugriffsfehler (Speicherabzug geschrieben)

I'm not really sure how to further describe my situation.

jimthouston commented 1 week ago

Hi Paul,

Here is the combination which is working for me: Ubuntu 24.04.1 LTS python 3.12.3 pyusb 1.2.1 usbtmc 0.8 usbgpib firmware V1.6

I see that there is a V1.7 FW which I have not tried.

Jim

jimthouston commented 1 week ago

I installed the V1.7 FW and I'm still unable to reproduce the problem. All I have been testing is running the usbgpib_conf.py.

Paul are you seeing this problem first time every time or might it require accessing the attached gpib device?

paulwunner commented 1 week ago

Hey Jim, I guess I have the V1.7 FW as since my programming no change happened on Github. The problem happens every time. I'm entirely not able to read out anything from the adapter.

jimthouston commented 1 week ago

Hi Paul,

I see from the errors that you are using Anaconda as your python package manager. I have not used it but it looks like it has magic to export an environment to allow reproducing problems.

If you would do this I will try to reproduce the problem. I think activating your environment and running this command will create the needed file:

conda env export > environment.yml

I have memories of github refusing to let me attach a file. If it doesn't like the .yml file try compressing it with gzip.

Also am I correct in assuming that you are running Ubuntu 24.04.1 LTS on an X86_64 PC?

Jim

paulwunner commented 2 days ago

Hey Jim, sorry for the delay, I'm somewhat busy. Here is my environment that you asked for. environment.zip

Yes I am running Ubuntu 24.04.1 LTS on a X86_64 machine.

jimthouston commented 2 days ago

Hi Paul,

I tried your Anaconda environment.yml and everything is still working correctly here. I still plan to double check some revisions to see if conda has done what I expect.

I'm trying to imagine anything that we might be doing differently. I only have one UsbGpib and I suspect that there might be confusion if there was more than one connected.

Do you have any other software running which might be using the UsbGpib.

You might be able to figure out what is going on by using Wireshark to capture a USB trace.

jimthouston commented 1 day ago

Hi Paul,

An obvious difference might be the connected GPIB device. Could you try a different GPIB device? What make/model is the DMM you are using?

There could be a race between the inst.pulse command and the inst.ask() and something happening on the GPIB bus. I'm wonder if your DMM is setup to periodically send the value?

I have been playing with the usbgpib_gui.py script. I was trying to read back the configuration option I had changed and getting USB EPIPE errors. I found that the error went away when I changed an inst.write() to an instr.ask(). I had been using the write when I was sending a command where I didn't expect a reply. When I used the inst,ask() the response was the voltage value from my HP3478A meter.