フィルターのクリア

USB serial data receive problem and 'COM' port problem?

1 回表示 (過去 30 日間)
Nimisha
Nimisha 2016 年 4 月 20 日
コメント済み: Walter Roberson 2016 年 4 月 21 日
I have written following code to receive serial data and move cube. But I am not receiving data in MATLAB command window as well cube not rotating. I checked in terminal software about data receive, and hardware worksfine. Please correct my code.
A1 = 0; B1 = 0; C1 = 0;
A = [A1 B1 C1];
B = [A1+1 B1 C1];
C = [A1 B1+1 C1];
D = [A1 B1 C1+1];
E = [A1 B1+1 C1+1];
F = [A1+1 B1 C1+1];
G = [A1+1 B1+1 C1];
H = [A1+1 B1+1 C1+1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
axis([-25 25 -25 25 -25 25])
plot3(0,0,0,'or')
xlabel('X')
ylabel('Y')
zlabel('Z')
pause
s = serial('COM6');
fopen(s)
for i = 1:1000
P = fgets(s)
if P == '5'
A1 = 0; B1 = 0; C1 = 0;
A = [A1 B1 C1];
B = [A1+1 B1 C1];
C = [A1 B1+1 C1];
D = [A1 B1 C1+1];
E = [A1 B1+1 C1+1];
F = [A1+1 B1 C1+1];
G = [A1+1 B1+1 C1];
H = [A1+1 B1+1 C1+1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
axis([-25 25 -25 25 -25 25])
plot3(0,0,0,'or')
xlabel('X')
ylabel('Y')
zlabel('Z')
grid on
elseif P == '1'
A1 = A1+1; B1 = B1; C1 = C1;
A = [A1 B1 C1];
B = [A1+1 B1 C1];
C = [A1 B1+1 C1];
D = [A1 B1 C1+1];
E = [A1 B1+1 C1+1];
F = [A1+1 B1 C1+1];
G = [A1+1 B1+1 C1];
H = [A1+1 B1+1 C1+1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
axis([-25 25 -25 25 -25 25])
plot3(0,0,0,'or')
xlabel('X')
ylabel('Y')
zlabel('Z')
grid on
elseif P == '2'
A1 = A1-1; B1 = B1; C1 = C1;
A = [A1 B1 C1];
B = [A1+1 B1 C1];
C = [A1 B1+1 C1];
D = [A1 B1 C1+1];
E = [A1 B1+1 C1+1];
F = [A1+1 B1 C1+1];
G = [A1+1 B1+1 C1];
H = [A1+1 B1+1 C1+1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
axis([-25 25 -25 25 -25 25])
plot3(0,0,0,'or')
xlabel('X')
ylabel('Y')
zlabel('Z')
grid on
elseif P == '3'
A1 = A1; B1 = B1; C1 = C1+1;
A = [A1 B1 C1];
B = [A1+1 B1 C1];
C = [A1 B1+1 C1];
D = [A1 B1 C1+1];
E = [A1 B1+1 C1+1];
F = [A1+1 B1 C1+1];
G = [A1+1 B1+1 C1];
H = [A1+1 B1+1 C1+1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
axis([-25 25 -25 25 -25 25])
plot3(0,0,0,'or')
xlabel('X')
ylabel('Y')
zlabel('Z')
grid on
elseif P == '4'
A1 = A1; B1 = B1; C1 = C1-1;
A = [A1 B1 C1];
B = [A1+1 B1 C1];
C = [A1 B1+1 C1];
D = [A1 B1 C1+1];
E = [A1 B1+1 C1+1];
F = [A1+1 B1 C1+1];
G = [A1+1 B1+1 C1];
H = [A1+1 B1+1 C1+1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
axis([-25 25 -25 25 -25 25])
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
axis([-25 25 -25 25 -25 25])
plot3(0,0,0,'or')
xlabel('X')
ylabel('Y')
zlabel('Z')
grid on
else
end
end
fclose(s)
delete(instrfindall)

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 4 月 20 日
fgets() always returns the line with the line terminator included. You then compare the line to a single character. The line terminator is not going to match the character so the comparison will always be false.
I always recommend using fgetl() instead of fgets() except under uncommon circumstances. And if you are reading a single character, use fread() with a count of 1.
  3 件のコメント
Nimisha
Nimisha 2016 年 4 月 21 日
please help as soon as possible.!
Walter Roberson
Walter Roberson 2016 年 4 月 21 日
For the moment, for testing purposes, switch back to fgets(), but after you read the value, display double(P) and tell us what the values look like.
You should also be switching to use strcmp() instead of using == for strings.
I did not compare line for line, but it looks to me as if your different cases involve one different line such as
A1 = A1; B1 = B1; C1 = C1-1;
and that the rest of the drawing code is the same? If so then it would be more readable to have the if block only set the A1, B1, C1 as appropriate, and then after the end of the if, have the common drawing code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxes Transformations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by