Python3 scpi binary data how to decode

Applications, development tools, FPGA, C, WEB
Post Reply
matthijsfh
Posts: 7
Joined: Wed Jun 23, 2021 12:09 pm

Python3 scpi binary data how to decode

Post by matthijsfh » Wed Jun 23, 2021 12:34 pm

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

pavel
Posts: 656
Joined: Sat May 23, 2015 5:22 pm

Re: Python3 scpi binary data how to decode

Post by pavel » Wed Jun 23, 2021 12:43 pm

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)
Last edited by pavel on Wed Jun 23, 2021 1:37 pm, edited 2 times in total.

matthijsfh
Posts: 7
Joined: Wed Jun 23, 2021 12:09 pm

Re: Python3 scpi binary data how to decode

Post by matthijsfh » Wed Jun 23, 2021 1:16 pm

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

pavel
Posts: 656
Joined: Sat May 23, 2015 5:22 pm

Re: Python3 scpi binary data how to decode

Post by pavel » Wed Jun 23, 2021 1:30 pm

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.

matthijsfh
Posts: 7
Joined: Wed Jun 23, 2021 12:09 pm

Re: Python3 scpi binary data how to decode

Post by matthijsfh » Wed Jun 23, 2021 1:56 pm

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

pavel
Posts: 656
Joined: Sat May 23, 2015 5:22 pm

Re: Python3 scpi binary data how to decode

Post by pavel » Wed Jun 23, 2021 2:08 pm

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.

matthijsfh
Posts: 7
Joined: Wed Jun 23, 2021 12:09 pm

Re: Python3 scpi binary data how to decode

Post by matthijsfh » Wed Jun 23, 2021 6:54 pm

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

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 1 guest