Serial Port as a property of an object of a class

4 ビュー (過去 30 日間)
Swarnava Pramanik
Swarnava Pramanik 2016 年 8 月 3 日
コメント済み: Walter Roberson 2016 年 8 月 3 日
Hi,
I have created a serial port as a property to an object. In the program I assigned the BytesAvailableFcn property of the serial port to a callback function. However whenever I execute the code it gives an error " Undefined function 'acquireSamples' for input arguments of type 'serial'." Can anyone please help me regarding this issue ? I'm attaching the code.
classdef max10Display <max10lia.Max10LIAClass
properties
serialPort;
end
methods
function obj = max10Display(varargin)
obj.serialPort = serial('COM3');
obj.serialPort.BaudRate = 115200;
end
function getStarted(obj)
obj.serialPort = serial('COM3');
obj.serialPort.BaudRate = 115200;
size_data = 1000;
obj.serialPort.BytesAvailableFcn = {@acquireSamples size_data};
obj.serialPort.BytesAvailableFcnCount = size_data;
obj.serialPort.InputBufferSize = 30000;
obj.serialPort.BytesAvailableFcnMode = 'byte';
numSamples = obj.everyNSamples;
comma = ' ';
numSamples = ['N_Sample',comma,num2str(numSamples)];
fopen(obj.serialPort);
fprintf(obj.serialPort, numSamples);
end
function acquireSamples(obj,~,chunk)
........
........
end
end
end

採用された回答

Walter Roberson
Walter Roberson 2016 年 8 月 3 日
You made acquireSamples a non-static method of your class, so it expects an object of your class max10Display as the first parameter. But you are using a standard serial port BytesAvailableFcn callback, and for that the first parameter passed is the serial port object.
  2 件のコメント
Swarnava Pramanik
Swarnava Pramanik 2016 年 8 月 3 日
Hi Walter,
Thanks for your prompt response. I don't want to make the "acquireSamples" method as a static method so is there any other way?
Thanks
Walter Roberson
Walter Roberson 2016 年 8 月 3 日
obj.serialPort.BytesAvailableFcn = {@(src, event) acquireSamples(obj, src, event), size_data};

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by