Page 1 of 1

Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 12:34 pm
by matthijsfh
Hai!

Got myself a pitaya recently. Now playing around with python3. Made a simple scope script which works, but is not very fast.

So, tried the bin transfer mode. Already found out that I have to update the code as the default example does not work in python3.

Code: Select all

    def rx_bin(self):
        # The first thing it sends is always a #
        #
        buf = self._socket.recv(1)
        print(buf.decode('utf-8'))

        # The second thing it sends is the number of digits in the byte count. 
        buf = self._socket.recv(1)
        digits_in_byte_count = int(buf)
        print(digits_in_byte_count)

        # The third thing it sends is the byte count
        buf = self._socket.recv(digits_in_byte_count)
        print(buf.decode('utf-8'))
        byte_count = int(buf)

        buf = []
        while len(buf) != byte_count:
            buf.append(self._socket.recv(1))

	print(buf)
	
        return buf
        
That works. I now get a "list" back.

--> How to convert to floats again? I am lost!

Code: Select all

#
5
65536
[b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00', b'?', b'\x7f', b'\xf8', b'\x00'
Debugger reports variable "buf" is a list.

Anyone an example on how to go to floats again? Preferable np.array?

Greetings Matthijs

Re: Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 12:43 pm
by pavel
Try to replace

Code: Select all

        buf = []
        while len(buf) != byte_count:
            buf.append(self._socket.recv(1))

with

Code: Select all

        buf = np.empty(byte_count // 4, dtype = np.float32)
        size = self._socket.recv_into(buf)

Re: Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 1:16 pm
by matthijsfh
Thanks for the super fast reply!

Unfortunately this is the result:

Code: Select all

  File "../classes\redpitaya_class.py", line 66, in GetData_Bin
    buff_string = self.rp.rx_bin()

  File "../classes\redpitaya_scpi.py", line 111, in rx_bin
    buf = np.empty(byte_count / 2, dtype = np.int16)

TypeError: 'float' object cannot be interpreted as an integer
Why are you casting to int16? Is that the datatype of the bin transfer mode?

I guess there are 4 bytes in the data (65536 bytes for 16384 points), which matches 4 bytes for a float.

Grtz Matthijs

Re: Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 1:30 pm
by pavel
It's just an example of how to read binary data to a numpy array. I have no idea how the data is returned. It's up to you to change this example in a way that would work for you.

I think that the 'float object ...' error is because of wrong division operator in my example. I've just updated it. Try to replace 'byte_count / 2' with 'byte_count // 2'.

I see now that you mentioned 'floats' in your first comment. I've just updated the example and replaced int16 with float32.

Re: Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 1:56 pm
by matthijsfh
Your idea works!

But there is something going wrong in the whole communication when I switch to BIN data. The status messages which I need to check if the trigger has been triggered are now messed up.

So will stick to ascii for now. It's a pitty.

Greetings Matthijs

Re: Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 2:08 pm
by pavel
Another solution would be to try PyRPL:

https://pyrpl.readthedocs.io/en/latest/

It also has the oscilloscope functionality:

https://pyrpl.readthedocs.io/en/latest/ ... ules.scope

I've never used it myself but it looks interesting.

Re: Python3 scpi binary data how to decode

Posted: Wed Jun 23, 2021 6:54 pm
by matthijsfh
Yep, I have installed that one. Much more complex so I though to get the scpi right first. But getting a lot of strange results so far.

Greetings Matthijs