Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

performing single comments and stream at the same time, multi-threading


  • Please log in to reply
3 replies to this topic

#1 Martine

Martine
  • Members
  • 6 posts

Posted 23 January 2014 - 12:58 PM

Dear all,

 

I want to record the behaviour of my animal, and depending on the output of a sensor send a TTL to the reward pump with a certain delay. At the same time I would like to record the values of the sensor (multiple) and the reward pump at high frequencies. I program this all in Matlab.

 

I splitted it up into two Labjacks, one master that organizes all the events, and one recorder that records at high frequencies all the parameters.

 

Within matlab I can call the individual functions on there own and this works fine. However I can't get them to work at the same time. My approach was

- Open two matlab programns

- let each matlab program run there own script based on one labjack.

 

I have defined the labjack as

[ljerror, ljhandle1] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '1', false , 0);

[ljerror, ljhandle2] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '2', false , 0);

in function master and function record respectively.

 

However although these lines together in a single script work fine, if I start to run to seperate scripts I get the error of Lab Jack already being open. In addition I am not sure if opening two matlab programs is the most elegant way to do this. Should I go to multi-threading (next question how?)

 

Any suggestion how to improve?

 

Thanks in advance.

 

 



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 24 January 2014 - 11:50 AM

The U3 (and in general LabJacks) can only be opened by one process at a time over USB. The other program will get the "LabJack already open" open errors. Make sure that each script only opens one of the U3s. Close the script/MATLAB to free the U3 so another process can use it.

 

Using two scripts should be fine, though as I mentioned one script should only open/claim one U3. If using one script for both U3s, you should use a thread for each stream loop. I haven't used it myself, by I think using "batch" you can run parallel code from scripts.



#3 Martine

Martine
  • Members
  • 6 posts

Posted 27 January 2014 - 03:33 AM

Dear all,

 

I have applied this method, and have one master function that starts the functions in paralel, with the handle to the Labjack as input variable. Although the handle number is passed properly, any labjack communinication line using the handle gives an "UD Error: Invalid handle":

 

Main function:

function[]=StartMasterAndRecord_v1()
clear %Clear the MATLAB variables

ljasm = NET.addAssembly('LJUDDotNet'); %Make the UD .NET assembly visible in MATLAB
ljudObj = LabJack.LabJackUD.LJUD;

ioType = LabJack.LabJackUD.IO;
channel = LabJack.LabJackUD.CHANNEL;

try
chanType = LabJack.LabJackUD.CHANNEL.LOCALID.GetType;

%Read and display the UD version.
disp(['UD Driver Version = ' num2str(ljudObj.GetDriverVersion())])

%Open the first found LabJack U3.
[ljerror, ljhandle1] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '1', false , 0);
[ljerror, ljhandle2] = ljudObj.OpenLabJack(LabJack.LabJackUD.DEVICE.U3, LabJack.LabJackUD.CONNECTION.USB, '2', false , 0);

catch e
end

ljhandle1
ljhandle2

funcs={@Record_v1,@Master_v1};
arguments={ljhandle1;ljhandle2};
results=cell(1,2);

matlabpool(2)
parfor i = 1:2
results{1,i}=funcs{i}(arguments{i});
end
matlabpool close

results{1,1}.test
results{1,2}.test

end

stript record and master functions:

function[output]=Master_v1(ljhandle)
ljhandle

ljasm = NET.addAssembly('LJUDDotNet'); %Make the UD .NET assembly visible in MATLAB
ljudObj = LabJack.LabJackUD.LJUD;

try

%Read and display the UD version.
disp(['UD Driver Version = ' num2str(ljudObj.GetDriverVersion())])

%Read and display the hardware version of this U3.
ljhandle
[ljerror, dblValue] = ljudObj.eGet(ljhandle, LabJack.LabJackUD.IO.GET_CONFIG,LabJack.LabJackUD.CHANNEL.HARDWARE_VERSION, 0, 0);
disp(['U3 Hardware Version = ' num2str(dblValue)])

catch e
showErrorMessage(e)
end
output.test='Rosa';
end
function[output]=Record_v1(ljhandle)
% Open the first found LabJack U3.
ljhandle
output.test='Martine';
end

Can someone explain why I get the following error after the line (in master_v1)

[ljerror, dblValue] = ljudObj.eGet(ljhandle, LabJack.LabJackUD.IO.GET_CONFIG,LabJack.LabJackUD.CHANNEL.HARDWARE_VERSION, 0, 0);

 

"UD Error: Invalid handle
Error using ==> Master_v1 at 33
Message: Error in the application.
Source: LJUDDotNet
HelpLink:

Error in ==> StartMasterAndRecord_v1>(parfor body) at 33
        results{1,i}=funcs{i}(arguments{i});

Error in ==> parallel_function>make_general_channel/channel_general at 879
                    O = F(C{:});

Error in ==> remoteParallelFunction at 31
        out = distcompMakeByteBufferHandle(distcompserialize(feval(channel, channelArgs{:})));"



#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 27 January 2014 - 01:37 PM

First, are your OpenLabJack calls returning an error/exception? It looks like your code will ignore open errors and pass invalid handlers afterwards.

 

Second, in your Master_v1 function remove the "ljasm = NET.addAssembly('LJUDDotNet');" call and see if that helps.

 

The easiest way to stream multiple devices would be to have a script or function run both the opening and streaming code of one U3 and run those in parallel.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users