questions about bytesavailablefcncount of a serial port.

조회 수: 2 (최근 30일)
Ramoflaple
Ramoflaple 2017년 1월 17일
편집: Vinod 2017년 3월 24일
I used a wireless module NRF24L01 to communicate with a host computer via a serial port. 24 bytes packets were sent every 80 ms.Then in matlab I created a serial port object configured as follows:
function joystick_OpeningFcn(hObject, ~, handles, varargin)
handles.output = hObject;
%create serial port
instrreset;
hwinfo=instrhwinfo('serial');
hwinfo=hwinfo.AvailableSerialPorts;
assert(~isscalar(hwinfo),'please plug in NRF24l01 receiver first');
s=serial(hwinfo{2},'BaudRate',115200,'DataBits',8,'StopBits',1,'Parity','none');
s.BytesAvailableFcnMode='byte';
s.TimerPeriod=0.01;
s.BytesAvailableFcnCount=24;
s.RecordDetail='verbose';
s.TimerFcn=@(o,e,data)dataread(o,e,handles);
handles.scom=s;
guidata(hObject, handles);
and the timer function was defined as follows:
function dataread(o,~,handles)
assert(isvalid(o),'the serial com object is no longer valid');
if o.bytesavailable>=24
a=reshape(fread(o,o.bytesavailablefcncount),3,uint8(o.bytesavailablefcncount/3));
for i=1:size(a,2)
if bitget(a(1,i),5)
tmp=bitcmp(a(2,i),'uint8')+1;
set(handles.(['x' num2str(i)]),'String',num2str(-int16(tmp)));
else
tmp=a(2,i);
set(handles.(['x' num2str(i)]),'String',num2str(int16(tmp)));
end
if bitget(a(1,i),6)
tmp=bitcmp(a(3,i),'uint8')+1;
set(handles.(['y' num2str(i)]),'String',num2str(-int16(tmp)));
else
tmp=a(3,i);
set(handles.(['y' num2str(i)]),'String',num2str(int16(tmp)));
end
end
end
however, the recorded events revealed something strange:
* _Legend:
* - An event occurred.
> - A write operation occurred.
< - A read operation occurred.
1 Recording on 16-Jan-2017 at 16:25:12.380. Binary data in little endian format.
2 * Timer event occurred at 16-Jan-2017 at 16:25:12.397.
3 * Timer event occurred at 16-Jan-2017 at 16:25:12.413.
4 * Timer event occurred at 16-Jan-2017 at 16:25:12.428.
5 < 24 uchar values.
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
6 * Timer event occurred at 16-Jan-2017 at 16:25:12.444.
7 * Timer event occurred at 16-Jan-2017 at 16:25:12.459.
8 * Timer event occurred at 16-Jan-2017 at 16:25:12.475.
9 * Timer event occurred at 16-Jan-2017 at 16:25:12.491.
10 * Timer event occurred at 16-Jan-2017 at 16:25:12.514.
11 * Timer event occurred at 16-Jan-2017 at 16:25:12.530.
12 < 24 uchar values.
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
13 * Timer event occurred at 16-Jan-2017 at 16:25:12.545.
14 * Timer event occurred at 16-Jan-2017 at 16:25:12.561.
15 * Timer event occurred at 16-Jan-2017 at 16:25:12.577.
16 * Timer event occurred at 16-Jan-2017 at 16:25:12.592.
17 < 24 uchar values.
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
18 * Timer event occurred at 16-Jan-2017 at 16:25:12.602.
19 * Timer event occurred at 16-Jan-2017 at 16:25:12.626._*
what does * < 24 uchar values.* mean and how to fix it? (In this application, it seemed to cause the data unaligned).
attached are the the events record.If there is something unclear please let me know.

채택된 답변

Vinod
Vinod 2017년 3월 22일
Behind the scenes, MATLAB is continuously polling the serial port for data and storing the results in a local buffer. The
s.RecordDetail='verbose';
sets the logging to verbose so you are seeing all this information. As you see in the "legend" of the verbose information,
* - An event occurred.
> - A write operation occurred.
< - A read operation occurred.
So,
2 * Timer event occurred at 16-Jan-2017 at 16:25:12.397.
Is to be interpreted as the timer that is continuously polling the serial port for data got an event to poll.
5 < 24 uchar values.
Is to be interpreted as 24 unsigned char (uchar) values were read back from the buffer of data available.
To answer your question: The data is likely unaligned because there is data in the buffer that you have not cleared out. Just before you FOPEN the port, you may want to FLUSHINPUT to clear any data in the buffer from when the buffer was previously pulling data.
  댓글 수: 2
Ramoflaple
Ramoflaple 2017년 3월 22일
thank you for your answer. I finally resorted to MFC to build my own serial communication and it worked very well. Since the data is very important to me, I have to assure there is no data dropped. Hence FLUSHINPUT is not going to be my option. I just doubt that the serial port object in Matlab can handle this in real time.
Vinod
Vinod 2017년 3월 22일
편집: Vinod 2017년 3월 24일
If you are using callbacks and have set appropriate buffer sizes, specifically InputBufferSize, on the serial object, there should be no data dropped.
I'm not sure of your specific application, but usually the devices that are sending data need to be sent a command to start sending the data. If you are doing this from MATLAB, you would Set up the callback, FOPEN, FLUSHINPUT, send the command to tell the device to start sending data, have your callback do what it needs to, including sending commands to the device to stop sending data, if needed.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by