MSLNZ / msl-loadlib

Load a shared library (and access a 32-bit library from 64-bit Python)
MIT License
75 stars 17 forks source link

Question to labview32.dll #13

Closed rddaz2013 closed 6 years ago

rddaz2013 commented 6 years ago

If my Vi has an output labview string - how i get his back to my python64 environment

Input double - Output String (Labview32) Python 64 ? string?

jborbely commented 6 years ago

To illustrate how to do this I created the following VI

double_to_string

and used a 32-bit version of LabVIEW to build it in to a DLL called double_in_string_out.dll

The corresponding header file that LabVIEW created for the exported DLL is

#include "extcode.h"
#pragma pack(push)
#pragma pack(1)

#ifdef __cplusplus
extern "C" {
#endif

void __cdecl Double_to_string(double DoubleIn, char StringOut[], int32_t StringLength);

long __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);

#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)

I created a module called my_server.py, which calls the LabVIEW function

import ctypes
from msl.loadlib import Server32

class MyServer(Server32):

    def __init__(self, host, port, quiet, **kwargs):
        super(MyServer, self).__init__('double_in_string_out.dll', 'cdll', host, port, quiet)

    def double_to_string(self, number):    
        n = 256  # make it big enough to allocate enough memory space
        string = ctypes.create_string_buffer(n)
        self.lib.Double_to_string(ctypes.c_double(number), string, n)        
        return string.value

and a module called my_client.py, which is run from 64-bit Python

from msl.loadlib import Client64

class MyClient(Client64):

    def __init__(self):
        super(MyClient, self).__init__(module32='my_server')

    def call_labview_function(self, number):
        return self.request32('double_to_string', number)

I saved double_in_string_out.dll, my_server.py and my_client.py in an issue13 folder then ran the following

D:\issue13>python
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from my_client import MyClient
>>> c = MyClient()
>>> c.call_labview_function(123.456789)
b'LabVIEW received the number 123.456789'
rddaz2013 commented 6 years ago

many thx ... it works - very good example

jborbely commented 6 years ago

Good. I'm happy to hear that you got it working.