Making Serial Port object as a property of another class gives an error

1 次查看(过去 30 天)
Hi,
I've a class named 'Task' which has one property named 'serialPort' as shown in the code below:
classdef Task < handle
properties
serialPort;
end
methods
function obj = Task(varargin)
oldSerial = instrfind('Port', 'COM4');
if (~isempty(oldSerial))
delete(oldSerial)
end
obj.serialPort = serial('COM4');
obj.serialPort.BaudRate = 115200;
size_data = 1000;
obj.serialPort.BytesAvailableFcn = {@store_plot_data,size_data};
obj.serialPort.BytesAvailableFcnCount = size_data;
obj.serialPort.InputBufferSize = 30000;
obj.serialPort.BytesAvailableFcnMode = 'byte';
fopen(obj.serialPort);
end
end
In the constructor I'm assigning the properties of the serial port object, but on executing this gives an error
"Open failed: Port: COM4 is not available. Available ports: COM1, COM3. Use INSTRFIND to determine if other instrument objects are connected to the requested device".
I have checked that COM4 port is available during the creation of the Task object but still it throws this error whereas I don't have port COM1 and COM3. Can anyone please suggest me where am I going wrong ?
Thanks,

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by