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

%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

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.
on the first packet itself @walter roberson
Okay, have you tried adding a format to the fscanf()? Perhaps what you want is fgetl() instead of fscanf() ?
Undefined function 'fget1' for input arguments of type 'serial'. i am getting this error and i am facing same error which i have mentioned above even though i am using format specification
'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
same error is repeating even if i use fgetl() 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.
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.
126 0 16 131 16 7 40 0 1 30 0 0 176 0 166 0 168 0 169 119 this is one packet sent by xbee where 13,15,17,19 are sensor data and theres is no value equal to terminator
i am trying to read the data using fread i am able to get data but it is not correct
Are you saying that packets are determined by length and there is never a terminator?
the last of the value of the packet is varying from 118-125

Iniciar sesión para comentar.

Respuestas (1)

If there is never a terminator configure BytesAvailableFcnMode = 'byte' and do not configure Terminator, and use
a = fread(s, 20, '*uint8');

14 comentarios

i have configured terminator as empty and i used a = fread(s, 20, '*uint8'); then ia m getting error as
Invalid PRECISION specified. Type 'help serial/fread' for more information.
i tried using it as a = fread(s, 20, 'uint8'); but i coulsnt see the change in data
i am able to get the data but some of the data contains values less than 175,166,168,168 values which is equal to 0 force for each sensor.if i read the data normally using fread() by specifying buffer size i am getting accurate data
You could try 'int8' instead of 'uint8'
thank walter roberson for your suggestions i tried using 'int8' i wonder i am getting negative values
i am applying max force input to one of the sensor =255 but i am reading it as
If you get any value >= 128 in the int8 then something has gone wrong: values 128 and higher should come out negative in int8.
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.
this is the c++ code to get real time plot in matlab
can u please convert the given code to get data in to workspace
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;
i have changed byte order to big endian and followed the instruction you have given but i am getting the graph as
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??

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Mzo. de 2014

Comentada:

el 26 de Mzo. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by