mccdaq / mcculw

MCC Universal Library Python API for Windows
MIT License
78 stars 30 forks source link

Reading analog inputs in different processes #5

Closed toti08 closed 2 years ago

toti08 commented 6 years ago

It looks like it is not possible to read the different channels in different processes, the configuration is somehow only known in the main process memory space. Is there some sort of limitation on this?

jeffreyg3 commented 6 years ago

Hi toti08, please elaborate on what you mean by 'read the different channels in different processes'?

toti08 commented 6 years ago

Hi jeffreyg4 sorry, I understand my explanation was not very clear. What I am trying to do is to read all the channels at the same moment, so I want to create a different process for every channel. Something like this:

class DAQDevice():
    def __init__(self):
        ul.ignore_instacal()
        self.devices = ul.get_daq_device_inventory(InterfaceType.ETHERNET)
        board_num = 0

        for device in self.devices:
            ul.create_daq_device(board_num, device)
            ul.a_input_mode(board_num, AnalogInputMode.SINGLE_ENDED)
            board_num += 1        

    def read_ai_channel(self, channel):
        retVal = []
        board_num = 0
        try:
            for i in range(1000):
                value = ul.a_in(board_num, channel, AI_RANGE)
                retVal.append(ul.to_eng_units(board_num, AI_RANGE, value))            
        except Exception as e:
            print e
            retVal = 0

        return retVal

    def read_all_channels(self):
        processes = []
        for i in range(NUM_CHANNELS):
            dev = self.devices[0]
            proc = Process(target = self.read_ai_channel, args = (i,))
            processes.append(proc)        
        [x.start() for x in processes]

This code fails to execute because the "read_ai_channel" method does not recognize the board (I get "Error 1: Invalid board number"). The same functions work with no issues when run without multiprocessing.

Hope this helps to explain the problem, I'm not sure it's an issue.

jeffreyg3 commented 6 years ago

Hi toti08, Given, I don't claim to be an expert in Python. Looked up what a process is in Python, states it is an instance of a program. if that is so, then that would require a new instance of the universal library as well. And you don't want to have multiple instances of the library loaded. Better to have just one instance of the driver/library loaded in a single thread, and have your other instances/processes access the data from that original thread. If I understand what you are doing above, In the original process, I would start am AInScan() for the number of channels you want, at the rate you want, and keep moving that data to a local array or list. From your other process(es) access that array/list to get the latest data.

toti08 commented 6 years ago

Thanks a lot, I'll give it a try.

Meanwhile I was able to get it to work using threads (which run on the same memory space of the main process) instead of processes (which have their own memory space). But I think your solution is better.

willyd commented 4 years ago

Better to have just one instance of the driver/library loaded in a single thread, and have your other instances/processes access the data from that original thread.

@jeffreyg3 Is this true when using multiple devices on the same computer? We have an issue where we load the library in a process to access the data from a USB2404-UI and a second process where we access a USB2416. When we close the the process accessing the USB2416 the other process throws a ULError with this error message:

The device acknowledged the operation, but has not completed before the timeout.

This error does not happen when we close the process reading the USB2404-UI and keep the process using the USB2416 alive.

jeffreyg3 commented 4 years ago

Yes this is true for multiple devices as well. As I do not know what you are doing, I cannot say why you get that response. I would need a stripped down working example of you app. However, this is not the forum to continue this conversation. To discuss further, please submit a support request through https://www.mccdaq.com/support/support_form.aspx or call us at 508-946-5100

willyd commented 4 years ago

@jeffreyg3 Thanks for the quick answer. I will create an official support ticket.