asvela / tektronix-func-gen

Python control of Tektronix AFG1000 and AFG3000 (possibly also other) arbitrary function generators through PyVISA
https://asvela.github.io/tektronix-func-gen/
MIT License
16 stars 6 forks source link

[PyVISA >v1.11 compatibility issues] module write() function expects tuple from pyvisa write(), gets int; custom waveform transfer issue #2

Closed laurenwilliams22 closed 3 years ago

laurenwilliams22 commented 3 years ago

I just copied one of the basic examples in the read me file (pasted below for reference). When I run it, I get errors in lines 117 and lines 200 in the tektronix_func_gen.py file. The error is: TypeError: cannot unpack non-iterable int object. Do you know why this might be happening? I have pyvisa, NI VISA, and numpy installed.

import tektronix_func_gen as tfg

with tfg.FuncGen('GPIB0::11::INSTR') as fgen:
      fgen.ch1.set_function("SIN")
      fgen.ch1.set_frequency(25, unit="Hz")
      fgen.ch1.set_offset(50, unit="mV")
      fgen.ch1.set_amplitude(0.002)
      fgen.ch1.set_output("ON")
      fgen.ch2.set_output("OFF")
      # alternatively fgen.ch1.print_settings() to show from one channel only
      fgen.print_settings()
asvela commented 3 years ago

Hi Lauren,

Thanks for letting me know! This is not something I have experience before. Just to check, I noticed that in your example code, there are no tabs in front of the lines after the with statement, is this a paste error or do you not have tabs in your code?

As it is line 117 in _tektronix_funcgen.py that's prompting the error at 200, it happens in the initialisation of the device. The error at line 200 suggests that pyvisa is not returning both the number of bytes and the status code. If you temporarily change line 200 to be

temp = self.inst.write(command)

and then insert the lines

print(type(temp))
print(temp)

right after line 200, and run the programme again, what does the terminal show?

laurenwilliams22 commented 3 years ago

Thank you for your quick reply! I did have the tabs in front of the lines after the with statement, it was a paste error. I inserted those lines and got: <class 'int'> 6

asvela commented 3 years ago

Okay, I will have to investigate this as it seems the output from the pyvisa library's write() is not as it used to be (now only number of bytes vs previously both number the of bytes and the statuscode), but looking through the change logs I can't find anything about it being changed.

However, as a temporary fix, I hope you should be able to run it if you change the write() in tektronix_func_gen.py (lines 200-210) function to be:

num_bytes= self.inst.write(command)
return num_bytes, None

Let me know if that solves it for now.

laurenwilliams22 commented 3 years ago

Thank you so much for your help. I made the changes you suggested and also included the override compatibility as shown below. It works with these changes. Thanks again!

with tfg.FuncGen('GPIB0::11::INSTR', override_compatibility=True) as fgen:

asvela commented 3 years ago

Good to hear!

I had a look and discovered that pyvisa has changed a bit with its release of v1.11, I assume you are using that one? (You can check that by executing pip list or pyvisa-info in the terminal/command prompt, sorry if I am explaining things you know well, I just find it frustrating when people ask for info but don't tell you how to obtain it.)

I have made some changes to the module now to accommodate for this new pyvisa version. Could you please try using the code on the branch fix/issue2?

PS: Out of curiosity, what model of the AFG are you using? The override_compatibility is needed because you are not using AFG1022, but be warned, the code will assume the voltage/frequency limits of the AFG1022:

"frequency lims": ({"min": 1e-6, "max": 25e6}, "Hz"),
"voltage lims":   ({"50ohm": {"min": -5, "max": 5},
                    "highZ": {"min": -10, "max": 10}}, "V"),
"amplitude lims": ({"50ohm": {"min": 0.001, "max": 10},
                    "highZ": {"min": 0.002, "max": 20}}, "Vpp")}

so if your instrument has larger/smaller limits you will not be able to access the full range/might run into errors.

laurenwilliams22 commented 3 years ago

Thank you so much for updating the code. I am using v1.11 (I am actually quite a novice at this stuff so honestly the more info the better!). I am using the AFG31102, thanks for the warning about the limits. The new code works perfectly for the original example I was using. When I tried copying the arbitrary waveform example, I ran into pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed. I am using NI VISA. There still seems to be some issue with pyvisa. I have pasted the entire error message below. I will keep trying to figure this out. Thanks again for all your help.

error
asvela commented 3 years ago

The line in question here was due to a funny problem with previous versions of PyVISA, where an empty query was needed after a write_binary_values(). However, it seems v1.11 solved this and there is no need for the line anymore.

Could you please try commenting out line 479 in your tektronix_func_gen2.py (saw you had named it with that extra 2)? This will hopefully let you run it.

The AFG31102 is a more advanced instrument than the 1022 that I have tested the package with, so unfortunately your instrument might not be compatible with all the code (the instruments might have slightly different VISA commands for some features). The programmer's manual for your instrument is then the resource you need – it should be fairly easy to make the necessary changes using the code in the module as a base and make changes as appropriate.

However, all the errors so far have been related to pyvisa, so I hope the two instruments are overlapping enough that all the functionality in this module works for you.

asvela commented 3 years ago

@laurenwilliams22 It would be great to know if commenting out the line I suggested works for you!

asvela commented 3 years ago

As far as I know fixed by 9c9a71e, re-open if not.