Clear Filters
Clear Filters

Writting data via serial port in matlab

6 views (last 30 days)
Hi, I'm trying to send a 'N' character with a number from a math operation, then 2 commands packed in one. I have a 2x10 float number vector and i want to send the first numbers row with 'd' character, then the 'i' character with the second number row, but i cant use at all the fprintf commad. this is the logic
data_numbers=[0.0150, 0.0140;
0.0236, 0.0264;
0.0329, 0.0371;]
characters=['i','d','N']
number=((t1+t2+t3)/(tiempo_muestreo))-1;
pause(0.8);
fprintf(bt, 'N%d',number)
for i=1 : height(data_numbers)
pause(0.8);
fprintf(bt, '%c%.4f', characters{1}(1,1),data_numbers(1,i));
pause(0.8);
fprintf(bt, '%c%.4f', characters{2}(1,1),data_numbers(2,i))
end
first, when i compile, matlab show me error like the following
Error using icinterface/fprintf (line 94)
The third input argument must be a character vector or string.
Error in untitled>ENVIAR_Callback (line 674)
fprintf(bt, 'N%d',number)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled (line 43)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled('ENVIAR_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
I expect the output may:
N78
i0.0150
d0.0140
i0.0236
d0.0264
i0.0329
d0.0371
the number at the right side of 'N' character is already calculated but i have problems to send via fprintf to bluetooth, and the other data is without /n, I wrote that to make it more easy to read, actually it may be like:
N78i0.0150d0.0140i0.0236d0.0264i0.0329d0.0371

Answers (1)

Walter Roberson
Walter Roberson on 25 Feb 2019
If you look at the fprintf serial documentation, although it permits a number of format codes, the only form of data accepted for formatting is officially character vector. In practice, it also accepts double. But only one such parameter is permitted, whereas you are trying to send two such parameters.
The work-around is to use sprintf() to do the formatting for you, and fprintf() or fwrite() the result. If you use fwrite() then beware that occurances of \n will not be automatically translated to the Terminator character configured for the port... but fwrite() is otherwise a much easier function to deal with for this situation.

Community Treasure Hunt

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

Start Hunting!