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
2016 年 4 月 21 日
handles.serPIC.BytesAvailableFcn = @MyInterruptFcn;
handles.serPIC.BytesAvailableFcnCount = 1; %or as appropriate
fopen(handles.serPIC)
6 件のコメント
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
2016 年 4 月 21 日
You function will not get passed "handles". Change to
function intcon1(hObject, eventdata)
and
this_input = fread(hObject,1);
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
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
2016 年 4 月 21 日
thanks you its working
Jeff King
2018 年 1 月 16 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
