Serial Port as a property of an object of a class

4 次查看(过去 30 天)
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
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by