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
采纳的回答
handles.serPIC.BytesAvailableFcn = @MyInterruptFcn;
handles.serPIC.BytesAvailableFcnCount = 1; %or as appropriate
fopen(handles.serPIC)
6 个评论
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);
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.)
thanks you its working
Thanks, this post was extremely helpful!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
