Python Ctypes输出根据执行顺序返回不正确的值吗?

时间:2018-07-10 19:34:42

标签: python python-3.x numpy ctypes labview

所以我在python中有一个类,它接收两个输入numpy 1d数组和一个int并将它们发送到由LABVIEW控制的设备。我们编写LABVIEW代码的方式是从设备读取信号。单元测试当前失败:我试图将两个输入数组读回python程序,它们与输入数组不同,并且LABVIEW不会更改它们。

我们还注意到读取函数的顺序也很重要(read_output,read_input_array1,read_input_array2)。

import os
from ctypes import cdll, c_int32, c_double
from numpy.ctypeslib import ndpointer
import numpy as np


class measurement_device():
    def __init__(self,path=path, dll=dll):
        self.path = path
        self.dll = dll
        os.chdir(self.path)
        self.LabVIEWQMS = cdll.LoadLibrary(self.dll)

    def read_output(self, int_value, input_array2, input_array1):

        assert len(input_array1) == len(input_array2), 'lengths of arrays are not the same'
        self.LabVIEWQMS.Main_LABVIEW_Script.argtype = [c_int32, c_int32*int_value, c_double*int_value]
        self.LabVIEWQMS.Main_LABVIEW_Script.restype = c_double*int_value

        output = self.LabVIEWQMS.Main_LABVIEW_Script(int_value, input_array1, input_array2)

        return output

    def read_input_array1(self, int_value, input_array_1):
        self.LabVIEWQMS.LABVIEW_Return_Array1.argtype = [c_int32, c_double*int_value]
        self.LabVIEWQMS.LABVIEW_Return_Array1.restype = c_double*number_of_masses
        array1_out = self.LabVIEWQMS.LABVIEW_Return_Array1(int_value, input_array1)

        return array1_out

    def read_input_array2(self, int_value, input_array2):
        self.LabVIEWQMS.LABVIEW_Return_Array2.argtype = [c_int32, c_int32*int_value]
        self.LabVIEWQMS.LABVIEW_Return_Array2.restype = c_int32*int_value
        array2_out = self.LabVIEWQMS.LABVIEW_Return_Array2(int_value, input_array2)

        return array2_out

然后运行此命令:

if __name__ == "__main__":
    device1 = measurement_device()
    input_array1 = np.ctypeslib.as_ctypes(np.array([17.69, 27.91]))
    input_array2 = np.ctypeslib.as_ctypes(np.array([3, 3]))
    #1
    signals = device1.read_output(2, input_array2, input_array1)
    signals = np.ctypeslib.as_array(signals)
    print('signals: \n' + str(signals))
    #2
    array2_out = device1.read_input_array2(2, input_array2)
    array2_out = np.ctypeslib.as_array(array2_out)
    print('array2: \n' + str(array2_out))
    #3
    array1_out = device1.read_input_array1(2, input_array1)
    array1_out = np.ctypeslib.as_array(array1_out)
    print('array1: \n' + str(array1_out))

更改数字1、2和3的顺序会更改脚本输出,但是所有三个输出都不正确。例如,数字1可能是正确的,但数字2是完全错误的,数字3与数字1相同。

例如

signals: 
[ 57.26031494 110.0261747 ]
array2: 
[         0 1079738796]
array3: 
[ 57.26031494 110.0261747 ]

vs。如果订单为#2,1,3,则输出为:

array2: 
[0 3]
signals: 
[ 66.8012085  112.98357817]
array1: 
[ 66.8012085  112.98357817]

0 个答案:

没有答案