MATLAB Answers

0

questions about bytesavailablefcncount of a serial port.

Asked by Ramoflaple on 17 Jan 2017
Latest activity Edited by Vinod
on 24 Mar 2017
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.

  0 Comments

Sign in to comment.

1 Answer

Answer by Vinod
on 22 Mar 2017
 Accepted Answer

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 Comments

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.
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.

Sign in to comment.