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
Mostrar comentarios más antiguos
%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 comentarios
Walter Roberson
el 16 de Mzo. de 2014
On the first packet, or after it has been running for a time?
The behaviour of fscanf() is undefined when you do not provide a format specification.
avinash pabbireddy
el 16 de Mzo. de 2014
Walter Roberson
el 16 de Mzo. de 2014
Okay, have you tried adding a format to the fscanf()? Perhaps what you want is fgetl() instead of fscanf() ?
avinash pabbireddy
el 17 de Mzo. de 2014
Salaheddin Hosseinzadeh
el 18 de Mzo. de 2014
'fget1' is of course undefined to matlab it's FGETL all lower case. It reads a LINE that's why it ends with L not number one 1
lol
avinash pabbireddy
el 18 de Mzo. de 2014
Walter Roberson
el 18 de Mzo. de 2014
Try increasing your input buffer size -- if the terminator is indeed present then it is not going to matter that your buffer is longer than strictly required.
Double-check that LF alone is the terminator. CR alone or CR+LF are common.
avinash pabbireddy
el 19 de Mzo. de 2014
avinash pabbireddy
el 19 de Mzo. de 2014
Walter Roberson
el 19 de Mzo. de 2014
Are you saying that packets are determined by length and there is never a terminator?
avinash pabbireddy
el 19 de Mzo. de 2014
avinash pabbireddy
el 22 de Mzo. de 2014
avinash pabbireddy
el 26 de Mzo. de 2014
Respuestas (1)
Walter Roberson
el 19 de Mzo. de 2014
If there is never a terminator configure BytesAvailableFcnMode = 'byte' and do not configure Terminator, and use
a = fread(s, 20, '*uint8');
14 comentarios
avinash pabbireddy
el 19 de Mzo. de 2014
avinash pabbireddy
el 19 de Mzo. de 2014
Walter Roberson
el 20 de Mzo. de 2014
You could try 'int8' instead of 'uint8'
avinash pabbireddy
el 20 de Mzo. de 2014
avinash pabbireddy
el 20 de Mzo. de 2014
Walter Roberson
el 23 de Mzo. de 2014
If you get any value >= 128 in the int8 then something has gone wrong: values 128 and higher should come out negative in int8.
Walter Roberson
el 23 de Mzo. de 2014
When I look at your graph I get the impression that the values are biased, that a non-load input would be represented by a value other than 0. As if, for example, one should read the data in uint8 form, convert to int16, and then subtract 128 or perhaps 160 to get the actual value.
I'm not going to know without the documentation for the sensor.
avinash pabbireddy
el 23 de Mzo. de 2014
avinash pabbireddy
el 23 de Mzo. de 2014
Walter Roberson
el 25 de Mzo. de 2014
Each data channel is a two byte unsigned integer, stored big-endian (most significant value is first, which is not the default for fread()). Take the two bytes and convert to floating point. Subtract 170. Divide the result by 853 to get 0 to 1 pound.
ch1 = fread(s, 1, '*uint16', 'be');
force1 = (double(ch1) - 170) / 853;
avinash pabbireddy
el 26 de Mzo. de 2014
avinash pabbireddy
el 26 de Mzo. de 2014
avinash pabbireddy
el 26 de Mzo. de 2014
avinash pabbireddy
el 26 de Mzo. de 2014
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
