Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

Running python in parallell with LabVIEW RT

Hello,

 

i have a code running on a cRIO-9040. The code runs on the RT part and FPGA (cRIO Programmed in FPGA mode).

The RT part is handling data acquisition and control through C-series modules.

 

We do have an existing cloud solution that i now want to integrate on the cRIO. The existing code handling the data stream to cloud is written in python. 

 

Questions:

1.

Is it feasible to install python on the cRIO-9040 and running the python code in parallel with the existing LabVIEW RT code?

The python code is not time-critical and data will be passed from LabVIEW RT code ty python codeevery 30 sec or so.

2.

Data from LabVIEW RT code to python code will be passed as a TCP/IP package through localhost. This is reusing existing method that works fine on PC targets. My question is: Is there any built-in restrictions/firewalls etc. on the cRIO that will prevent such an approach, i.e. passing data between different applications through TCP/IP on localhost?

 

 

0 Kudos
Message 1 of 4
(112 Views)

I do not really understand your question. If you are asking if you can send data from a LabVIEW application to a python code, then yes. If you are asking if you can program a cRIO using python, the answer is no. I am currently doing a very similar application where I have programmed a cRIO to acquire data (the data is being sent through DMA FIFO from the FPGA). I am sending the data from the RT side to raspberry pi, programmed with python. I haven't had any problems. Here is my RT application and python code for reference. Be advised I removed a lot of the labview block diagram code to show the essence of sending data to a port via TCP/IP connections. In the middle of the flat sequence structure I have two loops.

In loop 1:

I am getting the data from the FIFO and enqueueing the data

In loop 2: 

I am dequeuing the data and writing it to TCP write  

wsimpson0050_0-1762548979850.png

 

Here is python code: 

import socket
from matplotlib import pyplot as plt
from struct import unpack

HOST= '192.168.X.XX'
PORT = 6340

# Create socket and pull data
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# set rx buffer size
recv_buf_size = 4096
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, recv_buf_size)
s.connect((HOST, PORT))
print(f"Connected to {HOST}:{PORT}")

# Receive the length of the payload
len_bytes = s.recv(4)
length = int.from_bytes(len_bytes, byteorder='big')
print(f"Length: {length}")

if length > 1000000:
print("clipping")
length = 1000000

# Pull the actual data
got = 0
buf = bytearray(length)
view = memoryview(buf)

while got < length:
nread = s.recv_into(view[got:], length - got)
if nread == 0:
raise ConnectionError("closed")
got += nread
print(got)

print(f"RX length: {len(buf)}")
format_str = ">" + "h"*(length//2)
decoded = unpack(format_str, buf)
#print(f"Data: {decoded}")

# Send Close request


plt.figure()
plt.plot(decoded[0::2], label="CH1")
plt.plot(decoded[1::2], label="CH2")
plt.legend()
plt.show()

 

Furthermore:   

  • Hardware connection are to be considered. In my case my laptop is connected to a my cRIO via USC-C and regular USB. I use this connection to program the cRIO.
  • I then have a Ethernet cable connected from the cRIO to the raspberry pi, this connection is where the HOST= '192.168.X.XX' IP address comes from where the IP address is the IP address of the cRIO through the ethernet port. USE NI MAX to check those IP address

If you have more questions, please ask. If this was all you needed please leave a kudo and 'Accept as Solution'!

 

0 Kudos
Message 2 of 4
(73 Views)

Hi and thanks for the answer.

 

To clarify my question:

 

I already have the LabVIEW code running on the cRIO. I want to add functionality to stream data to a cloud solution directly from the cRIO, instead of having a device in-between.

Since our existing cloud solution for data stream is implemented in python, and the cloud solution vendor provide python functions handling the communication to the cloud, i wanted to investigate if it is feasible to run the python code on the cRIO, at the same time as the LabVIEW application runs on the cRIO..

 

As cRIO is a linux machine, i was wondering if it is possible to install python on it, and if it is actually possible to run the python code in addition to the LabVIEW real-time code.

I am aware that running python on cRIO in parallell with LabVIEW RT is most likely not supported by NI, i but i want to investigate the possibility of such a setup

 

 

0 Kudos
Message 3 of 4
(47 Views)

This is very interesting problem to solve. I do not have the necessary knowledge to answer your question, but I did find these links that might help... 

1.) GitHub - ni/nifpga-python: Python API for LabVIEW FPGA devices

2.) Integrating Python Code in LabVIEW - NI

3.) Python Resources for NI Hardware and Software - NI

4.) Set cRIO Programming Mode With a Python Script - NI Community

0 Kudos
Message 4 of 4
(34 Views)