Python U3 | LabJack
 

Python U3

3 posts / 0 new
Last post
CorX7
CorX7's picture
Python U3

I have a peripheral program (blender game engine 2.76) which I have worked on in another forum topic which successfully integrated LabJack with Blender2.76 whose python site-packages required Python3.4.3. Now I upgraded the easy commands from LabJackPython to be operable only on Python3 (not backwards compatible with Python2). However, I am need clarification on one line in u3.py. I am certain that this is only difficult since the % operator depreciated from Python2 to Python3 for byte operations. But I need to know what this line expects in as a type and what it needs to put out as a type.

Looking at the file the only lines needing change for u3.py to be fully Python3 compatible for the _DAC_ function is:::

        self.dac = Dac
        self.value = Value
        self.cmdBytes = [ 38 + (Dac % 2), Value % 256, Value >> 8 ]

Just by switching to Python3 from Python2I was getting the error 'Type error: not all arguments converted during string formatting (u3.py) line 2379

Now I input a value (self.value).

#set DAC1 to DAC1_value after converting to hex (16bit ^4) from decimal
    DAC1_value=int(round(DAC1_value/10*65535,0))
    DAC1=str(hex(DAC1_value))
    print(DAC1)
    bytes = str.encode(DAC1)
    type(bytes) # insures its bytes
    print(bytes)
    u3d.getFeedback(u3.DAC16(Dac=0, Value=bytes))

When I implemented the above code changing to bytes, I was getting the error' Type error: unsupported operand type(s) for %: 'bytes' and 'int'

This again is the unsupported % depreciated movement to bytes ...in Python3.4.3? In the Python3.4.3 Idle, 2%256 gives 2 and 3%2 gives 1. What I need to do is use .format and encode commands to give this function the correct input and I need to edit this one line of code in u3.py and then my program will work.

u3.py says"

d.getFeedback(u3.DAC16(Dac = 0, Value = 0x5566))

It wants this format for Value (is that a bytes type b'0x5566' or just '0x5566')

In Python3.4.3,the codecs.encode( takes a b' ' and makes it hex value but not 0x0000 format

codecs.encode(b'64', 'hex')
b'3634'

That can be done (I think)

>>> str(hex(32768))
'0x8000'
>>> str.encode('0x8000')
b'0x8000'

Any help is more than appreciated. I cannot figure out what to put in the function to get it to work in Python3.4.3...bytes....hex....string....

LabJack Support
labjack support's picture
The value you pass for u3

The value you pass for u3.DAC16 is the integer value of the binary data. So if you want to set the value to 0x5566, it would look like:

d.getFeedback(u3.DAC16(Dac = 0, Value = 0x5566))  # 0x5566 = 21862

or

d.getFeedback(u3.DAC16(Dac = 0, Value = 21862))

Don't pass a string or a bytes object.

Here's a quick example to set DAC0 to 1.5 volts:

import u3
d = u3.U3()
d.getCalibrationData()
dac0_value = d.voltageToDACBits(1.5, dacNumber = 0, is16Bits = True) # voltageToDACBits returns an int
d.getFeedback(u3.DAC16(Dac = 0, Value = dac0_value)) # Set DAC0 to 1.5 V

This is derived from the low-level quick start page's code:

https://labjack.com/support/software/examples/ud/labjackpython/low-level

CorX7
CorX7's picture
Thank you very much for the

Thank you very much for the help. You were exactly right. I had been passing strings <types> or <bytes>. The device now integrates with the application. Your company is terrific. We will definitely be purchasing more of your devices.