Serial Port as a property of an object of a class

3 vues (au cours des 30 derniers jours)
Swarnava Pramanik
Swarnava Pramanik le 3 Août 2016
Commenté : Walter Roberson le 3 Août 2016
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

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Août 2016
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 commentaires
Swarnava Pramanik
Swarnava Pramanik le 3 Août 2016
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 le 3 Août 2016
obj.serialPort.BytesAvailableFcn = {@(src, event) acquireSamples(obj, src, event), size_data};

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by