저는 다른 데이터 수집 프로젝트에서 작업 중이며, 이는 객체 지향 프로그래밍 질문으로 바뀌 었습니다. 내 코드의 맨 아래에있는 "main"에서 Object DAQInput의 두 인스턴스를 만듭니다. 내가 이걸 썼을 때, 내 메소드 .getData가 특정 인스턴스의 taskHandle을 참조 할 것이라고 생각했지만 그렇지 않다. 코드를 실행하면 첫 번째 핸들을 두 번 사용하여 getData 작업을 수행하므로 Python에서 객체 지향 프로그래밍을 명확하게 이해하지 못합니다. 유감스럽게도이 코드는 PyDAQmx와 National Instruments 보드가 없으면 작동하지 않습니다.Python의 두 클래스 인스턴스가 다르지 않습니다.
from PyDAQmx import *
import numpy
class DAQInput:
# Declare variables passed by reference
taskHandle = TaskHandle()
read = int32()
data = numpy.zeros((10000,),dtype=numpy.float64)
sumi = [0,0,0,0,0,0,0,0,0,0]
def __init__(self, num_data, num_chan, channel, high, low):
""" This is init function that opens the channel"""
#Get the passed variables
self.num_data = num_data
self.channel = channel
self.high = high
self.low = low
self.num_chan = num_chan
# Create a task and configure a channel
DAQmxCreateTask(b"",byref(self.taskHandle))
DAQmxCreateAIThrmcplChan(self.taskHandle, self.channel, b"",
self.low, self.high,
DAQmx_Val_DegC,
DAQmx_Val_J_Type_TC,
DAQmx_Val_BuiltIn, 0, None)
# Start the task
DAQmxStartTask(self.taskHandle)
def getData(self):
""" This function gets the data from the board and calculates the average"""
print(self.taskHandle)
DAQmxReadAnalogF64(self.taskHandle, self.num_data, 10,
DAQmx_Val_GroupByChannel, self.data, 10000,
byref(self.read), None)
# Calculate the average of the values in data (could be several channels)
i = self.read.value
for j in range(self.num_chan):
self.sumi[j] = numpy.sum(self.data[j*i:(j+1)*i])/self.read.value
return self.sumi
def killTask(self):
""" This function kills the tasks"""
# If the task is still alive kill it
if self.taskHandle != 0:
DAQmxStopTask(self.taskHandle)
DAQmxClearTask(self.taskHandle)
if __name__ == '__main__':
myDaq1 = DAQInput(1, 4, b"cDAQ1Mod1/ai0:3", 200.0, 10.0)
myDaq2 = DAQInput(1, 4, b"cDAQ1Mod2/ai0:3", 200.0, 10.0)
result = myDaq1.getData()
print (result[0:4])
result2 = myDaq2.getData()
print (result2[0:4])
myDaq1.killTask()
myDaq2.killTask()
공유 객체 (클래스 변수, 예 : sumi)가 munged되고있을 가능성이 있습니다 (그러나 케이스가 최소가 아니므로 * 증상 *과 예상되는 동작이 명확하게 설명되지 않았기 때문에 나는 더 이상보고 싶지 않습니다). See [클래스 내부 및 외부 변수 __init __() 함수] (http://stackoverflow.com/questions/1537202/variables-inside-and-outside-of-a-class-init-function) – user2246674