Callback error with serial connection
Mostra commenti meno recenti
I'm using serial connection to connect a syringe pump to MatLab through using GUIs. When the user hits the pushbutton for the GUI it sends a callback to the function like usual examples code is stated below
I initiated the serial connection earlier in the code and every time I run the code I keep getting the error undefined variable ser The serial port connection variable. I was wondering if there is also a way to do callbacks without a function and if so what would the code look like?
ser=serial('COM1','BAUD', 9600,'DataBits', 8);
S.fh = figure('units','pixels',...
'position',[500 500 350 80],...
'menubar','none',...
'numbertitle','off',...
'name','Spraymode',...
'resize','off');
S.rate = uicontrol('style','edit',...
'units','pixels',...
'position',[10 10 140 30],...
'fontsize',14,...
'string','R2000.');
S.time = uicontrol('style','edit',...
'units','pixels',...
'position',[10 40 140 30],...
'fontsize',14,...
'string','1.0');
S.pb = uicontrol('style','push',...
'units','pixels',...
'position',[240 20 80 50],...
'fonts',14,...
'str','Spray',...
'callback',{@pb_call,S});
%sprayrate=input('Define Spray rate: ','s');
%sprayrate1=str2num(sprayrate);
%adjsprayrate=(sprayrate1/40)*2000;
%rasprayrate=round(adjsprayrate);
%rasprayrate1=num2str(rasprayrate);
%srate=sprintf('R%s',rasprayrate1);
%srate=sprintf('R%s',sprayrate);
function [] = pb_call(varargin);
ser=serial('COM1','BAUD', 9600,'DataBits', 8);
%initialize, set up channels
fopen(ser);
% Callback for the button labeled PUSH_1.
S = varargin{3}; % Get the structure.
sprayrate=get(S.rate,'string');
spraytime=get(S.time,'string');
sptime=str2num(spraytime);
fprintf(ser,'L1;I;%s;C0.0;sync',sprayrate);
fprintf(ser,'L2;I;%s;C0.0;sync',sprayrate);
fprintf(ser,'G');
pause(sptime);
fprintf(ser,'H');
1 Commento
Risposte (1)
Walter Roberson
il 14 Apr 2016
It does not look to me as if ser will be said to be undefined, but it does look to me as if it might tell you that the port is already in use, possibly returning [] instead of an serial object. You should not be re-doing the serial(): you should be storing ser in your S structure and pulling it out of there.
Side note: it would be better if you split
S.pb = uicontrol('style','push',
'units','pixels',
'position',[240 20 80 50],
'fonts',14,
'str','Spray',
'callback',{@pb_call,S});
into
S.pb = uicontrol('style','push',
'units','pixels',
'position',[240 20 80 50],
'fonts',14,
'str','Spray') ;
set(S.pb, 'callback',{@pb_call,S});
Otherwise the value of S that gets recorded to be passed to the callback would be the S that did not yet have S.pb defined.
2 Commenti
Zahid Manzar
il 18 Apr 2016
Walter Roberson
il 18 Apr 2016
S.ser = ser;
before the
set(S.pb, 'callback',{@pb_call,S});
then reorder your statements slightly,
fopen(ser);
% Callback for the button labeled PUSH_1.
S = varargin{3}; % Get the structure.
to
% Callback for the button labeled PUSH_1.
S = varargin{3}; % Get the structure.
ser = S.ser;
fopen(ser);
Categorie
Scopri di più su Continuous Waveforms in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!