how to use serial port to get data from another device?

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

What is the error you get from fopen() ?
these are the errors that we got: The error Warning: The specified amount of data was not returned within the Timeout period. ??? Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in ==> acce_04 at 27 SensorReading(i,j)=native2unicode(fread(s1,1)','UTF-8');
??? Error using ==> serial.fopen at 72 Port: COM3 is not available. Available ports: COM1. Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in ==> acce_04 at 8 fopen(s1);
by the way, we can get in the workspace "SensorReading"when we open it we noticed that the data is exactly same as the data we got from the sensor with its program, but this data not save in the matrix "coordinates"
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?
about you question "Your code references COM4 but your error message references COM3 ?" .. Sorry i post the wrong port in the code but i tried the right port when i tried the code i used COM3. we have tried the function "instrfind()" but it returns nothing
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?
hi again, these are the answers of ur questions:
  • yes it returns nothing, when we tried COM1 it says that other ports availbe (same error)
  • i am dealing with usb port
  • no it shows COM4 existing
  • not too old, i have tested in other labtops (newer ones!) but i still get the same error
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.

Sign in to comment.

Answers (0)

Categories

Find more on Instrument Control Toolbox in Help Center and File Exchange

Tags

Asked:

on 12 Nov 2012

Community Treasure Hunt

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

Start Hunting!