How to send data to the serial port 3 times

조회 수: 1 (최근 30일)
George Francis
George Francis 2016년 4월 23일
댓글: George Francis 2016년 4월 24일
Hello I have to send some data to my arduino. Each line separetly. But It sends me some lines together. So where should I put function fprintf? The data in penultimate example are correct but I have to send each line separetly.
clear all;
clc;
b = Bluetooth('HC-05', 1);
fopen(b);
x=[]; neg = 0;
for a=1:3
neg = neg - 1;
if (neg< -5)
neg = -1; %//wraparound
end
x = [x; [randi([0 9], 1, 5) neg]]; %//addnewrowofrandomnumbers to x
a = num2str(x);
pause(5);
fprintf(b,a);
disp(a);
end
disp('all data');
disp(a);
Disp(a) in for function
1 5 4 0 3 -1
1 5 4 0 3 -1
1 7 3 5 1 -2
1 5 4 0 3 -1
1 7 3 5 1 -2
6 2 6 6 7 -3
Disp(a) in the end of program
1 5 4 0 3 -1
1 7 3 5 1 -2
6 2 6 6 7 -3
When I have fprintf in for function it show me. First line is correct others not.
0 5 8 6 1 -1
03 54 89 61 18 --12
036 543 891 614 184 ---123

답변 (1개)

Walter Roberson
Walter Roberson 2016년 4월 23일
a = [randi([0 9], 1, 5), neg];
x(end+1, :) = a;
fprintf(b, '%d %d %d %d %d %d\n', a);
  댓글 수: 7
Walter Roberson
Walter Roberson 2016년 4월 24일
Replace
x = [x; [randi([0 9], 1, 5) neg]]; %//addnewrowofrandomnumbers to x
a = num2str(x);
pause(5);
fprintf(b,a);
with
a = [randi([0 9], 1, 5), neg];
x(end+1, :) = a;
fprintf(b, '%d %d %d %d %d %d\n', a);
If that does not work, I need to see the exact error message and the current version of your code.
George Francis
George Francis 2016년 4월 24일
I tried that again and it shows me different error and nothing came to Arduino.
Matlab code
clear all;
clc;
b = Bluetooth('HC-05', 1);
fopen(b);
x=[]; neg = 0;
for b=1:3
neg = neg - 1;
if (neg< -5)
neg = -1; %//wraparound
end
a = [randi([0 9], 1, 5) neg];
x(end+1,:) = a;
fprintf(b,'%d %d %d %d %d %d\n',a);
end
disp(a);
Matlab error
4 4 6 7 7 -1
2 6 6 1 1 -2
Error using fprintf
Invalid file identifier. Use fopen to generate a valid
file identifier.
Error in test4 (line 13)
fprintf(b,'%d %d %d %d %d %d\n',a);

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

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by