how to use serial port to get data from another device?
Show older comments
good day,
i want to get the data from a sensor called"waspmote" to matlab and plot these data, the data is from accelerometer to plot x,y and z.
my dr help me with the code, but i got an error on fopen, and the matrix that called coordinates not saved any value this is the code
% accelometer reading and plotting use it with acce2 code
s1 = serial('COM4'); % define serial port
s1.TimerPeriod=10;
%s1.Terminator='LF';
s1.Terminator='%';
s1.BaudRate=38400;
fopen(s1);
i=2;%beinging of reading
j=1;
X=0;
Y=0;
Z=0;
%FIGURE
figureHandle = figure('NumberTitle','off','Name','accelerometer','Visible','off')
%axesHandle = axes('Parent',figureHandle,'YGrid','on','XGrid','on','YMinorTick','off');
set(figureHandle,'Visible','on');
%plotHandle = plot3(axesHandle,X,Y,Z,'Marker','.','LineWidth',2);
hold all;
Coordinates=[0 0 0 0];
%start reading and plotting
pause(5);
while i>0
SensorReading(i,j)=native2unicode(fread(s1,1)','UTF-8');
if (SensorReading(i,j)=='%')&&(~isempty(str2num(SensorReading(i,findstr(SensorReading(i,:),'-x:')+3:findstr(SensorReading(i,:),',y')-1)))&&~isempty(str2num(SensorReading(i,findstr(SensorReading(i,:),'y:')+2:findstr(SensorReading(i,:),',z')-1))&&~isempty(str2num(SensorReading(i,findstr(SensorReading(i,:),'z:')+2:findstr(SensorReading(i,:),'-t')-1)))))
SensorReading(i,:);
Coordinates(i,4)=str2num(SensorReading(i,findstr(SensorReading(i,:),'Time: ')+6:findstr(SensorReading(i,:),' -X')-1));
Coordinates(i,4)=i;
Coordinates(i,1)=str2num(SensorReading(i,findstr(SensorReading(i,:),'-x:')+3:findstr(SensorReading(i,:),',y')-1));
Coordinates(i,2)=str2num(SensorReading(i,findstr(SensorReading(i,:),'y:')+2:findstr(SensorReading(i,:),',z')-1));
Coordinates(i,3)= str2num(SensorReading(i,findstr(SensorReading(i,:),'z:')+2:findstr(SensorReading(i,:),'-t')-1));
set(plotHandle,'YData',Coordinates(1:i,2),'XData',Coordinates(1:i,1),'ZData',Coordinates(1:i,3))
subplot(3,1,1) , plot(Coordinates(1:i,1)), xlabel('time'), ylabel('Gforce'), title('X-Axis');
subplot(3,1,2) , plot(Coordinates(1:i,2)), xlabel('time'), ylabel('Gforce'), title('Y-Axis');
subplot(3,1,3) , plot(Coordinates(1:i,3)), xlabel('time'), ylabel('Gforce'), title('Z-Axis');
set(figureHandle,'Visible','on');
hold off
i=i+1;%newline or new reading
j=1;
end
j=j+1;
end
%Clean up and shut it down
fclose (s1)
delete (s1)
clear s1
clear all
7 Comments
Walter Roberson
on 12 Nov 2012
What is the error you get from fopen() ?
sara sayyar
on 12 Nov 2012
Walter Roberson
on 12 Nov 2012
fread(s1,1) means to read one double (8 bytes) from the input stream. Then native2unicode() expects numeric values in the range 0 - 255 so the double would be converted as though uint8(). With the UTF-8 conversion, if that one value was in the range 0 to 127, then char(that integer) would be returned, but if the one value was larger (it cannot be larger as negatives underflow to 0 in uint8) then char(65533) would be returned. And if there was no input so the fread() returned [] then [] would be returned. You then attempt to store this char() or empty string into an array element.
Are you sure this is what you want to do? The answer is probably not to use a precision of '*uint8' on the fread(): if you are attempting to read in a string which is represented in UTF-8 and convert that string to Unicode, then you cannot process the bytes one-at-a-time through native2unicode() as it is not going to keep track of any boundary conditions about what was processed in the byte before. You would need to read in a line and convert the line -- which would, in general, result in a different number of characters.
Your code references COM4 but your error message references COM3 ? Are you sure about your COM port number? If it is a USB device then the port number can change. Have you tried instrfind() to locate the port?
sara sayyar
on 13 Nov 2012
Walter Roberson
on 13 Nov 2012
instrfind() is returning nothing, even though fopen() is telling you that COM1 is available?
Is this a USB port you are dealing with?
Does your device manager show COM3 as existing?
Is the computer a quite old one?
sara sayyar
on 13 Nov 2012
Edited: Walter Roberson
on 13 Nov 2012
Walter Roberson
on 13 Nov 2012
Devices for USB ports must be plugged in and turned on before you start MATLAB or else MATLAB will (probably) not be able to detect them. Also if you disconnect the device (or turn it off) then in theory it could show up as a different COM port.
Ports that do not show up in your system device manager cannot be used in MATLAB. You might need to load a USB driver from the manufacturer of the device.
Answers (0)
Categories
Find more on Instrument Control Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!