필터 지우기
필터 지우기

Matlab GUI: How to use fprintf function by writing a value between two chars

조회 수: 1 (최근 30일)
Main objective:
I want to write a GUI MATLAB code that will allow me to send commands to an instrument and receive information from instrument.
Introduction:
First of all, I wish to thank Roger Yeh for sharing his serial_GUI matlab code. I took his simple application and tried to implement for my purposes. I have a specific command set that I can use to communicate with the instrument. For example when setting fine values I need to use the following command: SF z0 40<cr> , where z0 is a variable; 40 is the set value and<cr> is carriage return.
Problem description:
When I wish to define my command within fprinf I don't seem to get the string/value/string format. Here is the code I use for sending information to the instrument:
z0valf=str2double(get(handles.z0f,'String'));
%fprintf(handles.serConn,'SF z0 %d<cr>\n',z0valf); %serConn - holds information about serial port
currList = get(handles.history_box, 'String');
set(handles.history_box, 'String', ...[currList ; ['Sent @ ' datestr(now) ': SF z0 ' z0valf '<cr>']]);
set(handles.history_box, 'Value', length(currList) + 1 );
Getting information from the instrument:
try
RxText = fscanf(handles.serConn)
currList = get(handles.history_box, 'String');
if length(RxText) < 1
RxText = 'Timeout @ ';
set(handles.history_box, 'String', ...
[currList ; [RxText datestr(now)] ]);
else
set(handles.history_box, 'String', ...
[currList ; ['Received @ ' datestr(now) ': ' RxText ] ]);
end
set(handles.history_box, 'Value', length(currList) + 1 );
catch e
disp(e)
end
Output in the history_box:
Sent @ 05-Mar-2018 19:25:19: SF z0 <cr>
Received @ 05-Mar-2018 19:25:29: SF z0 5<cr>
The □ should be 5, how could I change this?
What I also tried is to input everything in a editable box and just use that within the fprintf function.
My second problem is that I seem to experience a lag when I read data from the serial COM port. The reason I know this is that when let's say I set the fine value writing in the full command line within a textbox and send that to the instrument. When I try to read the data using the get fine value( GF z0<sp>) it does not give me the same information as when I write it in Putty.
Can anyone give any help on this? Thank you in advance. In case I did not give enough information/details please let me know.

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 5일
set(handles.history_box, 'String', ...[currList ; ['Sent @ ' datestr(now) ': SF z0 ' str2num(z0valf) '<cr>']]);
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 3월 5일
Sorry I keep confusing str2num() and num2str()

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Örs Istok
Örs Istok 2018년 3월 6일
Meanwhile I also figured out how to solve the lag between the instrument and computer. If any of you is interested, then I can shortly summarise it.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by