Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Mzo. de 2018
set(handles.history_box, 'String', ...[currList ; ['Sent @ ' datestr(now) ': SF z0 ' str2num(z0valf) '<cr>']]);
  3 comentarios
Walter Roberson
Walter Roberson el 5 de Mzo. de 2018
Sorry I keep confusing str2num() and num2str()

Iniciar sesión para comentar.

Más respuestas (1)

Örs Istok
Örs Istok el 6 de Mzo. de 2018
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.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by