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
2 次查看(过去 30 天)
显示 更早的评论
%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 个评论
Walter Roberson
2014-3-16
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
2014-3-16
Okay, have you tried adding a format to the fscanf()? Perhaps what you want is fgetl() instead of fscanf() ?
avinash pabbireddy
2014-3-17
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
2014-3-18
'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
2014-3-18
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
2014-3-18
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
2014-3-19
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
2014-3-19
i am trying to read the data using fread i am able to get data but it is not correct
Walter Roberson
2014-3-19
Are you saying that packets are determined by length and there is never a terminator?
回答(1 个)
Walter Roberson
2014-3-19
If there is never a terminator configure BytesAvailableFcnMode = 'byte' and do not configure Terminator, and use
a = fread(s, 20, '*uint8');
14 个评论
avinash pabbireddy
2014-3-19
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
2014-3-19
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
2014-3-20
thank walter roberson for your suggestions i tried using 'int8' i wonder i am getting negative values
avinash pabbireddy
2014-3-20
i am applying max force input to one of the sensor =255 but i am reading it as
Walter Roberson
2014-3-23
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
2014-3-23
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
2014-3-25
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
2014-3-26
i have changed byte order to big endian and followed the instruction you have given but i am getting the graph as
avinash pabbireddy
2014-3-26
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
2014-3-26
wat about left shift followed by and operation to convert in to 0-1 pound??
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)