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

 採用された回答

Walter Roberson
Walter Roberson 2016 年 4 月 21 日

0 投票

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

6 件のコメント

Daniel
Daniel 2016 年 4 月 21 日
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)
Walter Roberson
Walter Roberson 2016 年 4 月 21 日
You function will not get passed "handles". Change to
function intcon1(hObject, eventdata)
and
this_input = fread(hObject,1);
Daniel
Daniel 2016 年 4 月 21 日
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"
Walter Roberson
Walter Roberson 2016 年 4 月 21 日
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 2016 年 4 月 21 日
thanks you its working
Jeff King
Jeff King 2018 年 1 月 16 日
Thanks, this post was extremely helpful!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2016 年 4 月 21 日

コメント済み:

2018 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by