Callback error with serial connection

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 Comment

Jan
Jan on 14 Apr 2016
Edited: Jan on 14 Apr 2016
Please format your code using the "{} Code" button. If you post the complete error message, it is easier (or possible) to understand, what happens. Which line causes the problem?

Sign in to comment.

Answers (1)

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 Comments

Thank you for your response but it seems that id I just place ends after the functions at the end of the code the functions end up sharing variables, thus solving the ser error.
However your answer was intriguing and was wondering how would I store the ser in the S structure and take it out?
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);

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Asked:

on 14 Apr 2016

Commented:

on 18 Apr 2016

Community Treasure Hunt

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

Start Hunting!