필터 지우기
필터 지우기

connection to arduino using bluetooth

조회 수: 4 (최근 30일)
Abbas Hussien Miry
Abbas Hussien Miry 2016년 4월 5일
답변: Walter Roberson 2016년 4월 5일
hello
I am attempting to send some information from Matlab, to an Arduino Uno, via bluetooth with
matlab program
b=Bluetooth('HC-06',1);
fopen(b);
for i=1:1:15
fprintf(b,i);
out(i) = fscanf(b,'%d');
end
fclose(b)
and arduino program
int matlabval=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
matlabval=Serial.read();
Serial.println(matlabval);
}
}
the output of arduino is
1
10
2
10
3
10
4
10
5
10
6
10
7
10
8
10
9
10
10
10
11
10
12
10
13
10
14
10
15
10
the number 10 is appear after each number ?why?
Thanks in advance!

답변 (1개)

Walter Roberson
Walter Roberson 2016년 4월 5일
When you use
fprintf(b,i);
in MATLAB, because you did not specify a format, the default format used is '%s\n' . Each of your values for i is being converted using char() to become a character, and that is sent and then newline. Note that 1 would convert to char(1), the ASCII SOH (Start of Header) character, the one with binary value 1, not to '1', the digit 1, which is char(49) .
You should be considering using fwrite() instead of fprintf() if you want to send the values as binary, and if you want '1' and '2' and so on to be sent then you should use a format such as
fprintf(b, '%d\n', i)
You might need to adjust the read on the Arduino side.

카테고리

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