I currently have a script in Python (v3.6) that initializes a connection to a serial port and creates a serial object that can be called whenever I need to reset the buffer, read a register address, etc. The issue is I would like to use LabVIEW instead to call this Python initialization script, create the serial object, and store it (in the LabVIEW session) so it can be used later in LabVIEW without having to reinitialize the object. I do not want to keep the python session open in LabVIEW either as I need to run other scripts unassociated with this python object (i.e. move stages around, control a camera, etc.). Here is the sort of block diagram I'm testing with. Clearly, it does not work because the python object is being casted to array.
Here is the snippet of code I am using to initialize the object:
ser = serial.Serial(port=port, baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, timeout=10,
xonxoff=False, rtscts=False, dsrdtr=False)
And here is an example of a function I would like to call later in LabVIEW, but requires the object to be initialized:
def set_LED_RGB_currents(ser, currents=[100, 100, 100]):
set_LED_current(ser, 'R', currents[0])
set_LED_current(ser, 'G', currents[1])
set_LED_current(ser, 'B', currents[2])
I know there is a serial connection example VI in LabVIEW and maybe I can use this sample VI to create the object and pass it into Python, but I'm not sure how to do the passing. Note, the above functions are just examples, I have more convoluted scripts I need to perform and it would be easier just to call them from LabVIEW rather than rewrite them so Python is not called at all.
I hope this all makes sense, I'm not great at explaining. Ultimately, my question is how do I pass serial objects between the two languages while maintaining the object's integrity without reinitializing a connection every time? Thanks in advance!
To pass an arbitrary Python object to LabVIEW and back I guess you should be able to use pickle.dumps
to turn the object into a bytes
stream which you can pass across and pickle.loads
to go back the other way. But I'm pretty sure that won't persist the state of a serial port object on the Python side.
I also don't know any meaningful way in which you can pass a LabVIEW serial 'object' to Python and use it in Python. Maybe this can be done by digging into to the lower levels of VISA.
From your description it sounds as though the overall control of your process or experiment is going to be in LabVIEW? In that case I think your options are: