2014-01-13 33 views
0

我是Matlab OOP的新手。 我附上了一個類,我試圖打開一個串口。我能夠生成SERIAL OBJ,但fopen函數不起作用。地位從未轉向。在matlab中的序列obj類

classdef Channel 

properties 
    COM 
    Baudrate 
    SERIAL_obj 

end 

methods 
    % Constructor 
     function thisChannel=Channel(COM,Baudrate) 
      if nargin==2 
       thisChannel.COM=COM; 
       thisChannel.Baudrate=Baudrate; 
      end 
     end 

    % Generate SERIAL OBJ 
     function SERIAL_obj=get.SERIAL_obj(thisChannel) 
      SERIAL_obj = serial(thisChannel.COM,'Baudrate',thisChannel.Baudrate); 
     end 

    % Connect to SERIAL OBJ 
    function OPEN_SERIAL(thisChannel) 
     fopen(thisChannel.SERIAL_obj); 
     pause(0.2) 
    end 

這裏是生成對象的腳本:

c=Channel('COM197',230400); 
OPEN_SERIAL(c) 

下面是結果:

Serial Port Object : Serial-COM196 

Communication Settings 
    Port:    COM196 
    BaudRate:   230400 
    Terminator:   'LF' 

Communication State 
    Status:    closed 
    RecordStatus:  off 

Read/Write State 
    TransferStatus:  idle 
    BytesAvailable:  0 
    ValuesReceived:  0 
    ValuesSent:   0 

回答

0

我懷疑這是由於value vs. handle class semantics。所以,要麼使之成爲手柄類由handle繼承,或寫open方法爲:

% Connect to SERIAL OBJ 
function thisChannel = OPEN_SERIAL(thisChannel) 
    fopen(thisChannel.SERIAL_obj); 
    pause(0.2) 
end 

,並稱呼其爲:

c = Channel('COM197',230400); 
c = OPEN_SERIAL(c); 

(這是未經測試,我沒有attacted任何串口設備來試試吧)

雖這麼說,我沒有看到你創建的類的點,serial類已經暴露了這些屬性以及更多..