GUI SERIAL COMMUNICATION INTERRUPT FUNCTION

I want to run a serial communication with a device.
i want all incoming messages to be displayed automatically. don't want to press a special button for that.
i tried using the
handles.serPIC.BytesAvailableFcnCount = @MyInterruptFcn
...
...
...
function MyInterruptFcn(hObject, eventdata)
{code for the interrupt function}
end
but this has no result

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Apr. 2016

0 Stimmen

handles.serPIC.BytesAvailableFcn = @MyInterruptFcn;
handles.serPIC.BytesAvailableFcnCount = 1; %or as appropriate
fopen(handles.serPIC)

6 Kommentare

here is the relevant part of my code:
handles.SerPIC.BytesAvailableFcn = @intcon1;
handles.SerPIC.BytesAvailableFcnMode = 'byte';
handles.SerPIC.BytesAvailableFcnCount = 1;
... some code
function intcon1(hObject, eventdata, handles)
persistent current_state
persistent L;
persistent i;
persistent DATA;
persistent recieved_checksum;
% current_state = 0;
*this_input = fread(handles.SerPIC,1);*
switch current_state
case 0 % state 0 wait for HEADER_1
L=0;
i=0;
DATA=0;
recieved_checksum=0;
if (this_input == 85)
current_state = 1;
else
current_state = 0;
end
case 1 % state 1 wait for HEADER_2
if (this_input == 51) %was 170
current_state = 2;
recieved_checksum = 136;
elseif (this_input == 85)
current_state = 1;
else
current_state = 0;
end
case 2 % state 2 wait for message length
MESSAGE_LENGTH = this_input;
L=MESSAGE_LENGTH;
i=1;
current_state = 3;
recieved_checksum = mod(recieved_checksum + this_input,255);
case 3
% if (s.BytesAvailable == MESSAGE_LENGTH)
DATA(i) = this_input;
recieved_checksum = mod(recieved_checksum + this_input,255);
i=i+1;
L = L - 1;
if L > 0
current_state = 3;
else
current_state = 4;
end
case 4
recieved_checksum = recieved_checksum - this_input;
if (recieved_checksum ==0)
set(handles.temp_read,'string',[num2str(DATA(1))])
set(handles.voltage_read,'string',[num2str(DATA(2))])
set(handles.current_read,'string',[num2str(DATA(3))])
current_state = 0;
else
current_state = 0;
end
end
guidata(hObject,handles)
end
i get an error message regarding the callback function: undefined variable handles or class handless.serPIC error... this_input = fread(handles.serPIC,1)
You function will not get passed "handles". Change to
function intcon1(hObject, eventdata)
and
this_input = fread(hObject,1);
Daniel
Daniel am 21 Apr. 2016
thanks. that problem is solved. i also changed current_state paramter to global so i can share data and initialize it.
now at the end of the code i posted, i want to write data to a textbox acording to the data i recieve. but this function doesnt recognize handle... "the name 'temp_read' is not an accessible property for an instance of class 'serial port object'
question: how can i write to a textbox from within my callback function. i dont understand where this callback function "lives"
handles.SerPIC.BytesAvailableFcn = @(src, event) intcon1(src, event, handles.TextBoxToWriteTo);
function intcon1(hObject, eventdata, handle_to_output_to)
....
set(handle_to_output_to, 'String', ....)
The callback "lives" attached to the serial port object as a property that is a function handle. When MATLAB detects appropriate input, it will pause whatever else is happening, invoke the function handle, and when the invoked function returns, whatever was paused will be resumed. Any parent calling function will be buried in the guts of MATLAB. The situation is the same as with timers, I think (not completely sure.)
Daniel
Daniel am 21 Apr. 2016
thanks you its working
Jeff King
Jeff King am 16 Jan. 2018
Thanks, this post was extremely helpful!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by