-4

I am trying to create a few inputs in LabVIEW 2016 which a user can modify (eg. total frames = 100). This number will then be sent to and RPi3 unit which is running a basic TCP/IP server script. The idea is that these numbers will set the variables in a simple instrument control code. I can connect to the RPi in LabVIEW and can send a string but things break down there:

-the string seems to be sent as Decimal ASCII

-when decoded in python, the string is converted so that every character is a separate element, with a separate index (eg. if I send 100 I get str[0]=1, str[1]=0, str[2]=0, len(str)=3)

-i need to send several variables and several commands in one string and then unpack it in python so that I can call each one separately and assign it to the appropriate function

Any advice would be appreciated.

  • 1
    Please show the LabVIEW code you're using to send the data (as a [VI Snippet](http://www.ni.com/tutorial/9330/en/)) and the Python code you're using to receive it. Also what do you mean by 'total frames = 100'? – nekomatic Mar 18 '19 at 10:16
  • I have been using the LVClient2011.vi and SimpleServer.py code found here: https://forums.ni.com/t5/Example-Program-Drafts/Python-LabVIEW-TCP-IP-socket-communication/ta-p/3537049 The code you find there works to establish TCP/IP connections. The labVIEW vi includes buttons to 'control' the server code. In the VI block diagram you can see it is sending constant words via TCP such as 'PLAY',. The python code looks for these in the received data. – Tim Henley Mar 19 '19 at 16:32
  • The ultimate goal is to have a VI where users can input variables (for instance we must specify the 'total frames' for a given experiment - the total frames acquired by a camera in our microscope).
    We already have a working python GPIO code on the RPI which we use to control the lasers and cameras in our set up. We would like to be able to adjust the variables in this GPIO code in LabVIEW because we control the stage and objective of our microscope in LabVIEW.
    We must be able to input a half dozen variables in LabVIEW and send these to RPI to use as parameters in the code.
    – Tim Henley Mar 19 '19 at 16:35
  • Please don't ever post links to code in the comments, there are a number of reasons for that, but here are two. 1: The link is more likely to be deprecated than providing the code in the question itself 2: The code should always be inside of the core question, as it is a core element of the question – Stephan T. Mar 19 '19 at 18:15

1 Answers1

0

I would personally just create a cluster of your paramters in labview, then use 'flatten to json' and you can simply decode that in python into a object with each of your variables inside it.

in python, you can then easily load that config data

import json

# sample string, replace this with received data from client/server comms
config = '{'total frames': 100, 'rate': 30, 'additional': 'whatever'}'  
newconfig = json.loads(config)
print( list(newconfig) )  # list of all the tuples
# ['total frames', 'rate', 'additional']
# to access any tuple
num_frames = newconfig['total frames']
Unipsycho
  • 25
  • 7
  • Thank you Unipsycho, this did solve my initial problem. For example: `cmnd = clientConnection.recv(1024)` `new_config = cmnd.decode("utf-8", "ignore")` `config = json.loads(new_config)` `total_frames = float(config['total_frames'])` – Tim Henley May 07 '19 at 19:22