creating and using serial objects within a class
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, i've created a script where i am setting up a serialport object in one function and then sending commands to the serial object in another.
however the object is empty when i try to talk to it any ideas where i'm going wrong here?
the functions are being called from an app.
classdef XY_STAGE_TEST
%UNTITLED Summary of this class goes here
% Detailed explanation goes here
properties (Access = public)
XY_port
XY_Comm
p;
b;
db;
sb;
par;
fcon;
Serial;
State;
S_port;
end
methods (Access = public)
function app = XY_STAGE_TEST(port,braud, databits, sbit, parity, fcontrol)
app.p=strcat('COM',num2str(port));
app.b=braud;
app.db=databits;
app.sb =sbit;
app.par = parity;
app.fcon = fcontrol;
end
function [app,State,msg] = XYSerialConnectPort(app) %setup port and check port is availible
if ismember(app.p,serialportlist)
msg = strcat(app.p,' port found');
try
app.S_port = serialport(app.p,app.b,'DataBits',app.db,'StopBits',app.sb,'Parity',app.par,'FlowControl',app.fcon);
State = true;
msg = strcat(msg,' and Connected');
catch
State = false;
msg = strcat(msg,' but I cannot talk to XY comm port, is it already in in use?');
end
else
State = false;
msg = strcat(app.p,' port can not be connected to are you sure it is or not already in use')
end
%disp (State)
end
function [app,State,msg] = connect(app)
[app,State,msg]= app.XYSerialConnectPort();
end
function [app] = home(app)
%app.S_port=serialport(app.p,app.b,'DataBits',app.db,'StopBits',app.sb,'Parity',app.par,'FlowControl',app.fcon) % if i create the serial object here it works fine
%Home Y '430401002201'
str= '430401002201';
command=sscanf(str,'%2X')
write(app.S_port,command,'int8');
end
function close(app)
delete(app.XY_Comm)
delete(app.S_port)
disp 'closed'
end
end
end
2 Kommentare
Walter Roberson
am 28 Feb. 2020
Is it thinking that it is able to connect to it when you use XYSerialConnectPort ?
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!