Why am I unable to generate bytes-available events from my Serial port ?

7 views (last 30 days)
Why am I unable to generate bytes-available events from my Serial port ?
I have a problem using an action function property in a serial connection with MATLAB. I want to connect the serial port of my computer to a device in interrupt mode (when my computer receives 10 bytes from the serial port, plot this data). I used the BytesAvailableAction function and these two scripts:
The first script is connect.m to establish a connection:
global s
s=serial('com1');
s.bytesavailableactionmode='byte';
s.bytesavailableactioncount=15;
s.bytesavailableaction='plotout';
fopen(s)
The MATLAB file that I expect to run when acquisition is interrupted is plotout.m:
function plotout(obj,event)
global s
global in1;
if s.bytesavailable>1
in1=fread (s,s.bytesavailable/2,'int16');
load in in
ts=size(in);
tad=size(in1);
in(1,ts(2)+1:ts(2)+tad(1))=in1';
save in in
plot(in)
pause
end
When I use these MATLAB files, plotout executes only one time and stops until I push the ‘Enter’ button.
I send 45 bytes to my computer's serial port from another port (COM2) with these commands:
s2=serial('com2');
fopen(s2)
for i=1:45
fwrite(s2, i,int16);
end
When it receives the first 15 bytes, it plot this data and stops, and I must push the ‘Enter’ button.
I want to be able to plot all the received data automatically without my interference

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Oct 2022
Edited: MathWorks Support Team on 8 Oct 2022
As stated in our documentation, regarding a "BytesAvailableAction" event:
A bytes-available event can be generated only for asynchronous read operations."
You can find information about this by typing "doc" at the MATLAB Command Window, and then clicking on "index" and then typing "bytesavailableaction".
To perform an asynchronous read, you should use the READASYNC function. For more information on this function, please type "doc readasync" at the MATLAB Command Window.
If you would like to generate events without changing how you are reading data, you should set the "BytesAvailableActionMode" to "terminator", and specify a terminator using the "Terminator" property.
Please look in the documentation index for more information on the "Terminator" property.
You can also use the PEEKDATA function to update a continuously running plot. For more information on PEEKDATA, please see the following URL:

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!