필터 지우기
필터 지우기

serial data communication, and calculation.?

조회 수: 2 (최근 30일)
Nimisha
Nimisha 2016년 3월 19일
댓글: Star Strider 2016년 3월 19일
p = serial('COM6');
fopen(p)
d = 0
for i = 1:1000
s = 0
s = fgets(p)
t = 1
d=(s-t)
if d == 0
s = 'o'
elseif d == 1
s = 'u'
elseif d == 2
s = 'w'
elseif d == 3
s = 'd'
elseif d == 4
s = 'y'
else
end
end
When i run this code, i am getting following output,
d = 50 12 9
It is obviously wrong. d value should be constant, as i am receiving serial data as either 1 or 2 or 3 or 4 or 5 numerics only.
So, difference will be 0 or 1 or 2 or 3 or 4 respectively.. What is error in code writing.?

채택된 답변

Star Strider
Star Strider 2016년 3월 19일
From the documentation, fgets (serial) returns a line of text from the serial device. The way I read your code, ‘d’ is subtracting 1 from the ASCII representation of the number ‘s’ represents.
See if:
d = (str2num(s) - t)
gives you the result you want.
Note that I cannot test this.
  댓글 수: 2
Nimisha
Nimisha 2016년 3월 19일
Thanks, Its working fine.
Star Strider
Star Strider 2016년 3월 19일
My pleasure!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by