MATLAB Answers

0

i am reading data from a node with four force sensors and Xbee and i have Xbee receiver.i want to get the sensors data continously int to my pc using serial communication.my sensor data lies in 13,15,17,19 bytes of a 20 byte packet sent by Xbee

Asked by avinash pabbireddy on 15 Mar 2014
Latest activity Commented on by avinash pabbireddy on 26 Mar 2014
%run('clean');
clear all;
close all;
delete(instrfindall);
   s = serial('COM5');   %assigns the object s to serial port
   s.ReadAsyncMode = 'continuous';
set(s, 'InputBufferSize',20);    %number of bytes in inout buffer
set(s, 'BaudRate', 115200);
set(s,'Terminator','LF')
%clc;
   x=0;
   y=0;
   z=0;
   w=0;
fopen(s);  
         %opens the serial port
disp('Running');
while(true)  
   a =fscanf(s);%reads the data from the serial port and stores it to the matrix a
   a1=str2num(a);
     s1=a1(13);
     x =[x s1];% Merging the value to an array, this is not very computationaly effective, as the array size is dynamic.
     plot(s1,'Color','g');
     axis auto;
     grid on;
     hold on;
     drawnow;
     s2=a1(15);
    y =[y s2];
     plot(s2,'Color','g');
     axis auto;
     grid on;
     hold on;
     drawnow;
      s3=a1(17);
       z =[z s3];
   plot(s3,'Color','g');
     axis auto;
     grid on;
     hold on;
     drawnow;
        s4=a1(19);
     w =[w s4];
      plot(s4,'Color','g');
     axis auto;
     grid on;
     hold on;
     drawnow;
     a1=0;  %Clear the buffer
fclose(s); %close the serial port
end

i am facing error-----Warning: Unsuccessful read: The input buffer was filled before the Terminator was reached. Attempted to access a1(13); index out of bounds because numel(a1)=0.

  13 Comments

the last of the value of the packet is varying from 118-125

@ MathWorks Support Team

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 19 Mar 2014

If there is never a terminator configure BytesAvailableFcnMode = 'byte' and do not configure Terminator, and use

a = fread(s, 20, '*uint8');

  14 Comments

what about the other 3 sensors??? thanks for your reply

ReadFile(g_hComPort, gyroBuffer, 7, &dwBytesRead, NULL);

ReadFile(g_hComPort, gyroBuffer, 9, &dwBytesRead, NULL); what are these two lines in C# code?? there is no change in output eventhough the input is changed to sensor

wat about left shift followed by and operation to convert in to 0-1 pound??


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!