Opening a serial port in matlab no java mode

时间:2018-10-24 11:24:22

标签: java matlab port eventtrigger psychtoolbox

I'm trying to open an utilise a serial port connected to a BBTK USB TTL Module. The code I'm using is as follows:

function [s]=TTLInit

 if ~isempty(instrfind) %Close any open ports
     fclose(instrfind);
     delete(instrfind);
 end


 s=serial('COM3') %Set up serial object 

 try
    fopen(s) %Open serial port
 catch
    disp('TTLInit failed because no port connected')
    s=999;
 end
 if s~=999
    set(s,'BaudRate',115200,'DataBits',8,'StopBits',1,'Parity','none') %Set parameters
    fprintf(s,'RR') %test trigger
 end

This works fine so long as I'm running MATLAB fully. However, I need to be able to utilise this alongside Psychtoolbox which unfortunately means I have to run in No Java Mode. When I try to run this through no java mode it returns the error: "Undefined variable "com" or class "com.mathworks.toolbox.instrument.Instrument.getNonLockedObjects".

My understanding is that this is due to MATLAB needing java to open and control objects (though correct me if I'm wrong!). Therefore, I was wondering if anyone knew a way around this? Is it possible to open serial ports like this and send triggers through them without using the java objects or some other way in no java mode?

Thanks,

Martin

2 个答案:

答案 0 :(得分:0)

我设法找到了解决方法。无需启动任何Java模式(我认为我必须要做的就是让Psychtoolbox正常工作),我可以直接启动至任何桌面模式。这使MATLAB可以访问所需的Java函数(确认了我先前的假设,这是Java问题),还允许Psychtoolbox正常工作。因此,尽管我没有找到一种无需Java即可打开串行端口的方法(并且您可以继续讨论此问题,但我仍然有兴趣了解是否有方法!),但确实解决了我的紧迫问题。

答案 1 :(得分:0)

您通常不需要启动任何Java模式或桌面模式即可启动Psychtoolbox。

但是,如果您使用的是Psychtoolbox,则可以使用Psychtoolbox提供的IOPort函数来访问串行端口:http://psychtoolbox.org/docs/IOPort-OpenSerialPort

这样做的好处还在于可以跨操作系统和平台(即MATLAB和Octave)工作。