clade / PyDAQmx

Interface to National Instrument NIDAQmx driver
Other
133 stars 55 forks source link

WriteDigitalLines on __del__ causing mafunction #60

Open o7-machinehum opened 4 years ago

o7-machinehum commented 4 years ago

I want to but a quick command to drop the output in the case of program termination, however this is causing an exception.

Windows 10, Python 3.7.3

python ni.py

Exception ignored in: <function DO.__del__ at 0x000001BF19B26378>
Traceback (most recent call last):
  File "ni.py", line 20, in __del__
  File "ni.py", line 27, in low
  File "<string>", line 3, in WriteDigitalLines
  File "<string>", line 2, in function
  File "C:\ProgramData\Anaconda3\lib\site-packages\PyDAQmx\DAQmxFunctions.py", line 57, in mafunction
ctypes.ArgumentError: argument 6: <class 'ImportError'>: sys.meta_path is None, Python is likely shutting down
import PyDAQmx
from PyDAQmx import Task
import numpy as np
import time
import pdb
import weakref

high = np.array([1], dtype=np.uint8)
low  = np.array([0], dtype=np.uint8)

# Simple class that can be used to control one Digital output line
class DO:
    def __init__(self, card, lines):
        self.task = Task()
        self.task.CreateDOChan('/{}/port0/{}'.format(card, lines), 
            "", PyDAQmx.DAQmx_Val_ChanForAllLines)
        self.task.StartTask()

    def __del__(self):
        self.low()
        self.task.StopTask()

    def high(self):    
        self.task.WriteDigitalLines(1, 1, 10.0, PyDAQmx.DAQmx_Val_GroupByChannel, high, None, None)

    def low(self): 
        self.task.WriteDigitalLines(1, 1, 10.0, PyDAQmx.DAQmx_Val_GroupByChannel, low, None, None)

if __name__ == '__main__':
    dig_0 = DO('cDAQ1Mod1', 'line0') 
    dig_0.high()
    time.sleep(5) 
    dig_0.low()