You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
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
1 view (last 30 days)
Show older comments
%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
Walter Roberson
on 16 Mar 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.
Walter Roberson
on 16 Mar 2014
Okay, have you tried adding a format to the fscanf()? Perhaps what you want is fgetl() instead of fscanf() ?
avinash pabbireddy
on 17 Mar 2014
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
Salaheddin Hosseinzadeh
on 18 Mar 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
on 18 Mar 2014
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.
Walter Roberson
on 18 Mar 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
on 19 Mar 2014
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
avinash pabbireddy
on 19 Mar 2014
i am trying to read the data using fread i am able to get data but it is not correct
Walter Roberson
on 19 Mar 2014
Are you saying that packets are determined by length and there is never a terminator?
Answers (1)
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
avinash pabbireddy
on 19 Mar 2014
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
avinash pabbireddy
on 19 Mar 2014
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
avinash pabbireddy
on 20 Mar 2014
thank walter roberson for your suggestions i tried using 'int8' i wonder i am getting negative values
avinash pabbireddy
on 20 Mar 2014
i am applying max force input to one of the sensor =255 but i am reading it as
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178200/image.jpeg)
Walter Roberson
on 23 Mar 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
on 23 Mar 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.
Walter Roberson
on 25 Mar 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
on 26 Mar 2014
i have changed byte order to big endian and followed the instruction you have given but i am getting the graph as
avinash pabbireddy
on 26 Mar 2014
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
avinash pabbireddy
on 26 Mar 2014
wat about left shift followed by and operation to convert in to 0-1 pound??
See Also
Categories
Find more on Cardiology 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)