USB serial data receive problem and 'COM' port problem?
1 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!